icydb-schema 0.66.5

IcyDB — A type-safe, embedded ORM and schema system for the Internet Computer
Documentation
use crate::prelude::*;

///
/// FieldList
///

#[derive(Clone, Debug, Serialize)]
pub struct FieldList {
    fields: &'static [Field],
}

impl FieldList {
    #[must_use]
    pub const fn new(fields: &'static [Field]) -> Self {
        Self { fields }
    }

    #[must_use]
    pub const fn fields(&self) -> &'static [Field] {
        self.fields
    }

    // get
    #[must_use]
    pub fn get(&self, ident: &str) -> Option<&Field> {
        self.fields.iter().find(|field| field.ident() == ident)
    }
}

impl ValidateNode for FieldList {}

impl VisitableNode for FieldList {
    fn drive<V: Visitor>(&self, v: &mut V) {
        for node in self.fields() {
            node.accept(v);
        }
    }
}

///
/// Field
///

#[derive(Clone, Debug, Serialize)]
pub struct Field {
    ident: &'static str,
    value: Value,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    default: Option<Arg>,
}

impl Field {
    #[must_use]
    pub const fn new(ident: &'static str, value: Value, default: Option<Arg>) -> Self {
        Self {
            ident,
            value,
            default,
        }
    }

    #[must_use]
    pub const fn ident(&self) -> &'static str {
        self.ident
    }

    #[must_use]
    pub const fn value(&self) -> &Value {
        &self.value
    }

    #[must_use]
    pub const fn default(&self) -> Option<&Arg> {
        self.default.as_ref()
    }
}

impl ValidateNode for Field {
    fn validate(&self) -> Result<(), ErrorTree> {
        Ok(())
    }
}

impl VisitableNode for Field {
    fn route_key(&self) -> String {
        self.ident().to_string()
    }

    fn drive<V: Visitor>(&self, v: &mut V) {
        self.value().accept(v);
        if let Some(node) = self.default() {
            node.accept(v);
        }
    }
}