use core::num::{NonZeroU32, NonZeroUsize};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Route<T> {
weight: f64,
enabled: bool,
depth: Option<NonZeroUsize>,
options: Option<T>,
}
impl<T> Route<T> {
pub const DEFAULT_WEIGHT: f64 = 1.0;
pub const DEFAULT_ENABLED: bool = true;
#[inline]
#[must_use]
pub const fn new() -> Self {
Self {
weight: Self::DEFAULT_WEIGHT,
enabled: Self::DEFAULT_ENABLED,
depth: None,
options: None,
}
}
#[inline]
#[must_use]
pub fn with_weight(mut self, weight: f64) -> Self {
self.weight = weight;
self
}
#[inline]
#[must_use]
pub fn with_enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
#[inline]
#[must_use]
pub const fn with_depth(mut self, depth: NonZeroUsize) -> Self {
self.depth = Some(depth);
self
}
#[inline]
pub const fn set_depth(&mut self, depth: Option<NonZeroUsize>) -> &mut Self {
self.depth = depth;
self
}
#[inline]
#[must_use]
pub fn with_options(mut self, options: T) -> Self {
self.options = Some(options);
self
}
#[inline]
#[must_use]
pub const fn weight(&self) -> f64 {
self.weight
}
#[inline]
#[must_use]
pub const fn enabled(&self) -> bool {
self.enabled
}
#[inline]
#[must_use]
pub const fn depth(&self) -> Option<NonZeroUsize> {
self.depth
}
#[inline]
pub const fn options(&self) -> Option<&T> {
self.options.as_ref()
}
}
#[inline]
#[must_use]
pub fn route_depth(
edge: Option<NonZeroUsize>,
declared: Option<&NonZeroU32>,
budget: NonZeroUsize,
) -> NonZeroUsize {
let asked = edge.unwrap_or(budget);
match declared {
Some(ceiling) => asked.min(NonZeroUsize::try_from(*ceiling).unwrap_or(NonZeroUsize::MAX)),
None => asked,
}
}
impl<T> Default for Route<T> {
#[inline]
fn default() -> Self {
Self::new()
}
}
#[cfg(feature = "async-graphql")]
impl<T: async_graphql::InputType> async_graphql::InputType for Route<T> {
type RawValueType = Self;
fn type_name() -> ::std::borrow::Cow<'static, str> {
::std::borrow::Cow::Owned(::std::format!("{}Route", T::type_name()))
}
fn create_type_info(registry: &mut async_graphql::registry::Registry) -> ::std::string::String {
registry.create_input_type::<Self, _>(
async_graphql::registry::MetaTypeId::InputObject,
|registry| {
let mut fields = async_graphql::indexmap::IndexMap::new();
fields.insert(
::std::string::String::from("weight"),
route_input_value::<f64>(registry, "weight", "1"),
);
fields.insert(
::std::string::String::from("enabled"),
route_input_value::<bool>(registry, "enabled", "true"),
);
fields.insert(
::std::string::String::from("depth"),
async_graphql::registry::MetaInputValue {
name: ::std::string::String::from("depth"),
description: ::std::option::Option::None,
ty: <::core::option::Option<::core::num::NonZeroUsize> as async_graphql::InputType>
::create_type_info(registry),
deprecation: async_graphql::registry::Deprecation::NoDeprecated,
default_value: ::std::option::Option::None,
visible: ::std::option::Option::None,
inaccessible: false,
tags: ::std::vec::Vec::new(),
is_secret: false,
directive_invocations: ::std::vec::Vec::new(),
},
);
fields.insert(
::std::string::String::from("options"),
async_graphql::registry::MetaInputValue {
name: ::std::string::String::from("options"),
description: ::std::option::Option::None,
ty: <::core::option::Option<T> as async_graphql::InputType>::create_type_info(registry),
deprecation: async_graphql::registry::Deprecation::NoDeprecated,
default_value: ::std::option::Option::None,
visible: ::std::option::Option::None,
inaccessible: false,
tags: ::std::vec::Vec::new(),
is_secret: false,
directive_invocations: ::std::vec::Vec::new(),
},
);
async_graphql::registry::MetaType::InputObject {
name: ::std::borrow::Cow::into_owned(Self::type_name()),
description: ::std::option::Option::None,
input_fields: fields,
visible: ::std::option::Option::None,
inaccessible: false,
tags: ::std::vec::Vec::new(),
rust_typename: ::std::option::Option::Some(::std::any::type_name::<Self>()),
oneof: false,
directive_invocations: ::std::vec::Vec::new(),
}
},
)
}
fn parse(
value: ::std::option::Option<async_graphql::Value>,
) -> async_graphql::InputValueResult<Self> {
let ::std::option::Option::Some(async_graphql::Value::Object(object)) = value else {
return ::std::result::Result::Err(async_graphql::InputValueError::expected_type(
value.unwrap_or_default(),
));
};
::std::result::Result::Ok(Self {
weight: route_slot::<f64, Self>(&object, "weight")?.unwrap_or(Self::DEFAULT_WEIGHT),
enabled: route_slot::<bool, Self>(&object, "enabled")?.unwrap_or(Self::DEFAULT_ENABLED),
depth: route_slot::<NonZeroUsize, Self>(&object, "depth")?,
options: route_slot::<T, Self>(&object, "options")?,
})
}
fn to_value(&self) -> async_graphql::Value {
let mut map = async_graphql::indexmap::IndexMap::new();
map.insert(
async_graphql::Name::new("weight"),
async_graphql::InputType::to_value(&self.weight),
);
map.insert(
async_graphql::Name::new("enabled"),
async_graphql::InputType::to_value(&self.enabled),
);
map.insert(
async_graphql::Name::new("depth"),
async_graphql::InputType::to_value(&self.depth),
);
map.insert(
async_graphql::Name::new("options"),
async_graphql::InputType::to_value(&self.options),
);
async_graphql::Value::Object(map)
}
#[inline]
fn as_raw_value(&self) -> ::std::option::Option<&Self::RawValueType> {
::std::option::Option::Some(self)
}
}
#[cfg(feature = "async-graphql")]
impl<T: async_graphql::InputType> async_graphql::InputObjectType for Route<T> {}
#[cfg(feature = "async-graphql")]
fn route_input_value<V: async_graphql::InputType>(
registry: &mut async_graphql::registry::Registry,
name: &str,
default: &str,
) -> async_graphql::registry::MetaInputValue {
async_graphql::registry::MetaInputValue {
name: ::std::string::String::from(name),
description: ::std::option::Option::None,
ty: <::core::option::Option<V> as async_graphql::InputType>::create_type_info(registry),
deprecation: async_graphql::registry::Deprecation::NoDeprecated,
default_value: ::std::option::Option::Some(::std::string::String::from(default)),
visible: ::std::option::Option::None,
inaccessible: false,
tags: ::std::vec::Vec::new(),
is_secret: false,
directive_invocations: ::std::vec::Vec::new(),
}
}
#[cfg(feature = "async-graphql")]
fn route_slot<V, F>(
object: &async_graphql::indexmap::IndexMap<async_graphql::Name, async_graphql::Value>,
name: &str,
) -> ::std::result::Result<::std::option::Option<V>, async_graphql::InputValueError<F>>
where
V: async_graphql::InputType,
F: async_graphql::InputType,
{
<::core::option::Option<V> as async_graphql::InputType>::parse(object.get(name).cloned())
.map_err(async_graphql::InputValueError::propagate)
}
#[cfg(test)]
mod depth;
#[cfg(test)]
mod tests;