1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use async_graphql::{InputValueError, Scalar, ScalarType, Value};
use chrono::Utc;

/// RFC3339 Time
pub struct Rfc3339Time(synd_feed::types::Time);

#[Scalar]
impl ScalarType for Rfc3339Time {
    fn parse(value: async_graphql::Value) -> async_graphql::InputValueResult<Self> {
        let Value::String(value) = value else {
            return Err(InputValueError::expected_type(value));
        };

        chrono::DateTime::parse_from_rfc3339(&value)
            .map(|t| t.with_timezone(&Utc))
            .map(Rfc3339Time)
            .map_err(InputValueError::custom)
    }

    fn to_value(&self) -> async_graphql::Value {
        async_graphql::Value::String(self.0.to_rfc3339())
    }
}

impl From<synd_feed::types::Time> for Rfc3339Time {
    fn from(value: synd_feed::types::Time) -> Self {
        Self(value)
    }
}