jannie

jannie (mirror)
Log | Files | Refs | README | LICENSE

node.rs (1518B)


      1 use std::path::{Path, PathBuf};
      2 
      3 use indexmap::IndexSet;
      4 use uuid::Uuid;
      5 
      6 pub struct Node<M> {
      7     id: Uuid,
      8     path: PathBuf,
      9     meta: M,
     10     children: IndexSet<Uuid>,
     11     parent: Option<Uuid>,
     12 }
     13 
     14 impl<M> Node<M> {
     15     pub fn new(path: PathBuf, meta: M) -> Self {
     16         Self {
     17             id: Uuid::new_v4(),
     18             path,
     19             meta,
     20             children: IndexSet::new(),
     21             parent: None,
     22         }
     23     }
     24 
     25     pub fn id(&self) -> Uuid {
     26         self.id
     27     }
     28 
     29     pub fn path(&self) -> &Path {
     30         &self.path
     31     }
     32 
     33     pub fn meta(&self) -> &M {
     34         &self.meta
     35     }
     36 
     37     pub fn meta_mut(&mut self) -> &mut M {
     38         &mut self.meta
     39     }
     40 
     41     pub fn set_meta(&mut self, meta: M) {
     42         self.meta = meta;
     43     }
     44 
     45     /// A node is a leaf if it has no children
     46     pub fn is_leaf(&self) -> bool {
     47         self.children.is_empty()
     48     }
     49 
     50     pub fn parent(&self) -> Option<Uuid> {
     51         self.parent
     52     }
     53 
     54     pub fn children(&self) -> impl Iterator<Item = Uuid> {
     55         self.children.iter().copied()
     56     }
     57 
     58     pub fn children_vec(&self) -> Vec<Uuid> {
     59         self.children.iter().copied().collect()
     60     }
     61 
     62     pub fn add_child(&mut self, node_id: Uuid) {
     63         self.children.insert(node_id);
     64     }
     65 
     66     pub fn set_parent_opt(&mut self, parent_id: Option<Uuid>) {
     67         self.parent = parent_id;
     68     }
     69     pub fn set_parent(&mut self, parent_id: Uuid) {
     70         self.parent = Some(parent_id);
     71     }
     72 
     73     pub fn unset_parent(&mut self) {
     74         self.parent = None;
     75     }
     76 }