From 6656e9f5d1ece59ef678327a56d1897e5c186a2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Sat, 28 Oct 2023 21:14:20 +0200 Subject: [PATCH] refactor(db): better impls for UNode/UHierPath --- db/src/hierarchies.rs | 36 +++++++++++++++--------------------- db/src/stores/fs/mod.rs | 12 +++++++----- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/db/src/hierarchies.rs b/db/src/hierarchies.rs index e15e2ef..c1700c1 100644 --- a/db/src/hierarchies.rs +++ b/db/src/hierarchies.rs @@ -17,21 +17,21 @@ use super::UpEndConnection; #[derive(Debug, Clone, Eq, PartialEq, Hash)] pub struct UNode(String); -impl UNode { - pub fn new>(s: T) -> Result { - let s = s.into(); +impl std::str::FromStr for UNode { + type Err = anyhow::Error; - if s.is_empty() { - return Err(anyhow!("UNode can not be empty.")); + fn from_str(string: &str) -> Result { + if string.is_empty() { + Err(anyhow!("UNode can not be empty.")) + } else { + Ok(Self(string.to_string())) } - - Ok(Self(s)) } } -impl From for String { - fn from(value: UNode) -> Self { - value.0 +impl std::fmt::Display for UNode { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0) } } @@ -48,7 +48,7 @@ impl std::str::FromStr for UHierPath { let result: Result> = string .trim_end_matches('/') .split('/') - .map(|part| UNode::new(String::from(part))) + .map(UNode::from_str) .collect(); Ok(UHierPath(result?)) @@ -56,12 +56,6 @@ impl std::str::FromStr for UHierPath { } } -impl std::fmt::Display for UNode { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.0) - } -} - impl std::fmt::Display for UHierPath { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( @@ -112,7 +106,7 @@ pub fn fetch_or_create_dir( .query(Query::SingleQuery(QueryPart::Matches(PatternQuery { entity: QueryComponent::Variable(None), attribute: QueryComponent::Exact(ATTR_LABEL.into()), - value: QueryComponent::Exact(String::from(directory.clone()).into()), + value: QueryComponent::Exact(directory.to_string().into()), })))? .into_iter() .map(|e: Entry| e.entity); @@ -142,7 +136,7 @@ pub fn fetch_or_create_dir( let directory_entry = Entry { entity: new_directory_address.clone(), attribute: String::from(ATTR_LABEL), - value: String::from(directory).into(), + value: directory.to_string().into(), provenance: "SYSTEM FS".to_string(), timestamp: chrono::Utc::now().naive_utc(), }; @@ -249,10 +243,10 @@ mod tests { #[test] fn test_unode_nonempty() { - let node = UNode::new("foobar"); + let node = "foobar".parse::(); assert!(node.is_ok()); - let node = UNode::new(""); + let node = "".parse::(); assert!(node.is_err()); } diff --git a/db/src/stores/fs/mod.rs b/db/src/stores/fs/mod.rs index 5639c7a..7fb5bcd 100644 --- a/db/src/stores/fs/mod.rs +++ b/db/src/stores/fs/mod.rs @@ -1,7 +1,7 @@ use self::db::files; use super::{Blob, StoreError, UpStore, UpdatePathOutcome}; -use crate::hierarchies::{resolve_path, resolve_path_cached, ResolveCache, UHierPath, UNode}; +use crate::hierarchies::{resolve_path, resolve_path_cached, ResolveCache, UHierPath}; use crate::jobs::{JobContainer, JobHandle}; use crate::util::hash_at_path; use crate::{ConnectionOptions, LoggingHandler, UpEndConnection, UpEndDatabase, UPEND_SUBDIR}; @@ -435,10 +435,12 @@ impl FsStore { let (filename, dir_path) = components.split_last().unwrap(); let upath = UHierPath( - iter::once(UNode::new("NATIVE").unwrap()) - .chain(dir_path.iter().map(|component| { - UNode::new(component.as_os_str().to_string_lossy().to_string()).unwrap() - })) + iter::once("NATIVE".parse().unwrap()) + .chain( + dir_path + .iter() + .map(|component| component.as_os_str().to_string_lossy().parse().unwrap()), + ) .collect(), ); let resolved_path = match resolve_cache {