1use alloc::borrow::Cow;
5use alloc::vec::Vec;
6
7use crate::expression::{Expression, Literal, VariableExpression, VariableRef};
8use crate::pattern::Pattern;
9
10#[derive(Clone, PartialEq, Eq, Hash, Debug)]
12#[non_exhaustive]
15pub enum Message<'a> {
16 Pattern(PatternMessage<'a>),
18 Select(SelectMessage<'a>),
20}
21
22impl<'a> Message<'a> {
23 pub fn declarations(&self) -> &[Declaration<'a>] {
25 match self {
26 Message::Pattern(m) => &m.declarations,
27 Message::Select(m) => &m.declarations,
28 }
29 }
30
31 pub fn into_owned(self) -> Message<'static> {
33 match self {
34 Message::Pattern(m) => Message::Pattern(m.into_owned()),
35 Message::Select(m) => Message::Select(m.into_owned()),
36 }
37 }
38}
39
40#[derive(Clone, PartialEq, Eq, Hash, Debug)]
42pub struct PatternMessage<'a> {
43 pub declarations: Vec<Declaration<'a>>,
45 pub pattern: Pattern<'a>,
47}
48
49impl PatternMessage<'_> {
50 pub fn into_owned(self) -> PatternMessage<'static> {
52 PatternMessage {
53 declarations: self
54 .declarations
55 .into_iter()
56 .map(Declaration::into_owned)
57 .collect(),
58 pattern: self.pattern.into_owned(),
59 }
60 }
61}
62
63#[derive(Clone, PartialEq, Eq, Hash, Debug)]
65pub struct SelectMessage<'a> {
66 pub declarations: Vec<Declaration<'a>>,
68 pub selectors: Vec<VariableRef<'a>>,
70 pub variants: Vec<Variant<'a>>,
72}
73
74impl SelectMessage<'_> {
75 pub fn into_owned(self) -> SelectMessage<'static> {
77 SelectMessage {
78 declarations: self
79 .declarations
80 .into_iter()
81 .map(Declaration::into_owned)
82 .collect(),
83 selectors: self
84 .selectors
85 .into_iter()
86 .map(VariableRef::into_owned)
87 .collect(),
88 variants: self.variants.into_iter().map(Variant::into_owned).collect(),
89 }
90 }
91}
92
93#[derive(Clone, PartialEq, Eq, Hash, Debug)]
95#[non_exhaustive]
98pub enum Declaration<'a> {
99 Input(InputDeclaration<'a>),
101 Local(LocalDeclaration<'a>),
103}
104
105impl Declaration<'_> {
106 pub fn name(&self) -> &str {
108 match self {
109 Declaration::Input(d) => &d.name,
110 Declaration::Local(d) => &d.name,
111 }
112 }
113
114 pub fn into_owned(self) -> Declaration<'static> {
116 match self {
117 Declaration::Input(d) => Declaration::Input(d.into_owned()),
118 Declaration::Local(d) => Declaration::Local(d.into_owned()),
119 }
120 }
121}
122
123#[derive(Clone, PartialEq, Eq, Hash, Debug)]
126pub struct InputDeclaration<'a> {
127 pub name: Cow<'a, str>,
129 pub value: VariableExpression<'a>,
131}
132
133impl InputDeclaration<'_> {
134 pub fn into_owned(self) -> InputDeclaration<'static> {
136 InputDeclaration {
137 name: Cow::Owned(self.name.into_owned()),
138 value: self.value.into_owned(),
139 }
140 }
141}
142
143#[derive(Clone, PartialEq, Eq, Hash, Debug)]
145pub struct LocalDeclaration<'a> {
146 pub name: Cow<'a, str>,
148 pub value: Expression<'a>,
150}
151
152impl LocalDeclaration<'_> {
153 pub fn into_owned(self) -> LocalDeclaration<'static> {
155 LocalDeclaration {
156 name: Cow::Owned(self.name.into_owned()),
157 value: self.value.into_owned(),
158 }
159 }
160}
161
162#[derive(Clone, PartialEq, Eq, Hash, Debug)]
164pub struct Variant<'a> {
165 pub keys: Vec<Key<'a>>,
167 pub value: Pattern<'a>,
169}
170
171impl Variant<'_> {
172 pub fn into_owned(self) -> Variant<'static> {
174 Variant {
175 keys: self.keys.into_iter().map(Key::into_owned).collect(),
176 value: self.value.into_owned(),
177 }
178 }
179}
180
181#[derive(Clone, PartialEq, Eq, Hash, Debug)]
183#[non_exhaustive]
186pub enum Key<'a> {
187 Literal(Literal<'a>),
189 CatchAll(CatchAllKey<'a>),
191}
192
193impl Key<'_> {
194 pub fn into_owned(self) -> Key<'static> {
196 match self {
197 Key::Literal(l) => Key::Literal(l.into_owned()),
198 Key::CatchAll(c) => Key::CatchAll(c.into_owned()),
199 }
200 }
201}
202
203#[derive(Clone, Default, PartialEq, Eq, Hash, Debug)]
206pub struct CatchAllKey<'a> {
207 pub value: Option<Cow<'a, str>>,
209}
210
211impl CatchAllKey<'_> {
212 pub fn into_owned(self) -> CatchAllKey<'static> {
214 CatchAllKey {
215 value: self.value.map(|v| Cow::Owned(v.into_owned())),
216 }
217 }
218}