use alloc::borrow::Cow;
use alloc::vec::Vec;
use crate::expression::{Expression, Literal, VariableExpression, VariableRef};
use crate::pattern::Pattern;
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub enum Message<'a> {
Pattern(PatternMessage<'a>),
Select(SelectMessage<'a>),
}
impl<'a> Message<'a> {
pub fn declarations(&self) -> &[Declaration<'a>] {
match self {
Message::Pattern(m) => &m.declarations,
Message::Select(m) => &m.declarations,
}
}
pub fn into_owned(self) -> Message<'static> {
match self {
Message::Pattern(m) => Message::Pattern(m.into_owned()),
Message::Select(m) => Message::Select(m.into_owned()),
}
}
}
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct PatternMessage<'a> {
pub declarations: Vec<Declaration<'a>>,
pub pattern: Pattern<'a>,
}
impl PatternMessage<'_> {
pub fn into_owned(self) -> PatternMessage<'static> {
PatternMessage {
declarations: self
.declarations
.into_iter()
.map(Declaration::into_owned)
.collect(),
pattern: self.pattern.into_owned(),
}
}
}
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct SelectMessage<'a> {
pub declarations: Vec<Declaration<'a>>,
pub selectors: Vec<VariableRef<'a>>,
pub variants: Vec<Variant<'a>>,
}
impl SelectMessage<'_> {
pub fn into_owned(self) -> SelectMessage<'static> {
SelectMessage {
declarations: self
.declarations
.into_iter()
.map(Declaration::into_owned)
.collect(),
selectors: self
.selectors
.into_iter()
.map(VariableRef::into_owned)
.collect(),
variants: self.variants.into_iter().map(Variant::into_owned).collect(),
}
}
}
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub enum Declaration<'a> {
Input(InputDeclaration<'a>),
Local(LocalDeclaration<'a>),
}
impl Declaration<'_> {
pub fn name(&self) -> &str {
match self {
Declaration::Input(d) => &d.name,
Declaration::Local(d) => &d.name,
}
}
pub fn into_owned(self) -> Declaration<'static> {
match self {
Declaration::Input(d) => Declaration::Input(d.into_owned()),
Declaration::Local(d) => Declaration::Local(d.into_owned()),
}
}
}
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct InputDeclaration<'a> {
pub name: Cow<'a, str>,
pub value: VariableExpression<'a>,
}
impl InputDeclaration<'_> {
pub fn into_owned(self) -> InputDeclaration<'static> {
InputDeclaration {
name: Cow::Owned(self.name.into_owned()),
value: self.value.into_owned(),
}
}
}
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct LocalDeclaration<'a> {
pub name: Cow<'a, str>,
pub value: Expression<'a>,
}
impl LocalDeclaration<'_> {
pub fn into_owned(self) -> LocalDeclaration<'static> {
LocalDeclaration {
name: Cow::Owned(self.name.into_owned()),
value: self.value.into_owned(),
}
}
}
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct Variant<'a> {
pub keys: Vec<Key<'a>>,
pub value: Pattern<'a>,
}
impl Variant<'_> {
pub fn into_owned(self) -> Variant<'static> {
Variant {
keys: self.keys.into_iter().map(Key::into_owned).collect(),
value: self.value.into_owned(),
}
}
}
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub enum Key<'a> {
Literal(Literal<'a>),
CatchAll(CatchAllKey<'a>),
}
impl Key<'_> {
pub fn into_owned(self) -> Key<'static> {
match self {
Key::Literal(l) => Key::Literal(l.into_owned()),
Key::CatchAll(c) => Key::CatchAll(c.into_owned()),
}
}
}
#[derive(Clone, Default, PartialEq, Eq, Hash, Debug)]
pub struct CatchAllKey<'a> {
pub value: Option<Cow<'a, str>>,
}
impl CatchAllKey<'_> {
pub fn into_owned(self) -> CatchAllKey<'static> {
CatchAllKey {
value: self.value.map(|v| Cow::Owned(v.into_owned())),
}
}
}