Skip to content

Commit 94fa720

Browse files
authored
EncodableApiTokenWithToken: Use serde(flatten) to reuse ApiToken serialization (#6324)
1 parent 84fe774 commit 94fa720

File tree

2 files changed

+8
-50
lines changed

2 files changed

+8
-50
lines changed

src/models/token.rs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ impl std::fmt::Debug for CreatedApiToken {
104104
#[cfg(test)]
105105
mod tests {
106106
use super::*;
107-
use crate::views::EncodableApiTokenWithToken;
108107
use chrono::NaiveDate;
109108

110109
#[test]
@@ -136,32 +135,4 @@ mod tests {
136135
.as_str()
137136
.find(r#""last_used_at":"2017-01-06T14:23:12+00:00""#));
138137
}
139-
140-
#[test]
141-
fn encodeable_api_token_with_token_serializes_to_rfc3339() {
142-
let tok = EncodableApiTokenWithToken {
143-
id: 12345,
144-
name: "".to_string(),
145-
token: "".to_string(),
146-
created_at: NaiveDate::from_ymd_opt(2017, 1, 6)
147-
.unwrap()
148-
.and_hms_opt(14, 23, 11)
149-
.unwrap(),
150-
last_used_at: Some(
151-
NaiveDate::from_ymd_opt(2017, 1, 6)
152-
.unwrap()
153-
.and_hms_opt(14, 23, 12),
154-
)
155-
.unwrap(),
156-
crate_scopes: None,
157-
endpoint_scopes: None,
158-
};
159-
let json = serde_json::to_string(&tok).unwrap();
160-
assert_some!(json
161-
.as_str()
162-
.find(r#""created_at":"2017-01-06T14:23:11+00:00""#));
163-
assert_some!(json
164-
.as_str()
165-
.find(r#""last_used_at":"2017-01-06T14:23:12+00:00""#));
166-
}
167138
}

src/views.rs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ use chrono::NaiveDateTime;
22
use url::Url;
33

44
use crate::github;
5-
use crate::models::token::{CrateScope, EndpointScope};
65
use crate::models::{
7-
Category, Crate, CrateOwnerInvitation, CreatedApiToken, Dependency, DependencyKind, Keyword,
8-
Owner, ReverseDependency, Team, TopVersions, User, Version, VersionDownload,
6+
ApiToken, Category, Crate, CrateOwnerInvitation, CreatedApiToken, Dependency, DependencyKind,
7+
Keyword, Owner, ReverseDependency, Team, TopVersions, User, Version, VersionDownload,
98
VersionOwnerAction,
109
};
1110
use crate::util::rfc3339;
@@ -470,29 +469,17 @@ impl From<Team> for EncodableTeam {
470469
/// the chance of token leaks.
471470
#[derive(Serialize, Debug)]
472471
pub struct EncodableApiTokenWithToken {
473-
pub id: i32,
474-
pub name: String,
475-
pub token: String,
476-
#[serde(with = "rfc3339")]
477-
pub created_at: NaiveDateTime,
478-
#[serde(with = "rfc3339::option")]
479-
pub last_used_at: Option<NaiveDateTime>,
480-
/// `None` or a list of crate scope patterns (see RFC #2947)
481-
pub crate_scopes: Option<Vec<CrateScope>>,
482-
/// A list of endpoint scopes or `None` for the `legacy` endpoint scope (see RFC #2947)
483-
pub endpoint_scopes: Option<Vec<EndpointScope>>,
472+
#[serde(flatten)]
473+
pub token: ApiToken,
474+
#[serde(rename = "token")]
475+
pub plaintext: String,
484476
}
485477

486478
impl From<CreatedApiToken> for EncodableApiTokenWithToken {
487479
fn from(token: CreatedApiToken) -> Self {
488480
EncodableApiTokenWithToken {
489-
id: token.model.id,
490-
name: token.model.name,
491-
token: token.plaintext,
492-
created_at: token.model.created_at,
493-
last_used_at: token.model.last_used_at,
494-
crate_scopes: token.model.crate_scopes,
495-
endpoint_scopes: token.model.endpoint_scopes,
481+
token: token.model,
482+
plaintext: token.plaintext,
496483
}
497484
}
498485
}

0 commit comments

Comments
 (0)