1use std::sync::Arc;
8
9#[derive(Debug, PartialEq, Eq)]
11pub struct RecordShape {
12 name: Arc<str>,
13 fields: Box<[Arc<str>]>,
14}
15
16impl RecordShape {
17 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 pub fn name(&self) -> &str {
27 &self.name
28 }
29
30 pub fn fields(&self) -> impl ExactSizeIterator<Item = &str> {
32 self.fields.iter().map(|field| &**field)
33 }
34
35 pub fn field(&self, index: usize) -> Option<&str> {
38 self.fields.get(index).map(|field| &**field)
39 }
40}
41
42#[derive(Clone, Debug, PartialEq, Eq)]
44pub enum Payload {
45 None,
47 Positional(usize),
49 Named(Box<[Arc<str>]>),
51}
52
53impl Payload {
54 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 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#[derive(Clone, Debug, PartialEq, Eq)]
71pub struct Constructor {
72 name: Arc<str>,
73 payload: Payload,
74}
75
76impl Constructor {
77 pub fn new(name: &str, payload: Payload) -> Self {
79 Constructor {
80 name: Arc::from(name),
81 payload,
82 }
83 }
84
85 pub fn name(&self) -> &str {
87 &self.name
88 }
89
90 pub fn payload(&self) -> &Payload {
92 &self.payload
93 }
94}
95
96#[derive(Debug, PartialEq, Eq)]
98pub struct EnumShape {
99 name: Arc<str>,
100 constructors: Box<[Constructor]>,
101}
102
103impl EnumShape {
104 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 pub fn name(&self) -> &str {
115 &self.name
116 }
117
118 pub fn constructor(&self, index: usize) -> Option<&Constructor> {
120 self.constructors.get(index)
121 }
122
123 pub fn constructors(&self) -> &[Constructor] {
125 &self.constructors
126 }
127}