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

source

pub fn parse(ty: &str) -> Result<Self, TypeParseError>

Parses a string type representation into a new Type.

Example
use trustfall_core::ir::Type;

let ty = Type::parse("[String!]!").unwrap();
assert_eq!(ty.to_string(), "[String!]!");

assert_eq!(Type::parse("[String!]").unwrap().to_string(), "[String!]");
source

pub fn new_named_type(base_type: &str, nullable: bool) -> Self

Creates an individual Type, not a list.

Example
use trustfall_core::ir::Type;

let nullable = false;
let ty = Type::new_named_type("String", nullable);

assert_eq!(ty.to_string(), "String!");
assert_eq!(ty, Type::parse("String!").unwrap());
source

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());
source

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);
source

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
source

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);
source

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);
source

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");
source

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);
source

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 Clone for Type

source§

fn clone(&self) -> Type

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Type

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'de> Deserialize<'de> for Type

source§

fn deserialize<D>(deserializer: D) -> Result<Type, D::Error>where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for Type

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl PartialEq for Type

source§

fn eq(&self, other: &Type) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Serialize for Type

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Eq for Type

source§

impl StructuralEq for Type

source§

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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<V> AsVertex<V> for Vwhere V: Debug + Clone,

source§

fn as_vertex(&self) -> Option<&V>

Dereference this value into a &V, if the value happens to contain a V. Read more
source§

fn into_vertex(self) -> Option<V>

Consume self and produce the contained V, if one was indeed present. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,