use crate::store::{ object::{Obj, ObjMetadata, ObjType}, structs::{NsObjPath, ObjPath}, }; use std::path::PathBuf; use std::sync::OnceLock; type NsObjectChilds = Vec>; pub struct NsObject { pub obj_type: ObjType, /// path of the obj in the repo obj_path: OnceLock, /// path of the nsobj file in the store nsobj_path: OnceLock, childs: OnceLock, } impl NsObject { pub fn from_local_path(path: &ObjPath) -> Self { NsObject { obj_type: ObjType::Obj, obj_path: OnceLock::from(path.clone()), nsobj_path: OnceLock::new(), childs: OnceLock::new(), } } pub fn from_hash(hash: &str) -> Self { NsObject { obj_type: ObjType::Obj, obj_path: OnceLock::new(), nsobj_path: OnceLock::from(NsObjPath::from(hash)), childs: OnceLock::new(), } } pub fn get_obj_path(&self) -> &ObjPath { self.obj_path.get_or_init(|| todo!()) } fn get_nsobj_path(&self) -> &NsObjPath { self.nsobj_path .get_or_init(|| NsObjPath::from(self.get_obj_path())) } /// Return the existence of the nsobj in the store pub fn exists(&self) -> bool { println!("{}", self.get_nsobj_path().display()); self.get_nsobj_path().exists() } pub fn get_childs(&self) -> &NsObjectChilds { self.childs.get_or_init(|| { if self.obj_type != ObjType::Tree { Vec::new() } else { todo!() } }) } pub fn is_file(&self) -> bool { self.obj_type == ObjType::Blob } pub fn is_dir(&self) -> bool { self.obj_type == ObjType::Tree } pub fn get_local_obj(&self) -> Obj { let mut obj = Obj::from_local_path(self.get_obj_path()); obj.set_type(self.obj_type.clone()); obj } /// Saving the nsobj in the store /// /// first line consists of all size fixed datas (obj_type, size, modified) /// next lines are the size variable datas /// /// * if it is a Tree obj after an empty line there will be the definition /// of its subobjs (one line by subobj) * /// obj_type + hash pub fn save(&self) -> Result<(), ()> { if !self.get_obj_path().exists() { // delete current obj // delete reference on parent } else { } Ok(()) } pub fn get_metadata(&self) -> Option { todo!() } pub fn iter(&self) -> NsObjectIter<'_> { NsObjectIter { nsobject: self, index: 0, } } } pub struct NsObjectIter<'a> { nsobject: &'a NsObject, index: usize, } impl<'a> Iterator for NsObjectIter<'a> { type Item = &'a NsObject; fn next(&mut self) -> Option { self.index += 1; match self.nsobject.get_childs().get(self.index - 1) { None => None, Some(obj) => Some(&**obj), } } }