use crate::{
JiffCivilDateTime, JiffZoneOffset, ToGraphqlOutput,
graphql::filter::temporal::{parse_offset, parse_wall_clock},
};
use async_graphql::{
InputType, InputValueError, InputValueResult, Object, Scalar, ScalarType, Value,
};
use super::{LocalTimestamp, LocalTimestampOperand};
#[cfg(test)]
mod tests;
#[Scalar(
name = "LocalTimestampOperand",
specified_by_url = "https://en.wikipedia.org/wiki/ISO_8601"
)]
impl ScalarType for LocalTimestampOperand {
fn parse(value: Value) -> InputValueResult<Self> {
capture(&value).map(Self).map_err(InputValueError::custom)
}
fn is_valid(value: &Value) -> bool {
capture(value).is_ok()
}
fn to_value(&self) -> Value {
Value::String(render(self.0))
}
}
fn render(capture: LocalTimestamp) -> String {
match capture.offset() {
Some(offset) => format!("{}{offset}", capture.local()),
None => capture.local().to_string(),
}
}
fn capture(value: &Value) -> Result<LocalTimestamp, String> {
match value {
Value::Object(object) => {
const LOCAL: &str = "local";
const OFFSET: &str = "offset";
if let Some(unknown) = object
.keys()
.find(|key| !matches!(key.as_str(), LOCAL | OFFSET))
{
return Err(format!(
"a capture time holds `{LOCAL}` and an optional `{OFFSET}`, not `{unknown}`"
));
}
let local = object
.get(LOCAL)
.ok_or_else(|| String::from("a capture time states its wall clock; `local` is missing"))?;
let local =
<JiffCivilDateTime as InputType>::parse(Some(local.clone())).map_err(|refused| {
refused
.into_server_error(async_graphql::Pos::default())
.message
})?;
let offset = match object.get(OFFSET) {
None | Some(Value::Null) => None,
Some(offset) => Some(
<JiffZoneOffset as InputType>::parse(Some(offset.clone())).map_err(|refused| {
refused
.into_server_error(async_graphql::Pos::default())
.message
})?,
),
};
Ok(LocalTimestamp::new(
local.get(),
offset.map(JiffZoneOffset::get),
))
}
Value::String(text) => parse_text(text),
Value::Number(_) => Err(String::from(
"a capture time is a clock reading: send \"2021-08-14T16:45:09\" or the same with an offset \
— a bare number names no clock face",
)),
_ => Err(String::from("a capture time is a string or an object")),
}
}
fn parse_text(text: &str) -> Result<LocalTimestamp, String> {
let (local, offset) = split_offset(text);
let local = parse_wall_clock(local)?;
let offset = offset.map(parse_offset).transpose()?;
Ok(LocalTimestamp::new(local, offset))
}
fn split_offset(text: &str) -> (&str, Option<&str>) {
let Some(separator) = text.find(['T', 't', ' ']) else {
return (text, None);
};
match text[separator + 1..].find(['+', '-', 'Z', 'z']) {
Some(at) => {
let (local, offset) = text.split_at(separator + 1 + at);
(local, Some(offset))
}
None => (text, None),
}
}
impl ToGraphqlOutput for LocalTimestamp {
type Output<'a> = &'a Self;
#[inline]
fn to_graphql_output(capture: &Self) -> Self::Output<'_> {
capture
}
}
#[Object]
impl LocalTimestamp {
#[doc(hidden)]
#[graphql(name = "local")]
async fn __async_graphql_local__(&self) -> JiffCivilDateTime {
JiffCivilDateTime::from(LocalTimestamp::local(self))
}
#[doc(hidden)]
#[graphql(name = "offset")]
async fn __async_graphql_offset__(&self) -> Option<JiffZoneOffset> {
LocalTimestamp::offset(self).map(JiffZoneOffset::from)
}
#[doc(hidden)]
#[graphql(name = "zoned")]
async fn __async_graphql_zoned__(&self) -> bool {
self.is_zoned()
}
#[doc(hidden)]
#[graphql(name = "utc")]
async fn __async_graphql_utc__(&self) -> Option<crate::Timestamp> {
self.to_utc().ok().map(crate::Timestamp::from)
}
#[doc(hidden)]
#[graphql(name = "utcNanos")]
async fn __async_graphql_utc_nanos__(&self) -> Option<i64> {
self.to_utc_nanos().ok()
}
#[doc(hidden)]
#[graphql(name = "text")]
async fn __async_graphql_text__(&self) -> String {
render(*self)
}
}