async_graphql/types/external/
duration.rs

1use std::str::FromStr;
2
3use chrono::Duration;
4
5use crate::{InputValueError, InputValueResult, Scalar, ScalarType, Value};
6
7/// Implement the Duration scalar
8///
9/// The input/output is a string in ISO8601 format.
10#[Scalar(
11    internal,
12    name = "Duration",
13    specified_by_url = "https://en.wikipedia.org/wiki/ISO_8601#Durations"
14)]
15impl ScalarType for Duration {
16    fn parse(value: Value) -> InputValueResult<Self> {
17        match &value {
18            Value::String(s) => Ok(Duration::from_std(std::time::Duration::from(
19                iso8601::Duration::from_str(s)?,
20            ))?),
21            _ => Err(InputValueError::expected_type(value)),
22        }
23    }
24
25    fn to_value(&self) -> Value {
26        Value::String(self.to_string())
27    }
28}