Also support Duration
type via humantime
crate
This commit is contained in:
parent
b909996af1
commit
e9bc72b035
1 changed files with 27 additions and 1 deletions
|
@ -1,6 +1,7 @@
|
||||||
use std::time::{SystemTime};
|
use std::time::{SystemTime, Duration};
|
||||||
|
|
||||||
use humantime::{parse_rfc3339_weak, format_rfc3339};
|
use humantime::{parse_rfc3339_weak, format_rfc3339};
|
||||||
|
use humantime::{parse_duration, format_duration};
|
||||||
|
|
||||||
use Value;
|
use Value;
|
||||||
|
|
||||||
|
@ -19,6 +20,20 @@ graphql_scalar!(SystemTime as "DateTime" {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
graphql_scalar!(Duration as "Duration" {
|
||||||
|
description: "Duration in human-readable form, like '15min 2ms'"
|
||||||
|
|
||||||
|
resolve(&self) -> Value {
|
||||||
|
Value::string(format_duration(*self).to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
from_input_value(v: &InputValue) -> Option<Duration> {
|
||||||
|
v.as_string_value().and_then(|s| {
|
||||||
|
parse_duration(s).ok()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
|
@ -32,9 +47,20 @@ mod test {
|
||||||
assert_eq!(parsed, expected);
|
assert_eq!(parsed, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn duration(raw: &'static str) -> Duration {
|
||||||
|
let input = ::InputValue::String(raw.to_string());
|
||||||
|
::FromInputValue::from_input_value(&input).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn datetime_utc_from_input_value() {
|
fn datetime_utc_from_input_value() {
|
||||||
datetime_utc_test("2014-11-28T21:00:09Z", 1417208409)
|
datetime_utc_test("2014-11-28T21:00:09Z", 1417208409)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn duration_test() {
|
||||||
|
assert_eq!(duration("2min"), Duration::new(120, 0));
|
||||||
|
assert_eq!(duration("2m 15sec 12ms"), Duration::new(135, 12_000_000));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue