use std::sync::Arc;
#[derive(Debug, PartialEq, Eq)]
pub struct RecordShape {
name: Arc<str>,
fields: Box<[Arc<str>]>,
}
impl RecordShape {
pub fn new<'a>(name: &str, fields: impl IntoIterator<Item = &'a str>) -> Arc<Self> {
Arc::new(RecordShape {
name: Arc::from(name),
fields: fields.into_iter().map(Arc::from).collect(),
})
}
pub fn name(&self) -> &str {
&self.name
}
pub fn fields(&self) -> impl ExactSizeIterator<Item = &str> {
self.fields.iter().map(|field| &**field)
}
pub fn field(&self, index: usize) -> Option<&str> {
self.fields.get(index).map(|field| &**field)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Payload {
None,
Positional(usize),
Named(Box<[Arc<str>]>),
}
impl Payload {
pub fn arity(&self) -> usize {
match self {
Payload::None => 0,
Payload::Positional(count) => *count,
Payload::Named(fields) => fields.len(),
}
}
pub fn named<'a>(fields: impl IntoIterator<Item = &'a str>) -> Self {
Payload::Named(fields.into_iter().map(Arc::from).collect())
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Constructor {
name: Arc<str>,
payload: Payload,
}
impl Constructor {
pub fn new(name: &str, payload: Payload) -> Self {
Constructor {
name: Arc::from(name),
payload,
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn payload(&self) -> &Payload {
&self.payload
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct EnumShape {
name: Arc<str>,
constructors: Box<[Constructor]>,
}
impl EnumShape {
pub fn new(name: &str, constructors: impl IntoIterator<Item = Constructor>) -> Arc<Self> {
Arc::new(EnumShape {
name: Arc::from(name),
constructors: constructors.into_iter().collect(),
})
}
pub fn name(&self) -> &str {
&self.name
}
pub fn constructor(&self, index: usize) -> Option<&Constructor> {
self.constructors.get(index)
}
pub fn constructors(&self) -> &[Constructor] {
&self.constructors
}
}