flow-types 0.1.0-rc.5

The Flow declaration model with no values: types, declarations and their identities, record and enum shapes, and which types decoding may build.
Documentation
//! The built-in encoding forms: the [`Type`]s that are not a catalog
//! declaration but stand for a std type the encoding knows by kind.

use crate::types::{DeclId, Type};

/// A built-in encoding form, the head of a [`Type`] that names no catalog
/// declaration.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum Builtin {
    /// `Int`.
    Int,
    /// `Decimal`.
    Decimal,
    /// `Bool`.
    Bool,
    /// `Text`.
    Text,
    /// `Bytes`.
    Bytes,
    /// `Duration`.
    Duration,
    /// `Instant`.
    Instant,
    /// `Unit`.
    Unit,
    /// `Never`.
    Never,
    /// `json.Value`.
    Json,
    /// `List<T>`.
    List,
    /// `Set<T>`.
    Set,
    /// `Dict<K, V>`.
    Dict,
    /// `Option<T>`.
    Option,
    /// `task.Task<T>`.
    Task,
}

/// A [`Type`] by its head: a built-in form with its arguments, or one of the
/// heads a catalog declaration, a tuple, a function, a parameter or a type
/// that is not data. Every type has exactly one, so a reader of types
/// matches a head once and names no built-in form it does not mean.
#[derive(Clone, Debug)]
pub enum Head<'t> {
    /// A built-in form, applied to its arguments.
    Builtin(Builtin, Vec<&'t Type>),
    /// A declared type, applied to its descriptive arguments.
    Named(DeclId, &'t [Type]),
    /// A tuple of its parts.
    Tuple(&'t [Type]),
    /// A function.
    Function {
        /// Its parameter types, in order.
        params: &'t [Type],
        /// Its result type.
        result: &'t Type,
    },
    /// The `n`th type parameter.
    Param(u32),
    /// A type with no value encoding.
    NotData,
}

impl Type {
    /// This type's head.
    pub fn head(&self) -> Head<'_> {
        let builtin = |form, args| Head::Builtin(form, args);
        match self {
            Type::Int => builtin(Builtin::Int, Vec::new()),
            Type::Decimal => builtin(Builtin::Decimal, Vec::new()),
            Type::Bool => builtin(Builtin::Bool, Vec::new()),
            Type::Text => builtin(Builtin::Text, Vec::new()),
            Type::Bytes => builtin(Builtin::Bytes, Vec::new()),
            Type::Duration => builtin(Builtin::Duration, Vec::new()),
            Type::Instant => builtin(Builtin::Instant, Vec::new()),
            Type::Unit => builtin(Builtin::Unit, Vec::new()),
            Type::Never => builtin(Builtin::Never, Vec::new()),
            Type::Json => builtin(Builtin::Json, Vec::new()),
            Type::List(item) => builtin(Builtin::List, vec![&**item]),
            Type::Set(item) => builtin(Builtin::Set, vec![&**item]),
            Type::Dict(key, value) => builtin(Builtin::Dict, vec![&**key, &**value]),
            Type::Option(item) => builtin(Builtin::Option, vec![&**item]),
            Type::Task(item) => builtin(Builtin::Task, vec![&**item]),
            Type::Named(decl, args) => Head::Named(*decl, args),
            Type::Tuple(parts) => Head::Tuple(parts),
            Type::Function { params, result } => Head::Function { params, result },
            Type::Param(index) => Head::Param(*index),
            Type::NotData => Head::NotData,
        }
    }
}

impl Builtin {
    /// The type of this form applied to arguments, the `n`th of which is
    /// `arg(n)`: the inverse of [`Type::head`].
    pub fn apply(self, mut arg: impl FnMut(usize) -> Type) -> Type {
        match self {
            Builtin::Int => Type::Int,
            Builtin::Decimal => Type::Decimal,
            Builtin::Bool => Type::Bool,
            Builtin::Text => Type::Text,
            Builtin::Bytes => Type::Bytes,
            Builtin::Duration => Type::Duration,
            Builtin::Instant => Type::Instant,
            Builtin::Unit => Type::Unit,
            Builtin::Never => Type::Never,
            Builtin::Json => Type::Json,
            Builtin::List => Type::list(arg(0)),
            Builtin::Set => Type::set(arg(0)),
            Builtin::Dict => Type::dict(arg(0), arg(1)),
            Builtin::Option => Type::option(arg(0)),
            Builtin::Task => Type::Task(Box::new(arg(0))),
        }
    }
}