Struct trustfall_core::ir::Type
source · pub struct Type { /* private fields */ }
Expand description
A representation of a Trustfall type, independent of which parser or query syntax we’re using. Equivalent in expressiveness to GraphQL types, but not explicitly tied to a GraphQL library.
Implementations§
source§impl Type
impl Type
sourcepub fn new_named_type(base_type: &str, nullable: bool) -> Self
pub fn new_named_type(base_type: &str, nullable: bool) -> Self
sourcepub fn new_list_type(inner_type: Self, nullable: bool) -> Self
pub fn new_list_type(inner_type: Self, nullable: bool) -> Self
Creates a new list layer on a Type
.
Example
use trustfall_core::ir::Type;
let inner_nullable = false;
let inner_ty = Type::new_named_type("String", inner_nullable);
let outer_nullable = true;
let ty = Type::new_list_type(inner_ty, outer_nullable);
assert_eq!(ty.to_string(), "[String!]");
assert_eq!(ty, Type::parse("[String!]").unwrap());
sourcepub fn with_nullability(&self, nullable: bool) -> Self
pub fn with_nullability(&self, nullable: bool) -> Self
Returns a new type that is the same as this one, but with the passed nullability.
Example
use trustfall_core::ir::Type;
let nullable_ty = Type::parse("Int").unwrap();
assert_eq!(nullable_ty.nullable(), true);
let non_nullable_ty = nullable_ty.with_nullability(false);
assert_eq!(non_nullable_ty.nullable(), false);
// The original type is unchanged.
assert_eq!(nullable_ty.nullable(), true);
sourcepub fn nullable(&self) -> bool
pub fn nullable(&self) -> bool
Returns whether this type is nullable, at the top level, see example.
Example
use trustfall_core::ir::Type;
let nullable_ty = Type::parse("[Int!]").unwrap();
assert_eq!(nullable_ty.nullable(), true); // the list is nullable
let nullable_ty = Type::parse("Int!").unwrap();
assert_eq!(nullable_ty.nullable(), false); // the `Int` is nonnullable
sourcepub fn is_list(&self) -> bool
pub fn is_list(&self) -> bool
Returns whether the type is a list or not.
Example
use trustfall_core::ir::Type;
let non_null_int_arr = Type::parse("[Int!]").unwrap();
assert_eq!(non_null_int_arr.is_list(), true);
let non_null_int = Type::parse("Int!").unwrap();
assert_eq!(non_null_int.is_list(), false);
sourcepub fn as_list(&self) -> Option<Self>
pub fn as_list(&self) -> Option<Self>
Returns the type inside the outermost list of this type if it is a list, otherwise returns None
.
Example
use trustfall_core::ir::Type;
let non_null_int_arr = Type::parse("[Int!]").unwrap();
let non_null_int = Type::parse("Int!").unwrap();
assert_eq!(non_null_int_arr.as_list(), Some(non_null_int.clone()));
assert_eq!(non_null_int.as_list(), None);
sourcepub fn base_type(&self) -> &str
pub fn base_type(&self) -> &str
Returns the type of the elements of the first individual type found inside this type.
Example
use trustfall_core::ir::Type;
let int_list_ty = Type::parse("[Int!]").unwrap();
assert_eq!(int_list_ty.base_type(), "Int");
let string_ty = Type::parse("String!").unwrap();
assert_eq!(string_ty.base_type(), "String");
sourcepub fn intersect(&self, other: &Self) -> Option<Self>
pub fn intersect(&self, other: &Self) -> Option<Self>
For two types, return a type that is a subtype of both, or None if no such type exists. For example:
use trustfall_core::ir::Type;
let left = Type::parse("[String]!").unwrap();
let right = Type::parse("[String!]").unwrap();
let result = left.intersect(&right);
assert_eq!(Some(Type::parse("[String!]!").unwrap()), result);
let incompatible = Type::parse("[Int]").unwrap();
let result = left.intersect(&incompatible);
assert_eq!(None, result);
sourcepub fn is_valid_value(&self, value: &FieldValue) -> bool
pub fn is_valid_value(&self, value: &FieldValue) -> bool
Check if the given value is allowed by the specified type.
In particular, mixed integer types in a list are considered valid for types like [Int]
.
use trustfall_core::ir::{FieldValue, Type};
let ty = Type::parse("[Int]").unwrap();
let value = FieldValue::List([
FieldValue::Int64(-1),
FieldValue::Uint64(1),
FieldValue::Null,
].as_slice().into());
assert!(ty.is_valid_value(&value));
Trait Implementations§
source§impl<'de> Deserialize<'de> for Type
impl<'de> Deserialize<'de> for Type
source§fn deserialize<D>(deserializer: D) -> Result<Type, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Type, D::Error>where D: Deserializer<'de>,
source§impl PartialEq for Type
impl PartialEq for Type
impl Eq for Type
impl StructuralEq for Type
impl StructuralPartialEq for Type
Auto Trait Implementations§
impl RefUnwindSafe for Type
impl Send for Type
impl Sync for Type
impl Unpin for Type
impl UnwindSafe for Type
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.