Skip to content

Commit

Permalink
refactor: turn deserialize_datetime into a serde_datetime module
Browse files Browse the repository at this point in the history
  • Loading branch information
QuietMisdreavus committed Jun 29, 2020
1 parent d5c2f45 commit e6c34d1
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 23 deletions.
38 changes: 21 additions & 17 deletions src/common/mod.rs
Expand Up @@ -94,7 +94,6 @@ use std::future::Future;
use std::iter::Peekable;
use std::pin::Pin;

use chrono::{self, TimeZone};
use hyper::header::{HeaderMap, HeaderValue};
use mime;
use percent_encoding::{utf8_percent_encode, AsciiSet, PercentEncode};
Expand Down Expand Up @@ -427,23 +426,28 @@ where
}
}

pub fn deserialize_datetime<'de, D>(ser: D) -> Result<chrono::DateTime<chrono::Utc>, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(ser)?;
let date = (chrono::Utc)
.datetime_from_str(&s, "%a %b %d %T %z %Y")
.map_err(|e| D::Error::custom(e))?;
Ok(date)
}
pub mod serde_datetime {
use serde::{Serializer, Deserialize, Deserializer};
use serde::de::Error;
use chrono::TimeZone;

pub fn deserialize<'de, D>(ser: D) -> Result<chrono::DateTime<chrono::Utc>, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(ser)?;
let date = (chrono::Utc)
.datetime_from_str(&s, "%a %b %d %T %z %Y")
.map_err(|e| D::Error::custom(e))?;
Ok(date)
}

pub fn serialize_datetime<S>(src: &chrono::DateTime<chrono::Utc>, ser: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let out = src.format("%a %b %d %T %z %Y").to_string();
ser.serialize_str(&out)
pub fn serialize<S>(src: &chrono::DateTime<chrono::Utc>, ser: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
ser.collect_str(&src.format("%a %b %d %T %z %Y"))
}
}

pub fn deserialize_mime<'de, D>(ser: D) -> Result<mime::Mime, D::Error>
Expand Down
2 changes: 1 addition & 1 deletion src/list/mod.rs
Expand Up @@ -160,7 +160,7 @@ pub struct List {
///to create a link to the list.
pub uri: String,
///UTC timestamp of when the list was created.
#[serde(deserialize_with = "deserialize_datetime")]
#[serde(with = "serde_datetime")]
pub created_at: chrono::DateTime<chrono::Utc>,
}

Expand Down
3 changes: 1 addition & 2 deletions src/tweet/mod.rs
Expand Up @@ -153,8 +153,7 @@ round_trip! { raw::RawTweet,
///If present, the location coordinate attached to the tweet, as a (latitude, longitude) pair.
pub coordinates: Option<(f64, f64)>,
///UTC timestamp from when the tweet was posted.
#[serde(deserialize_with = "deserialize_datetime")]
#[serde(serialize_with = "serialize_datetime")]
#[serde(with = "serde_datetime")]
pub created_at: chrono::DateTime<chrono::Utc>,
///If the authenticated user has retweeted this tweet, contains the ID of the retweet.
pub current_user_retweet: Option<u64>,
Expand Down
6 changes: 4 additions & 2 deletions src/tweet/raw.rs
Expand Up @@ -2,15 +2,17 @@ use crate::{place, user};
use chrono;
use serde::Deserialize;

use crate::common::{serde_datetime};

use super::{
deserialize_datetime, deserialize_tweet_source, ExtendedTweetEntities, FilterLevel, Tweet,
deserialize_tweet_source, ExtendedTweetEntities, FilterLevel, Tweet,
TweetEntities, TweetSource,
};

#[derive(Debug, Clone, Deserialize)]
pub(crate) struct RawTweet {
pub coordinates: Option<RawCoordinates>,
#[serde(deserialize_with = "deserialize_datetime")]
#[serde(with = "serde_datetime")]
pub created_at: chrono::DateTime<chrono::Utc>,
pub current_user_retweet: Option<CurrentUserRetweet>,
pub display_text_range: Option<(usize, usize)>,
Expand Down
2 changes: 1 addition & 1 deletion src/user/raw.rs
Expand Up @@ -16,7 +16,7 @@ pub struct RawTwitterUser {
/// for Tweets issued by the user to be co-authored by another account. Rarely `true`.
pub contributors_enabled: bool,
/// The UTC timestamp for when this user account was created on Twitter.
#[serde(deserialize_with = "deserialize_datetime")]
#[serde(with = "serde_datetime")]
pub created_at: chrono::DateTime<chrono::Utc>,
/// When true, indicates that this user has not altered the theme or background of
/// their user profile.
Expand Down

0 comments on commit e6c34d1

Please sign in to comment.