Skip to main content

flow_types/
builtin.rs

1//! The built-in encoding forms: the [`Type`]s that are not a catalog
2//! declaration but stand for a std type the encoding knows by kind.
3
4use crate::types::{DeclId, Type};
5
6/// A built-in encoding form, the head of a [`Type`] that names no catalog
7/// declaration.
8#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
9pub enum Builtin {
10    /// `Int`.
11    Int,
12    /// `Decimal`.
13    Decimal,
14    /// `Bool`.
15    Bool,
16    /// `Text`.
17    Text,
18    /// `Bytes`.
19    Bytes,
20    /// `Duration`.
21    Duration,
22    /// `Instant`.
23    Instant,
24    /// `Unit`.
25    Unit,
26    /// `Never`.
27    Never,
28    /// `json.Value`.
29    Json,
30    /// `List<T>`.
31    List,
32    /// `Set<T>`.
33    Set,
34    /// `Dict<K, V>`.
35    Dict,
36    /// `Option<T>`.
37    Option,
38    /// `task.Task<T>`.
39    Task,
40}
41
42/// A [`Type`] by its head: a built-in form with its arguments, or one of the
43/// heads a catalog declaration, a tuple, a function, a parameter or a type
44/// that is not data. Every type has exactly one, so a reader of types
45/// matches a head once and names no built-in form it does not mean.
46#[derive(Clone, Debug)]
47pub enum Head<'t> {
48    /// A built-in form, applied to its arguments.
49    Builtin(Builtin, Vec<&'t Type>),
50    /// A declared type, applied to its descriptive arguments.
51    Named(DeclId, &'t [Type]),
52    /// A tuple of its parts.
53    Tuple(&'t [Type]),
54    /// A function.
55    Function {
56        /// Its parameter types, in order.
57        params: &'t [Type],
58        /// Its result type.
59        result: &'t Type,
60    },
61    /// The `n`th type parameter.
62    Param(u32),
63    /// A type with no value encoding.
64    NotData,
65}
66
67impl Type {
68    /// This type's head.
69    pub fn head(&self) -> Head<'_> {
70        let builtin = |form, args| Head::Builtin(form, args);
71        match self {
72            Type::Int => builtin(Builtin::Int, Vec::new()),
73            Type::Decimal => builtin(Builtin::Decimal, Vec::new()),
74            Type::Bool => builtin(Builtin::Bool, Vec::new()),
75            Type::Text => builtin(Builtin::Text, Vec::new()),
76            Type::Bytes => builtin(Builtin::Bytes, Vec::new()),
77            Type::Duration => builtin(Builtin::Duration, Vec::new()),
78            Type::Instant => builtin(Builtin::Instant, Vec::new()),
79            Type::Unit => builtin(Builtin::Unit, Vec::new()),
80            Type::Never => builtin(Builtin::Never, Vec::new()),
81            Type::Json => builtin(Builtin::Json, Vec::new()),
82            Type::List(item) => builtin(Builtin::List, vec![&**item]),
83            Type::Set(item) => builtin(Builtin::Set, vec![&**item]),
84            Type::Dict(key, value) => builtin(Builtin::Dict, vec![&**key, &**value]),
85            Type::Option(item) => builtin(Builtin::Option, vec![&**item]),
86            Type::Task(item) => builtin(Builtin::Task, vec![&**item]),
87            Type::Named(decl, args) => Head::Named(*decl, args),
88            Type::Tuple(parts) => Head::Tuple(parts),
89            Type::Function { params, result } => Head::Function { params, result },
90            Type::Param(index) => Head::Param(*index),
91            Type::NotData => Head::NotData,
92        }
93    }
94}
95
96impl Builtin {
97    /// The type of this form applied to arguments, the `n`th of which is
98    /// `arg(n)`: the inverse of [`Type::head`].
99    pub fn apply(self, mut arg: impl FnMut(usize) -> Type) -> Type {
100        match self {
101            Builtin::Int => Type::Int,
102            Builtin::Decimal => Type::Decimal,
103            Builtin::Bool => Type::Bool,
104            Builtin::Text => Type::Text,
105            Builtin::Bytes => Type::Bytes,
106            Builtin::Duration => Type::Duration,
107            Builtin::Instant => Type::Instant,
108            Builtin::Unit => Type::Unit,
109            Builtin::Never => Type::Never,
110            Builtin::Json => Type::Json,
111            Builtin::List => Type::list(arg(0)),
112            Builtin::Set => Type::set(arg(0)),
113            Builtin::Dict => Type::dict(arg(0), arg(1)),
114            Builtin::Option => Type::option(arg(0)),
115            Builtin::Task => Type::Task(Box::new(arg(0))),
116        }
117    }
118}