use crate::{registry, InputValueError, InputValueResult, InputValueType, Type, Value};
use async_graphql_parser::UploadValue;
use std::borrow::Cow;
use std::io::Read;
pub struct Upload(UploadValue);
impl Upload {
pub fn filename(&self) -> &str {
self.0.filename.as_str()
}
pub fn content_type(&self) -> Option<&str> {
self.0.content_type.as_deref()
}
pub fn into_read(self) -> impl Read + Sync + Send + 'static {
self.0.content
}
}
impl<'a> Type for Upload {
fn type_name() -> Cow<'static, str> {
Cow::Borrowed("Upload")
}
fn create_type_info(registry: &mut registry::Registry) -> String {
registry.create_type::<Self, _>(|_| registry::MetaType::Scalar {
name: Self::type_name().to_string(),
description: None,
is_valid: |value| match value {
Value::String(s) => s.starts_with("file:"),
_ => false,
},
})
}
}
impl<'a> InputValueType for Upload {
fn parse(value: Option<Value>) -> InputValueResult<Self> {
let value = value.unwrap_or_default();
if let Value::Upload(upload) = value {
Ok(Upload(upload))
} else {
Err(InputValueError::ExpectedType(value))
}
}
fn to_value(&self) -> Value {
Value::Null
}
}