Skip to content

Commit

Permalink
refactor: trade field_[primitive] functions for a bunch of FromJson…
Browse files Browse the repository at this point in the history
… impls
  • Loading branch information
QuietMisdreavus committed Aug 8, 2016
1 parent d6a3d28 commit e691722
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 63 deletions.
60 changes: 40 additions & 20 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,46 @@ impl FromJson for i64 {
}
}

impl FromJson for i32 {
fn from_json(input: &json::Json) -> Result<Self, error::Error> {
input.as_i64().map(|x| x as i32).ok_or(InvalidResponse)
}
}

impl FromJson for String {
fn from_json(input: &json::Json) -> Result<Self, error::Error> {
input.as_string().map(|s| s.to_string()).ok_or(InvalidResponse)
}
}

impl FromJson for bool {
fn from_json(input: &json::Json) -> Result<Self, error::Error> {
input.as_boolean().ok_or(InvalidResponse)
}
}

impl FromJson for (i32, i32) {
fn from_json(input: &json::Json) -> Result<Self, error::Error> {
//assumptions: input is
// - an array
// - of integers
// - with exactly two entries
//any deviation from these assumptions will return an error.
let int_vec = try!(input.as_array()
.ok_or(InvalidResponse)
.and_then(|v| v.iter()
.map(|i| i.as_i64())
.collect::<Option<Vec<_>>>()
.ok_or(InvalidResponse)));

if int_vec.len() != 2 {
return Err(InvalidResponse);
}

Ok((int_vec[0] as i32, int_vec[1] as i32))
}
}

///With the given response struct, parse it into a String.
pub fn response_raw(resp: &mut HyperResponse) -> Result<String, error::Error> {
let mut full_resp = String::new();
Expand Down Expand Up @@ -253,26 +293,6 @@ pub fn parse_response<T>(resp: &mut HyperResponse) -> Result<Response<T>, error:
})
}

///Extract a boolean field from the given Json.
pub fn field_bool(input: &json::Json, field: &'static str) -> Result<bool, error::Error> {
input.find(field).and_then(|f| f.as_boolean()).ok_or(MissingValue(field))
}

///Extract a string field from the given Json.
pub fn field_string(input: &json::Json, field: &'static str) -> Result<String, error::Error> {
input.find(field).and_then(|f| f.as_string()).map(|f| f.to_string()).ok_or(MissingValue(field))
}

///Extract an i64 field from the given Json.
pub fn field_i64(input: &json::Json, field: &'static str) -> Result<i64, error::Error> {
input.find(field).and_then(|f| f.as_i64()).ok_or(MissingValue(field))
}

///Extract an i32 field from the given Json.
pub fn field_i32(input: &json::Json, field: &'static str) -> Result<i32, error::Error> {
field_i64(input, field).map(|f| f as i32)
}

pub fn field<T: FromJson>(input: &json::Json, field: &'static str) -> Result<T, error::Error> {
T::from_json(try!(input.find(field).ok_or(MissingValue(field))))
}
86 changes: 43 additions & 43 deletions src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,50 +234,50 @@ impl FromJson for TwitterUser {
}

