From bc6a23b76bd481a28ad88c6eae91d091d3ea6bd2 Mon Sep 17 00:00:00 2001 From: grimhilt Date: Sun, 8 Sep 2024 15:56:20 +0200 Subject: [PATCH] feat(structs): set wrappers to path --- src/commands/add.rs | 3 +- src/commands/status.rs | 9 +++-- src/store.rs | 1 + src/store/nsobject.rs | 72 ++++++++++++++-------------------------- src/store/object.rs | 10 +++--- src/store/structs.rs | 74 ++++++++++++++++++++++++++++++++++++++++++ tests/status_test.rs | 3 ++ 7 files changed, 113 insertions(+), 59 deletions(-) create mode 100644 src/store/structs.rs diff --git a/src/commands/add.rs b/src/commands/add.rs index cb8ed92..fae9d6f 100644 --- a/src/commands/add.rs +++ b/src/commands/add.rs @@ -1,5 +1,6 @@ use crate::config::config::Config; use crate::store::{ + structs::to_obj_path, ignorer::Ignorer, indexer::Indexer, nsobject::{self, NsObject}, @@ -43,7 +44,7 @@ pub fn exec(args: AddArgs, config: Config) { } else { indexer.index_file(path_to_add); } - } else if NsObject::from_local_path(&path_to_add).exists() { + } else if NsObject::from_local_path(&to_obj_path(&path_to_add)).exists() { indexer.index_file(path_to_add); } else { // try globbing diff --git a/src/commands/status.rs b/src/commands/status.rs index 026d334..6706b38 100644 --- a/src/commands/status.rs +++ b/src/commands/status.rs @@ -1,5 +1,6 @@ use crate::config::config::Config; use crate::store::{ + structs::to_obj_path, indexer::Indexer, nsobject::NsObject, object::{Obj, ObjStatus, ObjType}, @@ -235,8 +236,7 @@ fn compare_dir( } }; - let repo_relative_entry = path::to_repo_relative(&entry.path(), root_path); - let local_obj = Obj::from_local_path(&repo_relative_entry); + let local_obj = Obj::from_local_path(&entry.path().into()); if entry.path().is_dir() { if entry.path().ends_with(".nextsync") { @@ -280,7 +280,7 @@ fn compare_dir( } // Read ns objects to find deleted - let entries = NsObject::from_local_path(&path); + let entries = NsObject::from_local_path(&to_obj_path(&path)); for entry in entries.iter() { if entry.is_file() { match local_files.get(entry.get_obj_path().to_str().unwrap()) { @@ -324,8 +324,7 @@ fn add_childs(root_path: &PathBuf, path: &PathBuf, res: Arc) { } }; - let repo_relative_entry = path::to_repo_relative(&entry.path(), root_path); - let local_obj = Obj::from_local_path(&repo_relative_entry); + let local_obj = Obj::from_local_path(&entry.path().into()); if entry.path().is_dir() { if entry.path().ends_with(".nextsync") { continue; diff --git a/src/store.rs b/src/store.rs index a525f3b..3d643af 100644 --- a/src/store.rs +++ b/src/store.rs @@ -3,3 +3,4 @@ pub mod indexer; pub mod nsignore; pub mod nsobject; pub mod object; +pub mod structs; diff --git a/src/store/nsobject.rs b/src/store/nsobject.rs index a226145..f59b073 100644 --- a/src/store/nsobject.rs +++ b/src/store/nsobject.rs @@ -1,66 +1,43 @@ -use crate::store::object::{Obj, ObjMetadata, ObjType}; -use crypto::digest::Digest; -use crypto::sha1::Sha1; +use crate::store::{ + object::{Obj, ObjMetadata, ObjType}, + structs::{NsObjPath, ObjPath}, +}; use std::path::PathBuf; use std::sync::OnceLock; pub static REPO_ROOT: OnceLock = OnceLock::new(); pub fn init(repo_root: &PathBuf) { - REPO_ROOT.set(repo_root.clone()); + let _ = REPO_ROOT.set(repo_root.clone()); +} + +pub fn get_repo_root() -> PathBuf { + match REPO_ROOT.get() { + Some(path) => path.clone(), + None => { + panic!("fatal: 'REPO_ROOT' not set, you must initialize nsobject before using it!") + } + } } type NsObjectChilds = Vec>; -struct NsObjectPath { - path: PathBuf, -} - -impl From<&str> for NsObjectPath { - fn from(hash: &str) -> Self { - let (dir, res) = hash.split_at(2); - - let mut ns_obj_path = match REPO_ROOT.get() { - Some(path) => path.clone(), - None => { - panic!("fatal: 'REPO_ROOT' not set, you must initialize nsobject before using it!") - } - }; - ns_obj_path.push(dir); - ns_obj_path.push(res); - - NsObjectPath { path: ns_obj_path } - } -} - -impl From<&PathBuf> for NsObjectPath { - fn from(obj_path: &PathBuf) -> Self { - let mut hasher = Sha1::new(); - hasher.input_str( - obj_path - .to_str() - .expect("Cannot contains non UTF-8 char in path"), - ); - NsObjectPath::from(hasher.result_str().as_str()) - } -} - pub struct NsObject { pub obj_type: ObjType, - obj_path: OnceLock, - nsobj_path: OnceLock, + /// path of the obj in the repo + obj_path: OnceLock, + /// path of the nsobj file in the store + nsobj_path: OnceLock, childs: OnceLock, - index: usize, } impl NsObject { - pub fn from_local_path(path: &PathBuf) -> Self { + pub fn from_local_path(path: &ObjPath) -> Self { NsObject { obj_type: ObjType::Obj, - obj_path: OnceLock::from(path.to_path_buf()), + obj_path: OnceLock::from(path.clone()), nsobj_path: OnceLock::new(), childs: OnceLock::new(), - index: 0, } } @@ -68,19 +45,18 @@ impl NsObject { NsObject { obj_type: ObjType::Obj, obj_path: OnceLock::new(), - nsobj_path: OnceLock::from(NsObjectPath::from(hash).path), + nsobj_path: OnceLock::from(NsObjPath::from(hash)), childs: OnceLock::new(), - index: 0, } } - pub fn get_obj_path(&self) -> &PathBuf { + pub fn get_obj_path(&self) -> &ObjPath { self.obj_path.get_or_init(|| todo!()) } - fn get_nsobj_path(&self) -> &PathBuf { + fn get_nsobj_path(&self) -> &NsObjPath { self.nsobj_path - .get_or_init(|| NsObjectPath::from(self.get_obj_path()).path) + .get_or_init(|| NsObjPath::from(self.get_obj_path())) } /// Return the existence of the nsobj in the store diff --git a/src/store/object.rs b/src/store/object.rs index d8c9ebd..1f58653 100644 --- a/src/store/object.rs +++ b/src/store/object.rs @@ -1,4 +1,4 @@ -use crate::store::nsobject::NsObject; +use crate::store::{structs::ObjPath, nsobject::NsObject}; use crate::utils::path; use std::fs; use std::path::PathBuf; @@ -55,16 +55,16 @@ pub struct Obj { obj_type: ObjType, status: OnceLock, /// path of the object from root - obj_path: PathBuf, + obj_path: ObjPath, } impl Obj { - pub fn from_local_path(path: &PathBuf) -> Self { + pub fn from_local_path(path: &ObjPath) -> Self { // todo set state Obj { obj_type: ObjType::Obj, status: OnceLock::new(), - obj_path: path.to_path_buf(), + obj_path: path.clone(), } } @@ -101,7 +101,7 @@ impl Obj { } pub fn get_metadata(&self) -> Option { - let metadata = match fs::metadata(&self.obj_path) { + let metadata = match fs::metadata(&*self.obj_path) { Ok(m) => m, Err(err) => { eprintln!( diff --git a/src/store/structs.rs b/src/store/structs.rs new file mode 100644 index 0000000..33d3c37 --- /dev/null +++ b/src/store/structs.rs @@ -0,0 +1,74 @@ +use crate::store::nsobject::get_repo_root; +use std::path::PathBuf; +use crypto::digest::Digest; +use crypto::sha1::Sha1; +use std::ops::{Deref, DerefMut}; +use crate::utils::path; + +#[derive(Debug, PartialEq, Clone)] +pub struct ObjPath { + path: PathBuf +} + +impl Deref for ObjPath { + type Target = PathBuf; + fn deref(&self) -> &PathBuf { &self.path } +} + +impl DerefMut for ObjPath { + fn deref_mut(&mut self) -> &mut PathBuf { &mut self.path } +} + +pub fn to_obj_path(path: &PathBuf) -> ObjPath { + ObjPath { path: path.clone() } +} + +impl Into for &PathBuf { + fn into(self) -> ObjPath { + ObjPath { path: path::to_repo_relative(self, &get_repo_root())} + } +} + +impl Into for PathBuf { + fn into(self) -> ObjPath { + ObjPath { path: path::to_repo_relative(&self, &get_repo_root())} + } +} + +#[derive(Debug, PartialEq)] +pub struct NsObjPath { + path: PathBuf +} + +impl Deref for NsObjPath { + type Target = PathBuf; + fn deref(&self) -> &PathBuf { &self.path } +} + +impl DerefMut for NsObjPath { + fn deref_mut(&mut self) -> &mut PathBuf { &mut self.path } +} + +impl From<&str> for NsObjPath { + fn from(hash: &str) -> Self { + let (dir, res) = hash.split_at(2); + + let mut ns_obj_path = get_repo_root(); + ns_obj_path.push(dir); + ns_obj_path.push(res); + + NsObjPath { path: ns_obj_path } + } +} + +impl From<&ObjPath> for NsObjPath { + fn from(obj_path: &ObjPath) -> Self { + let mut hasher = Sha1::new(); + hasher.input_str( + obj_path + .to_str() + .expect("Cannot contains non UTF-8 char in path"), + ); + NsObjPath::from(hasher.result_str().as_str()) + } +} diff --git a/tests/status_test.rs b/tests/status_test.rs index cb35777..483a158 100644 --- a/tests/status_test.rs +++ b/tests/status_test.rs @@ -103,3 +103,6 @@ fn part_of_folder() -> io::Result<()> { } // ../folder/file add +// force add ignored file +// status without ignored file +// all folder without ignored file