use async_graphql::{InputValueError, InputValueResult, Object, Scalar, ScalarType, Value};
use super::{DetectedLanguage, Language, LanguageId, LanguageIdOperand, Region, ScriptSubtag};
#[cfg(test)]
mod tests;
macro_rules! subtag_scalar {
($(#[$description:meta])* $subtag:ident, $name:literal, $error:ident) => {
$(#[$description])*
#[Scalar(name = $name)]
impl ScalarType for $subtag {
fn parse(value: Value) -> InputValueResult<Self> {
match &value {
Value::String(text) => Self::new(text).map_err(InputValueError::custom),
_ => Err(InputValueError::expected_type(value.clone())),
}
}
#[doc = concat!("[`", stringify!($error), "`](super::", stringify!($error), ")")]
#[inline]
fn is_valid(value: &Value) -> bool {
match value {
Value::String(text) => Self::new(text).is_ok(),
_ => false,
}
}
#[inline]
fn to_value(&self) -> Value {
Value::String(String::from(self.as_str()))
}
}
};
}
subtag_scalar! {
Language, "Language", ParseLanguageError
}
subtag_scalar! {
ScriptSubtag, "ScriptSubtag", ParseScriptSubtagError
}
subtag_scalar! {
Region, "Region", ParseRegionError
}
#[Scalar(name = "LanguageIdOperand")]
impl ScalarType for LanguageIdOperand {
fn parse(value: Value) -> InputValueResult<Self> {
match &value {
Value::String(text) => super::LanguageId::new(text)
.map(Self::new)
.map_err(InputValueError::custom),
_ => Err(InputValueError::expected_type(value.clone())),
}
}
#[inline]
fn is_valid(value: &Value) -> bool {
match value {
Value::String(text) => super::LanguageId::new(text).is_ok(),
_ => false,
}
}
#[inline]
fn to_value(&self) -> Value {
Value::String(self.get().to_string())
}
}
#[Object]
impl LanguageId {
#[doc(hidden)]
#[graphql(name = "tag")]
async fn __async_graphql_tag__(&self) -> String {
self.to_string()
}
#[doc(hidden)]
#[graphql(name = "language")]
async fn __async_graphql_language__(&self) -> Language {
self.language().clone()
}
#[doc(hidden)]
#[graphql(name = "script")]
async fn __async_graphql_script__(&self) -> Option<ScriptSubtag> {
self.script().cloned()
}
#[doc(hidden)]
#[graphql(name = "region")]
async fn __async_graphql_region__(&self) -> Option<Region> {
self.region().cloned()
}
#[doc(hidden)]
#[graphql(name = "rest")]
async fn __async_graphql_rest__(&self) -> Option<String> {
self.rest().map(|rest| String::from(rest.as_str()))
}
}
#[Object]
impl DetectedLanguage {
#[doc(hidden)]
#[graphql(name = "tag")]
async fn __async_graphql_tag__(&self) -> String {
self.language().to_string()
}
#[doc(hidden)]
#[graphql(name = "confidence")]
async fn __async_graphql_confidence__(&self) -> crate::F32 {
<crate::Score as crate::ToGraphqlOutput>::to_graphql_output(&self.confidence())
}
#[doc(hidden)]
#[graphql(name = "language")]
async fn __async_graphql_language__(&self) -> Language {
self.language().language().clone()
}
#[doc(hidden)]
#[graphql(name = "script")]
async fn __async_graphql_script__(&self) -> Option<ScriptSubtag> {
self.language().script().cloned()
}
#[doc(hidden)]
#[graphql(name = "region")]
async fn __async_graphql_region__(&self) -> Option<Region> {
self.language().region().cloned()
}
#[doc(hidden)]
#[graphql(name = "rest")]
async fn __async_graphql_rest__(&self) -> Option<String> {
self
.language()
.rest()
.map(|rest| String::from(rest.as_str()))
}
}