Ok(TwitterUser {
contributors_enabled: field_bool(input, "contributors_enabled").unwrap_or(false),
created_at: try!(field_string(input, "created_at")),
default_profile: try!(field_bool(input, "default_profile")),
default_profile_image: try!(field_bool(input, "default_profile_image")),
description: field_string(input, "description").ok(),
contributors_enabled: field(input, "contributors_enabled").unwrap_or(false),
created_at: try!(field(input, "created_at")),
default_profile: try!(field(input, "default_profile")),
default_profile_image: try!(field(input, "default_profile_image")),
description: field(input, "description").ok(),
//TODO: entities: ???,
favourites_count: try!(field_i32(input, "favourites_count")),
follow_request_sent: field_bool(input, "follow_request_sent").ok(),
following: field_bool(input, "following").ok(),
followers_count: try!(field_i32(input, "followers_count")),
friends_count: try!(field_i32(input, "friends_count")),
geo_enabled: try!(field_bool(input, "geo_enabled")),
id: try!(field_i64(input, "id")),
is_translator: try!(field_bool(input, "is_translator")),
lang: try!(field_string(input, "lang")),
listed_count: try!(field_i32(input, "listed_count")),
location: field_string(input, "location").ok(),
name: try!(field_string(input, "name")),
notifications: field_bool(input, "notifications").ok(),
profile_background_color: try!(field_string(input, "profile_background_color")),
profile_background_image_url: field_string(input, "profile_background_image_url").ok(),
profile_background_image_url_https: field_string(input, "profile_background_image_url_https").ok(),
profile_background_tile: field_bool(input, "profile_background_tile").ok(),
profile_banner_url: field_string(input, "profile_banner_url").ok(),
profile_image_url: try!(field_string(input, "profile_image_url")),
profile_image_url_https: try!(field_string(input, "profile_image_url_https")),
profile_link_color: try!(field_string(input, "profile_link_color")),
profile_sidebar_border_color: try!(field_string(input, "profile_sidebar_border_color")),
profile_sidebar_fill_color: try!(field_string(input, "profile_sidebar_fill_color")),
profile_text_color: try!(field_string(input, "profile_text_color")),
profile_use_background_image: try!(field_bool(input, "profile_use_background_image")),
protected: try!(field_bool(input, "protected")),
screen_name: try!(field_string(input, "screen_name")),
show_all_inline_media: field_bool(input, "show_all_inline_media").ok(),
favourites_count: try!(field(input, "favourites_count")),
follow_request_sent: field(input, "follow_request_sent").ok(),
following: field(input, "following").ok(),
followers_count: try!(field(input, "followers_count")),
friends_count: try!(field(input, "friends_count")),
geo_enabled: try!(field(input, "geo_enabled")),
id: try!(field(input, "id")),
is_translator: try!(field(input, "is_translator")),
lang: try!(field(input, "lang")),
listed_count: try!(field(input, "listed_count")),
location: field(input, "location").ok(),
name: try!(field(input, "name")),
notifications: field(input, "notifications").ok(),
profile_background_color: try!(field(input, "profile_background_color")),
profile_background_image_url: field(input, "profile_background_image_url").ok(),
profile_background_image_url_https: field(input, "profile_background_image_url_https").ok(),
profile_background_tile: field(input, "profile_background_tile").ok(),
profile_banner_url: field(input, "profile_banner_url").ok(),
profile_image_url: try!(field(input, "profile_image_url")),
profile_image_url_https: try!(field(input, "profile_image_url_https")),
profile_link_color: try!(field(input, "profile_link_color")),
profile_sidebar_border_color: try!(field(input, "profile_sidebar_border_color")),
profile_sidebar_fill_color: try!(field(input, "profile_sidebar_fill_color")),
profile_text_color: try!(field(input, "profile_text_color")),
profile_use_background_image: try!(field(input, "profile_use_background_image")),
protected: try!(field(input, "protected")),
screen_name: try!(field(input, "screen_name")),
show_all_inline_media: field(input, "show_all_inline_media").ok(),
//TODO: status: ???,
statuses_count: try!(field_i32(input, "statuses_count")),
time_zone: field_string(input, "time_zone").ok(),
url: field_string(input, "url").ok(),
utc_offset: field_i32(input, "utc_offset").ok(),
verified: try!(field_bool(input, "verified")),
statuses_count: try!(field(input, "statuses_count")),
time_zone: field(input, "time_zone").ok(),
url: field(input, "url").ok(),
utc_offset: field(input, "utc_offset").ok(),
verified: try!(field(input, "verified")),
withheld_in_countries: input.find("withheld_in_countries").and_then(|f| f.as_array())
.and_then(|arr| arr.iter().map(|x| x.as_string().map(|x| x.to_string()))
.collect::<Option<Vec<String>>>()),
withheld_scope: field_string(input, "withheld_scope").ok(),
withheld_scope: field(input, "withheld_scope").ok(),
})
}
}
Expand Down Expand Up @@ -581,8 +581,8 @@ impl FromJson for UserCursor {
}

Ok(UserCursor {
previous_cursor: try!(field_i64(input, "previous_cursor")),
next_cursor: try!(field_i64(input, "next_cursor")),
previous_cursor: try!(field(input, "previous_cursor")),
next_cursor: try!(field(input, "next_cursor")),
users: try!(field(input, "users")),
})
}
Expand Down Expand Up @@ -726,8 +726,8 @@ impl FromJson for IDCursor {
}

Ok(IDCursor {
previous_cursor: try!(field_i64(input, "previous_cursor")),
next_cursor: try!(field_i64(input, "next_cursor")),
previous_cursor: try!(field(input, "previous_cursor")),
next_cursor: try!(field(input, "next_cursor")),
ids: try!(field(input, "ids")),
})
}
Expand Down

0 comments on commit e691722

Please sign in to comment.