flickr 0.0.1

Rust binding to Flickr API
Documentation

#[macro_use]
pub mod macros;
pub mod auth;
pub mod favorites;

use std::fmt;
use std::str;
use std::string;

use serde::de::{self, Deserialize, Deserializer, Unexpected};
use chrono::prelude::{DateTime, Utc, Local, TimeZone};

// ---- Content -----------------------------------------------------------------------------------
#[derive(Deserialize, Debug, Default)]
struct Content {
  #[serde(default, rename = "_content")]
  pub content: String,
}

impl fmt::Display for Content {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.content)?;
        Ok(())
    }
}

impl str::FromStr for Content {
    type Err = string::ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Content{ content: s.to_string() })
    }
}

// ---- deserialize functions ---------------------------------------------------------------------
/// Deserialize i32 from sub-block with `_content` field.
fn i32_from_content_block<'de, D>(deserializer: D) -> Result<i32, D::Error>
where
    D: Deserializer<'de>,
{
    match Content::deserialize(deserializer)?.content.parse() {
        Ok(i) => {
            return Ok(i);
        },
        Err(_e) => {
            return Err(de::Error::invalid_value(Unexpected::Str(&""), &"i32 as string",));
        }
    }
}

/// Deserialize string from sub-block with `_content` field.
fn string_from_content_block<'de, D>(deserializer: D) -> Result<String, D::Error>
where
    D: Deserializer<'de>,
{
    return Ok(Content::deserialize(deserializer)?.content);
}

/// Deserialize bool from int
fn bool_from_int<'de, D>(deserializer: D) -> Result<bool, D::Error>
where
    D: Deserializer<'de>,
{
    match u8::deserialize(deserializer)? {
        0 => Ok(false),
        1 => Ok(true),
        other => Err(de::Error::invalid_value(
            Unexpected::Unsigned(other as u64),
            &"zero or one",
        )),
    }
}

/// Deserialize i32 from string
fn i32_from_string<'de, D>(deserializer: D) -> Result<i32, D::Error>
where
    D: Deserializer<'de>,
{
    match String::deserialize(deserializer)?.parse() {
        Ok(i) => {
            return Ok(i);
        },
        Err(_e) => {
            return Err(de::Error::invalid_value(Unexpected::Str(&""), &"i32 as string",));
        }
    }
}

/// Deserialize date from string
fn date_from_string<'de, D>(deserializer: D) -> Result<DateTime<Local>, D::Error>
where
    D: Deserializer<'de>,
{
    match String::deserialize(deserializer)?.parse::<i64>() {
        Ok(a) => {
            let utc = Utc.timestamp(a, 0);
            let local_tz = Local::now().timezone();
            return Ok(utc.with_timezone(&local_tz));
        },
        Err(_e) => {
            return Err(de::Error::invalid_value(Unexpected::Str(&""), &"i64 as string",));
        }
    }
}