icydb-schema 0.202.0

IcyDB — A schema-first typed query engine and persistence runtime for Internet Computer canisters
Documentation
use crate::prelude::*;

///
/// Enum
///

#[derive(Clone, Debug, Serialize)]
pub struct Enum {
    def: Def,
    variants: &'static [EnumVariant],
    ty: Type,
}

impl Enum {
    #[must_use]
    pub const fn new(def: Def, variants: &'static [EnumVariant], ty: Type) -> Self {
        Self { def, variants, ty }
    }

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

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

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

impl MacroNode for Enum {
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

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

impl VisitableNode for Enum {
    fn route_key(&self) -> String {
        self.def().path()
    }

    fn drive<V: Visitor>(&self, v: &mut V) {
        self.def().accept(v);
        for node in self.variants() {
            node.accept(v);
        }
        self.ty().accept(v);
    }
}

///
/// EnumVariant
///

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

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

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

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

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

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

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