Skip to main content

flow_types/
shape.rs

1//! Record and enum declarations (`types.md`, *Records* and *Enums*): the
2//! shared description a record or enum value holds beside its slots. It is
3//! what the text form reads, the type's name, its constructors' names and its
4//! fields' names, while the slots are what equality and order read. Two values
5//! of one type share one description, so holding it costs a pointer.
6
7use std::sync::Arc;
8
9/// A record declaration: its name and its fields in declaration order.
10#[derive(Debug, PartialEq, Eq)]
11pub struct RecordShape {
12    name: Arc<str>,
13    fields: Box<[Arc<str>]>,
14}
15
16impl RecordShape {
17    /// Returns the shape of the record `name` declaring `fields`, in order.
18    pub fn new<'a>(name: &str, fields: impl IntoIterator<Item = &'a str>) -> Arc<Self> {
19        Arc::new(RecordShape {
20            name: Arc::from(name),
21            fields: fields.into_iter().map(Arc::from).collect(),
22        })
23    }
24
25    /// Returns the type's declared name.
26    pub fn name(&self) -> &str {
27        &self.name
28    }
29
30    /// Returns the field names, in declaration order.
31    pub fn fields(&self) -> impl ExactSizeIterator<Item = &str> {
32        self.fields.iter().map(|field| &**field)
33    }
34
35    /// Returns the name of the field at `index`, counted from 0 in
36    /// declaration order.
37    pub fn field(&self, index: usize) -> Option<&str> {
38        self.fields.get(index).map(|field| &**field)
39    }
40}
41
42/// What a constructor carries.
43#[derive(Clone, Debug, PartialEq, Eq)]
44pub enum Payload {
45    /// Nothing: `TimedOut`.
46    None,
47    /// This many positional values: `Failed(problem)`.
48    Positional(usize),
49    /// Named fields, in declaration order: `StepFailed { step, last }`.
50    Named(Box<[Arc<str>]>),
51}
52
53impl Payload {
54    /// Returns how many values the payload holds.
55    pub fn arity(&self) -> usize {
56        match self {
57            Payload::None => 0,
58            Payload::Positional(count) => *count,
59            Payload::Named(fields) => fields.len(),
60        }
61    }
62
63    /// Returns named fields, in declaration order.
64    pub fn named<'a>(fields: impl IntoIterator<Item = &'a str>) -> Self {
65        Payload::Named(fields.into_iter().map(Arc::from).collect())
66    }
67}
68
69/// One constructor of an enum declaration.
70#[derive(Clone, Debug, PartialEq, Eq)]
71pub struct Constructor {
72    name: Arc<str>,
73    payload: Payload,
74}
75
76impl Constructor {
77    /// Returns the constructor `name` carrying `payload`.
78    pub fn new(name: &str, payload: Payload) -> Self {
79        Constructor {
80            name: Arc::from(name),
81            payload,
82        }
83    }
84
85    /// Returns the constructor's name.
86    pub fn name(&self) -> &str {
87        &self.name
88    }
89
90    /// Returns what it carries.
91    pub fn payload(&self) -> &Payload {
92        &self.payload
93    }
94}
95
96/// An enum declaration: its name and its constructors in declaration order.
97#[derive(Debug, PartialEq, Eq)]
98pub struct EnumShape {
99    name: Arc<str>,
100    constructors: Box<[Constructor]>,
101}
102
103impl EnumShape {
104    /// Returns the shape of the enum `name` declaring `constructors`, in
105    /// order.
106    pub fn new(name: &str, constructors: impl IntoIterator<Item = Constructor>) -> Arc<Self> {
107        Arc::new(EnumShape {
108            name: Arc::from(name),
109            constructors: constructors.into_iter().collect(),
110        })
111    }
112
113    /// Returns the type's declared name.
114    pub fn name(&self) -> &str {
115        &self.name
116    }
117
118    /// Returns the constructor at a declaration index.
119    pub fn constructor(&self, index: usize) -> Option<&Constructor> {
120        self.constructors.get(index)
121    }
122
123    /// Returns the constructors, in declaration order.
124    pub fn constructors(&self) -> &[Constructor] {
125        &self.constructors
126    }
127}