Skip to main content

xur_runtime/marshal/
type_desc.rs

1//! Printable descriptions of the types a native function takes and returns.
2//!
3//! A [`Signature`](crate::registry::Signature)'s shapes are what argument
4//! marshaling checks against; types with no `Facet` shape of their own
5//! ([`XurStr`](super::XurStr), [`Value`](crate::Value), [`Opt`](super::Opt),
6//! ...) have none to give. A [`TypeDesc`] describes every parameter and the
7//! return type for people instead: hovers, inlay hints and generated docs.
8//! [`Registry::type_desc_name`](crate::Registry::type_desc_name) prints one.
9
10use facet::Shape;
11
12/// What a parameter or return type is, for display.
13#[derive(Clone, Debug, PartialEq, Eq)]
14pub enum TypeDesc {
15    /// A `Facet` type, named through the registry (registered types by their
16    /// xur name, `String` as `str`, generics kept).
17    Shape(&'static Shape),
18    /// A xur type with no shape: `str`, `bytes`, `list`, `ptr`, `any`.
19    Name(&'static str),
20    /// A generic xur type such as `Option<T>` or `Result<T, str>`.
21    Generic(&'static str, Vec<TypeDesc>),
22    /// One of several types, as `A | B` (a [`OneOf`](super::OneOf) argument).
23    Union(Vec<TypeDesc>),
24    /// A named union (a [`one_of!`](crate::one_of) enum): shown by its name
25    /// when it has more than a few options, so long unions stay readable.
26    Alias(&'static str, Vec<TypeDesc>),
27    /// A tuple, `(A, B)`.
28    Tuple(Vec<TypeDesc>),
29    /// Not known.
30    Unknown,
31}
32
33impl TypeDesc {
34    /// The description of a type with an optional shape: the shape when
35    /// there is one, otherwise [`TypeDesc::Unknown`].
36    pub fn of_shape(shape: Option<&'static Shape>) -> Self {
37        shape.map_or(Self::Unknown, Self::Shape)
38    }
39
40    /// `str`, which most string arguments are taken as ([`XurStr`](super::XurStr)).
41    pub const STR: Self = Self::Name("str");
42    /// Any value.
43    pub const ANY: Self = Self::Name("any");
44}