use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum VrtNodeType {
Category,
Subcategory,
Variant,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct VrtNode {
pub id: String,
pub name: String,
#[serde(rename = "type")]
pub node_type: VrtNodeType,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub children: Vec<VrtNode>,
#[serde(skip_serializing_if = "Option::is_none")]
pub priority: Option<u8>,
}
impl VrtNode {
pub fn is_category(&self) -> bool {
self.node_type == VrtNodeType::Category
}
pub fn is_subcategory(&self) -> bool {
self.node_type == VrtNodeType::Subcategory
}
pub fn is_variant(&self) -> bool {
self.node_type == VrtNodeType::Variant
}
pub fn has_children(&self) -> bool {
!self.children.is_empty()
}
pub fn find_by_id(&self, id: &str) -> Option<&VrtNode> {
if self.id == id {
return Some(self);
}
for child in &self.children {
if let Some(found) = child.find_by_id(id) {
return Some(found);
}
}
None
}
pub fn variants(&self) -> Vec<&VrtNode> {
let mut variants = Vec::new();
if self.is_variant() {
variants.push(self);
}
for child in &self.children {
variants.extend(child.variants());
}
variants
}
}
pub type VrtTaxonomy = Vec<VrtNode>;