use std::sync::Arc;
use crate::value::{Meta, Value};
#[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)
}
}
#[derive(Clone, Debug)]
pub struct Record {
shape: Arc<RecordShape>,
fields: Arc<[Value]>,
meta: Meta,
}
impl Record {
pub fn new(shape: Arc<RecordShape>, fields: Vec<Value>) -> Option<Self> {
(fields.len() == shape.fields.len()).then(|| Record {
meta: Meta::around(&fields),
shape,
fields: fields.into(),
})
}
pub fn shape(&self) -> &Arc<RecordShape> {
&self.shape
}
pub fn fields(&self) -> &[Value] {
&self.fields
}
pub(crate) fn meta(&self) -> Meta {
self.meta
}
pub(crate) fn fields_mut(&mut self) -> Option<&mut [Value]> {
Arc::get_mut(&mut self.fields)
}
pub fn field(&self, index: usize) -> Option<&Value> {
self.fields.get(index)
}
}
impl Drop for Record {
fn drop(&mut self) {
if let Some(fields) = self.fields_mut() {
crate::value::dismantle(fields.iter_mut());
}
}
}
#[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)
}
}
#[derive(Clone, Debug)]
pub struct Variant {
shape: Arc<EnumShape>,
index: u32,
meta: Meta,
payload: Arc<[Value]>,
}
impl Variant {
pub fn new(shape: Arc<EnumShape>, index: usize, payload: Vec<Value>) -> Option<Self> {
let arity = shape.constructor(index)?.payload.arity();
let index = u32::try_from(index).ok()?;
(payload.len() == arity).then(|| Variant {
meta: Meta::around(&payload),
shape,
index,
payload: payload.into(),
})
}
pub fn shape(&self) -> &Arc<EnumShape> {
&self.shape
}
pub fn index(&self) -> usize {
self.index as usize
}
pub fn constructor(&self) -> &Constructor {
&self.shape.constructors[self.index()]
}
pub fn payload(&self) -> &[Value] {
&self.payload
}
pub(crate) fn meta(&self) -> Meta {
self.meta
}
pub(crate) fn payload_mut(&mut self) -> Option<&mut [Value]> {
Arc::get_mut(&mut self.payload)
}
}
impl Drop for Variant {
fn drop(&mut self) {
if let Some(payload) = self.payload_mut() {
crate::value::dismantle(payload.iter_mut());
}
}
}