Skip to content

Commit 5ffc714

Browse files
author
Andronik Ordian
committed
Use serde derive where possible
1 parent d02cf0e commit 5ffc714

File tree

8 files changed

+394
-372
lines changed

8 files changed

+394
-372
lines changed

Cargo.lock

Lines changed: 375 additions & 241 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/dependency.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ pub struct EncodableDependency {
4848
pub downloads: i32,
4949
}
5050

51-
#[derive(Copy, Clone, Debug)]
51+
#[derive(Copy, Clone, Serialize, Deserialize, Debug)]
52+
#[serde(rename_all = "lowercase")]
5253
#[repr(u32)]
5354
pub enum Kind {
5455
Normal = 0,

src/krate.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,7 @@ pub fn show(req: &mut Request) -> CargoResult<Response> {
10341034

10351035
#[derive(Serialize)]
10361036
struct R {
1037+
#[serde(rename = "crate")]
10371038
krate: EncodableCrate,
10381039
versions: Vec<EncodableVersion>,
10391040
keywords: Vec<EncodableKeyword>,
@@ -1185,6 +1186,7 @@ pub fn new(req: &mut Request) -> CargoResult<Response> {
11851186

11861187
#[derive(Serialize)]
11871188
struct R<'a> {
1189+
#[serde(rename = "crate")]
11881190
krate: EncodableCrate,
11891191
warnings: Warnings<'a>,
11901192
}

src/tests/all.rs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ extern crate time;
1919
extern crate url;
2020
extern crate s3;
2121

22-
use serde_json::Value;
2322
use std::borrow::Cow;
2423
use std::collections::HashMap;
2524
use std::env;
@@ -167,33 +166,9 @@ where
167166
let mut data = Vec::new();
168167
r.body.write_body(&mut data).unwrap();
169168
let s = std::str::from_utf8(&data).unwrap();
170-
let j = match serde_json::from_str(s) {
169+
match serde_json::from_str(s) {
171170
Ok(t) => t,
172171
Err(e) => panic!("failed to decode: {:?}\n{}", e, s),
173-
};
174-
let j = fixup(j);
175-
return match serde_json::from_value::<T>(j) {
176-
Ok(t) => t,
177-
Err(e) => panic!("failed to decode: {:?}\n{}", e, s),
178-
};
179-
180-
181-
fn fixup(json: Value) -> Value {
182-
match json {
183-
Value::Object(object) => {
184-
Value::Object(
185-
object
186-
.into_iter()
187-
.map(|(k, v)| {
188-
let k = if k == "crate" { "krate".to_string() } else { k };
189-
(k, fixup(v))
190-
})
191-
.collect(),
192-
)
193-
}
194-
Value::Array(list) => Value::Array(list.into_iter().map(fixup).collect()),
195-
j => j,
196-
}
197172
}
198173
}
199174

src/tests/krate.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@ struct Warnings {
4646
}
4747
#[derive(Deserialize)]
4848
struct GoodCrate {
49+
#[serde(rename = "crate")]
4950
krate: EncodableCrate,
5051
warnings: Warnings,
5152
}
5253
#[derive(Deserialize)]
5354
struct CrateResponse {
55+
#[serde(rename = "crate")]
5456
krate: EncodableCrate,
5557
versions: Vec<EncodableVersion>,
5658
keywords: Vec<EncodableKeyword>,

src/token.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ pub struct ApiToken {
2323
}
2424

2525
/// The serialization format for the `ApiToken` model without its token value.
26-
pub struct EncodableApiToken {
2726
#[derive(Deserialize, Serialize)]
27+
pub struct EncodableApiToken {
2828
pub id: i32,
2929
pub name: String,
3030
pub created_at: String,
@@ -34,8 +34,8 @@ pub struct EncodableApiToken {
3434
/// The serialization format for the `ApiToken` model with its token value.
3535
/// This should only be used when initially creating a new token to minimize
3636
/// the chance of token leaks.
37-
pub struct EncodableApiTokenWithToken {
3837
#[derive(Deserialize, Serialize)]
38+
pub struct EncodableApiTokenWithToken {
3939
pub id: i32,
4040
pub name: String,
4141
pub token: String,

src/upload.rs

Lines changed: 7 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,24 @@ pub struct NewCrate {
2727
pub badges: Option<HashMap<String, HashMap<String, String>>>,
2828
}
2929

30-
#[derive(PartialEq, Eq, Hash, Debug)]
30+
#[derive(PartialEq, Eq, Hash, Serialize, Debug)]
3131
pub struct CrateName(pub String);
3232
#[derive(Debug)]
3333
pub struct CrateVersion(pub semver::Version);
3434
#[derive(Debug)]
3535
pub struct CrateVersionReq(pub semver::VersionReq);
36-
#[derive(Debug)]
36+
#[derive(Serialize, Debug)]
3737
pub struct KeywordList(pub Vec<Keyword>);
38-
#[derive(Debug)]
38+
#[derive(Serialize, Debug)]
3939
pub struct Keyword(pub String);
40-
#[derive(Debug)]
40+
#[derive(Serialize, Debug)]
4141
pub struct CategoryList(pub Vec<Category>);
42-
#[derive(Debug)]
42+
#[derive(Serialize, Deserialize, Debug)]
4343
pub struct Category(pub String);
44-
#[derive(Debug)]
44+
#[derive(Serialize, Debug)]
4545
pub struct Feature(pub String);
4646

47-
#[derive(Deserialize, Serialize, Debug)]
47+
#[derive(Serialize, Deserialize, Debug)]
4848
pub struct CrateDependency {
4949
pub optional: bool,
5050
pub default_features: bool,
@@ -94,12 +94,6 @@ impl<'de> Deserialize<'de> for Keyword {
9494
}
9595
}
9696

97-
impl<'de> Deserialize<'de> for Category {
98-
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Category, D::Error> {
99-
String::deserialize(d).map(Category)
100-
}
101-
}
102-
10397
impl<'de> Deserialize<'de> for Feature {
10498
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Feature, D::Error> {
10599
let s = String::deserialize(d)?;
@@ -178,40 +172,6 @@ impl<'de> Deserialize<'de> for CategoryList {
178172
}
179173
}
180174
}
181-
182-
impl<'de> Deserialize<'de> for DependencyKind {
183-
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<DependencyKind, D::Error> {
184-
let s = String::deserialize(d)?;
185-
match &s[..] {
186-
"dev" => Ok(DependencyKind::Dev),
187-
"build" => Ok(DependencyKind::Build),
188-
"normal" => Ok(DependencyKind::Normal),
189-
_ => {
190-
const KINDS: &'static [&'static str] = &["dev", "build", "normal"];
191-
Err(de::Error::unknown_variant(&s, KINDS))
192-
}
193-
}
194-
}
195-
}
196-
197-
macro_rules! serialize_string {
198-
($name:ty) => (
199-
impl Serialize for $name {
200-
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
201-
where S: Serializer
202-
{
203-
serializer.serialize_str(self.deref())
204-
}
205-
}
206-
)
207-
}
208-
209-
serialize_string!(CrateName);
210-
serialize_string!(Keyword);
211-
serialize_string!(Category);
212-
serialize_string!(Feature);
213-
214-
215175
impl Serialize for CrateVersion {
216176
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
217177
where
@@ -230,39 +190,6 @@ impl Serialize for CrateVersionReq {
230190
}
231191
}
232192

233-
impl Serialize for KeywordList {
234-
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
235-
where
236-
S: Serializer,
237-
{
238-
let KeywordList(ref inner) = *self;
239-
inner.serialize(serializer)
240-
}
241-
}
242-
243-
impl Serialize for CategoryList {
244-
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
245-
where
246-
S: Serializer,
247-
{
248-
let CategoryList(ref inner) = *self;
249-
inner.serialize(serializer)
250-
}
251-
}
252-
253-
impl Serialize for DependencyKind {
254-
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
255-
where
256-
S: Serializer,
257-
{
258-
match *self {
259-
DependencyKind::Normal => "normal".serialize(serializer),
260-
DependencyKind::Build => "build".serialize(serializer),
261-
DependencyKind::Dev => "dev".serialize(serializer),
262-
}
263-
}
264-
}
265-
266193
impl Deref for CrateName {
267194
type Target = str;
268195
fn deref(&self) -> &str {

src/util/mod.rs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::error::Error;
33
use std::io::{self, Cursor};
44
use std::sync::Arc;
55

6-
use serde_json::{self, Value};
6+
use serde_json;
77
use serde::Serialize;
88
use url;
99

@@ -37,36 +37,17 @@ pub trait RequestUtils {
3737
}
3838

3939
pub fn json_response<T: Serialize>(t: &T) -> Response {
40-
let s = serde_json::to_string(t).unwrap();
41-
let json = fixup(s.parse().unwrap()).to_string();
40+
let json = serde_json::to_string(t).unwrap();
4241
let mut headers = HashMap::new();
4342
headers.insert(
4443
"Content-Type".to_string(),
4544
vec!["application/json; charset=utf-8".to_string()],
4645
);
4746
headers.insert("Content-Length".to_string(), vec![json.len().to_string()]);
48-
return Response {
47+
Response {
4948
status: (200, "OK"),
5049
headers: headers,
5150
body: Box::new(Cursor::new(json.into_bytes())),
52-
};
53-
54-
fn fixup(json: Value) -> Value {
55-
match json {
56-
Value::Object(object) => {
57-
Value::Object(
58-
object
59-
.into_iter()
60-
.map(|(k, v)| {
61-
let k = if k == "krate" { "crate".to_string() } else { k };
62-
(k, fixup(v))
63-
})
64-
.collect(),
65-
)
66-
}
67-
Value::Array(list) => Value::Array(list.into_iter().map(fixup).collect()),
68-
j => j,
69-
}
7051
}
7152
}
7253

0 commit comments

Comments
 (0)