Skip to main content

sim_list_cell/
citizen.rs

1//! Citizen descriptor for the cons-cell list class, mapping list items to and
2//! from their serialized expression form via [`cons_list_class_symbol`] and
3//! [`ConsListDescriptor`].
4
5use sim_citizen_derive::Citizen;
6use sim_kernel::{Error, Expr, Result, Symbol};
7
8/// Serialized citizen form of a [`crate::ConsList`]: the list's items as a
9/// vector of [`Expr`], encoded and decoded under the `list/ConsList` class.
10#[derive(Clone, Debug, Default, PartialEq, Citizen)]
11#[citizen(symbol = "list/ConsList", version = 0)]
12pub struct ConsListDescriptor {
13    /// The list elements in order, each as a serialized expression.
14    #[citizen(with = "crate::citizen::expr_items")]
15    pub items: Vec<Expr>,
16}
17
18/// The class symbol for the cons-cell list class, `list/ConsList`.
19pub fn cons_list_class_symbol() -> Symbol {
20    Symbol::qualified("list", "ConsList")
21}
22
23pub(crate) mod expr_items {
24    use super::*;
25
26    pub fn encode(items: &[Expr]) -> Expr {
27        Expr::List(items.to_vec())
28    }
29
30    pub fn decode(expr: &Expr) -> Result<Vec<Expr>> {
31        let Expr::List(items) = expr else {
32            return Err(Error::Eval(
33                "list citizen field items: expected list".to_owned(),
34            ));
35        };
36        Ok(items.clone())
37    }
38}