feat(structs): set wrappers to path
This commit is contained in:
parent
1df9c3fba5
commit
bc6a23b76b
@ -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
|
||||
|
@ -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<ObjStatuses>) {
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
|
@ -3,3 +3,4 @@ pub mod indexer;
|
||||
pub mod nsignore;
|
||||
pub mod nsobject;
|
||||
pub mod object;
|
||||
pub mod structs;
|
||||
|
@ -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<PathBuf> = 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<Box<NsObject>>;
|
||||
|
||||
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<PathBuf>,
|
||||
nsobj_path: OnceLock<PathBuf>,
|
||||
/// path of the obj in the repo
|
||||
obj_path: OnceLock<ObjPath>,
|
||||
/// path of the nsobj file in the store
|
||||
nsobj_path: OnceLock<NsObjPath>,
|
||||
childs: OnceLock<NsObjectChilds>,
|
||||
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
|
||||
|
@ -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<ObjStatus>,
|
||||
/// 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<ObjMetadata> {
|
||||
let metadata = match fs::metadata(&self.obj_path) {
|
||||
let metadata = match fs::metadata(&*self.obj_path) {
|
||||
Ok(m) => m,
|
||||
Err(err) => {
|
||||
eprintln!(
|
||||
|
74
src/store/structs.rs
Normal file
74
src/store/structs.rs
Normal file
@ -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<ObjPath> for &PathBuf {
|
||||
fn into(self) -> ObjPath {
|
||||
ObjPath { path: path::to_repo_relative(self, &get_repo_root())}
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<ObjPath> 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())
|
||||
}
|
||||
}
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user