xur-runtime 0.1.7

Values, native-function registry and facet conversions for the Xur language
Documentation
//! Printable descriptions of the types a native function takes and returns.
//!
//! A [`Signature`](crate::registry::Signature)'s shapes are what argument
//! marshaling checks against; types with no `Facet` shape of their own
//! ([`XurStr`](super::XurStr), [`Value`](crate::Value), [`Opt`](super::Opt),
//! ...) have none to give. A [`TypeDesc`] describes every parameter and the
//! return type for people instead: hovers, inlay hints and generated docs.
//! [`Registry::type_desc_name`](crate::Registry::type_desc_name) prints one.

use facet::Shape;

/// What a parameter or return type is, for display.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum TypeDesc {
    /// A `Facet` type, named through the registry (registered types by their
    /// xur name, `String` as `str`, generics kept).
    Shape(&'static Shape),
    /// A xur type with no shape: `str`, `bytes`, `list`, `ptr`, `any`.
    Name(&'static str),
    /// A generic xur type such as `Option<T>` or `Result<T, str>`.
    Generic(&'static str, Vec<TypeDesc>),
    /// One of several types, as `A | B` (a [`OneOf`](super::OneOf) argument).
    Union(Vec<TypeDesc>),
    /// Not known.
    Unknown,
}

impl TypeDesc {
    /// The description of a type with an optional shape: the shape when
    /// there is one, otherwise [`TypeDesc::Unknown`].
    pub fn of_shape(shape: Option<&'static Shape>) -> Self {
        shape.map_or(Self::Unknown, Self::Shape)
    }

    /// `str`, which most string arguments are taken as ([`XurStr`](super::XurStr)).
    pub const STR: Self = Self::Name("str");
    /// Any value.
    pub const ANY: Self = Self::Name("any");
}