diff --git a/src/core/error.rs b/src/core/error.rs index 1a8eb79..56cdf4d 100644 --- a/src/core/error.rs +++ b/src/core/error.rs @@ -145,6 +145,13 @@ impl MizeError { panic!(); } + pub fn location(mut self, location: MizeCodeLocation) -> MizeError { + + self.code_location = Some(location); + + self + } + } /* diff --git a/src/core/instance/mod.rs b/src/core/instance/mod.rs index df28254..14a7239 100644 --- a/src/core/instance/mod.rs +++ b/src/core/instance/mod.rs @@ -57,7 +57,7 @@ impl Instance { instance.init(); - debug!("instance inited with conifg: {}", instance.get("0/config")?.as_data_full()?); + debug!("instance inited with config: {}", instance.get("0/config")?.as_data_full()?); return Ok(instance); } @@ -85,15 +85,18 @@ impl Instance { debug!("overwriting instance config again with the one passed to Instance::with_config()"); instance.set("0", config); - //debug!("instance inited with conifg: {}", instance.get("0/config")?); - debug!("instance inited with conifg: no"); + debug!("instance inited with conifg: {}", instance.get("0/config")?); Ok(instance) } pub fn migrate_to_store(&mut self, new_store: Box) -> MizeResult<()> { + println!("MIGRATING"); let old_store = &self.store; + let inst_data = self.store.get_value_data_full(self.id_from_string("0".to_owned()))?; + new_store.set(self.id_from_string("0".to_owned()), inst_data.to_owned())?; + for id in old_store.id_iter()? { let data = self.store.get_value_data_full(self.id_from_string(id?))?; diff --git a/src/core/item.rs b/src/core/item.rs index f875ce1..a12f185 100644 --- a/src/core/item.rs +++ b/src/core/item.rs @@ -66,8 +66,8 @@ impl Item<'_> { impl<'a> Display for Item<'a> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let item_data = self.as_data_full().map_err(|_| std::fmt::Error)?; - write!(f, "{}", item_data); + //let item_data = self.as_data_full().map_err(|_| std::fmt::Error)?; + write!(f, "Item with id: {}", self.id()); return Ok(()); } } @@ -120,6 +120,9 @@ impl ItemData { pub fn item_data_get_path(data: &CborValue, path: Vec) -> MizeResult<&CborValue> { let mut path_iter = path.clone().into_iter(); + //println!("data: {:?}", data); + //println!("path: {:?}", path); + let path_el = match path_iter.nth(0) { Some(val) => val, None => return Ok(data), // our base case @@ -129,8 +132,11 @@ pub fn item_data_get_path(data: &CborValue, path: Vec) -> MizeResult<&Cb match data { CborValue::Map(ref map) => { for (key, val) in map { + //println!("key: {:?}", key); + //println!("path_el: {}", path_el); if let CborValue::Text(key_str) = key { if key_str == &path_el { + println!("setting sub_data to: {:?}", val); sub_data = val; } } diff --git a/src/core/memstore.rs b/src/core/memstore.rs index 099450a..d8dfdf0 100644 --- a/src/core/memstore.rs +++ b/src/core/memstore.rs @@ -86,6 +86,7 @@ impl Store for MemStore { } fn next_id(&self, prev_id_str: &str) -> MizeResult> { + println!("next_id: prev_id_str: {}", prev_id_str); let inner = self.inner.lock()?; let mut id = str_to_u64(prev_id_str)?; @@ -99,6 +100,7 @@ impl Store for MemStore { } if inner.map.contains_key(&id) { + println!("returning: {}", id); return Ok(Some(format!("{}", id))); } else { @@ -139,7 +141,20 @@ pub fn get_raw_from_cbor<'a>(value: &'a CborValue, path: Vec<&String>) -> MizeRe Some(val) => val, None => { // our base case - return Err(MizeError::new().msg("path is empty")); + let ret_val = match value { + CborValue::Bytes(vec) => { + trace!("[ {} ] ret value: {:?}", "RET".yellow(), &vec[..]); + return Ok(&vec[..]); + }, + CborValue::Text(string) => { + trace!("[ {} ] ret value: {:?}", "RET".yellow(), string.as_bytes()); + return Ok(string.as_bytes()); + }, + other => { + return Err(MizeError::new() + .msg("path is empty and the value is neither Bytes nor Text")); + }, + }; }, }; trace!("hoooooooo: {:?}", path_el); @@ -181,7 +196,7 @@ pub fn get_raw_from_cbor<'a>(value: &'a CborValue, path: Vec<&String>) -> MizeRe trace!("[ {} ] ret value: {:?}", "RET".yellow(), ret_value); return ret_value; }, - _ => Err(MizeError::new().msg("get_raw_from_cbor: value is not Bytes, Text or Map")), + _ => Err(MizeError::new().msg("get_raw_from_cbor: value is not a map, text or bytes")), } } diff --git a/src/platform/os/cli/get.rs b/src/platform/os/cli/get.rs index afda78c..f43d00a 100644 --- a/src/platform/os/cli/get.rs +++ b/src/platform/os/cli/get.rs @@ -19,6 +19,10 @@ pub fn get(sub_matches: &ArgMatches) -> MizeResult<()> { let item = instance.get(id)?; let recurse_count = sub_matches.get_count("recurse"); + + println!("{}", item.as_data_full()?); + return Ok(()); + if recurse_count > 0 { println!("{}", item.id()); println!("{}", item.as_data_full()?); diff --git a/src/platform/os/fsstore.rs b/src/platform/os/fsstore.rs index fbcd3fb..74b8c2f 100644 --- a/src/platform/os/fsstore.rs +++ b/src/platform/os/fsstore.rs @@ -20,23 +20,28 @@ pub struct FileStore { } impl FileStore { - pub fn new(path: String) -> MizeResult { + pub fn new(path_str: &str) -> MizeResult { + + let path = Path::new(path_str); // create that path if it does not exist fs::create_dir_all(Path::new(&path).join("store"))?; // check for valid pid file - if let Some(pid) = valid_pid_file(&path)? { + if let Some(pid) = valid_pid_file(path)? { // store already opened - return Err(mize_err!("MizeStore at path {} is already opened by process with pid {}", &path, pid)); + return Err(mize_err!("MizeStore at path {} is already opened by process with pid {}", path.display(), pid)); } else { // write our own pid file let pid = std::process::id(); - let mut file = OpenOptions::new().write(true).create(true).open(&path)?; + let mut file = OpenOptions::new().write(true).create(true).open(path.join("pid"))?; write!(file, "{}", pid)?; } + // init the store + fs::write(path.join("next_id"), "0"); + Ok(FileStore { path: Path::new(&path).to_owned() }) } } @@ -59,7 +64,10 @@ impl Store for FileStore { } fn set(&self, id: MizeId, data: ItemData) -> MizeResult<()> { + println!("setting: {:?}", id); let path = self.path.join("store").join(id.namespace_str()).join(id.store_part()); + println!("path: {:?}", &path); + fs::create_dir_all(self.path.join("store").join(id.namespace_str()))?; let file = OpenOptions::new().write(true).create(true).open(path)?; @@ -92,7 +100,9 @@ impl Store for FileStore { } fn get_value_data_full(&self, id: MizeId) -> MizeResult { - let file = OpenOptions::new().read(true).create(true).open(self.path.join("store").join(id.store_part()))?; + let file_path = self.path.join("store").join(id.namespace_str()).join(id.store_part()); + println!("file_path: {:?}", file_path.display()); + let file = OpenOptions::new().read(true).open(file_path)?; let cbor_value: CborValue = ciborium::from_reader(file) .mize_result_msg(format!("could not read file '{}' from FileStore", self.path.join("store").join(id.store_part()).display()))?; @@ -117,9 +127,9 @@ impl Store for FileStore { } } -fn valid_pid_file(path: &String) -> MizeResult> { +fn valid_pid_file(path: &Path) -> MizeResult> { - let pid_file_path = Path::new(path).join("pid"); + let pid_file_path = path.join("pid"); // if the pid file does not exist, there is no pid... if !pid_file_path.exists() { diff --git a/src/platform/os/mod.rs b/src/platform/os/mod.rs index 492dff9..455dfc2 100644 --- a/src/platform/os/mod.rs +++ b/src/platform/os/mod.rs @@ -59,11 +59,11 @@ pub fn os_instance_init(instance: &mut Instance) -> MizeResult<()> { }; ////// if a config.store is set, upgrade to the filestore there - let instance_path = instance.get("0/conifg/instance_path")?.value_string()?; + let store_path = instance.get("0/config/store_path")?.value_string()?; - if instance_path != "" { - let file_store = FileStore::new(instance_path)?; - let new_instance = &mut instance.migrate_to_store(Box::new(file_store))?; + if store_path != "" { + let file_store = FileStore::new(store_path.as_str())?; + instance.migrate_to_store(Box::new(file_store))?; } Ok(()) diff --git a/test-config.toml b/test-config.toml index dfe6d64..ce842ab 100644 --- a/test-config.toml +++ b/test-config.toml @@ -2,3 +2,4 @@ [config] namespace = "test.c2vi.dev" test = "hello from test item" +store_path = "/home/me/.mize"