refactor(db): better impls for UNode/UHierPath

feat/hier-walker
Tomáš Mládek 2023-10-28 21:14:20 +02:00
parent 4dbf8b745b
commit 6656e9f5d1
2 changed files with 22 additions and 26 deletions

View File

@ -17,21 +17,21 @@ use super::UpEndConnection;
#[derive(Debug, Clone, Eq, PartialEq, Hash)] #[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct UNode(String); pub struct UNode(String);
impl UNode { impl std::str::FromStr for UNode {
pub fn new<T: Into<String>>(s: T) -> Result<Self> { type Err = anyhow::Error;
let s = s.into();
if s.is_empty() { fn from_str(string: &str) -> Result<Self, Self::Err> {
return Err(anyhow!("UNode can not be empty.")); if string.is_empty() {
Err(anyhow!("UNode can not be empty."))
} else {
Ok(Self(string.to_string()))
} }
Ok(Self(s))
} }
} }
impl From<UNode> for String { impl std::fmt::Display for UNode {
fn from(value: UNode) -> Self { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
value.0 write!(f, "{}", self.0)
} }
} }
@ -48,7 +48,7 @@ impl std::str::FromStr for UHierPath {
let result: Result<Vec<UNode>> = string let result: Result<Vec<UNode>> = string
.trim_end_matches('/') .trim_end_matches('/')
.split('/') .split('/')
.map(|part| UNode::new(String::from(part))) .map(UNode::from_str)
.collect(); .collect();
Ok(UHierPath(result?)) 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 { impl std::fmt::Display for UHierPath {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!( write!(
@ -112,7 +106,7 @@ pub fn fetch_or_create_dir(
.query(Query::SingleQuery(QueryPart::Matches(PatternQuery { .query(Query::SingleQuery(QueryPart::Matches(PatternQuery {
entity: QueryComponent::Variable(None), entity: QueryComponent::Variable(None),
attribute: QueryComponent::Exact(ATTR_LABEL.into()), attribute: QueryComponent::Exact(ATTR_LABEL.into()),
value: QueryComponent::Exact(String::from(directory.clone()).into()), value: QueryComponent::Exact(directory.to_string().into()),
})))? })))?
.into_iter() .into_iter()
.map(|e: Entry| e.entity); .map(|e: Entry| e.entity);
@ -142,7 +136,7 @@ pub fn fetch_or_create_dir(
let directory_entry = Entry { let directory_entry = Entry {
entity: new_directory_address.clone(), entity: new_directory_address.clone(),
attribute: String::from(ATTR_LABEL), attribute: String::from(ATTR_LABEL),
value: String::from(directory).into(), value: directory.to_string().into(),
provenance: "SYSTEM FS".to_string(), provenance: "SYSTEM FS".to_string(),
timestamp: chrono::Utc::now().naive_utc(), timestamp: chrono::Utc::now().naive_utc(),
}; };
@ -249,10 +243,10 @@ mod tests {
#[test] #[test]
fn test_unode_nonempty() { fn test_unode_nonempty() {
let node = UNode::new("foobar"); let node = "foobar".parse::<UNode>();
assert!(node.is_ok()); assert!(node.is_ok());
let node = UNode::new(""); let node = "".parse::<UNode>();
assert!(node.is_err()); assert!(node.is_err());
} }

View File

@ -1,7 +1,7 @@
use self::db::files; use self::db::files;
use super::{Blob, StoreError, UpStore, UpdatePathOutcome}; 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::jobs::{JobContainer, JobHandle};
use crate::util::hash_at_path; use crate::util::hash_at_path;
use crate::{ConnectionOptions, LoggingHandler, UpEndConnection, UpEndDatabase, UPEND_SUBDIR}; use crate::{ConnectionOptions, LoggingHandler, UpEndConnection, UpEndDatabase, UPEND_SUBDIR};
@ -435,10 +435,12 @@ impl FsStore {
let (filename, dir_path) = components.split_last().unwrap(); let (filename, dir_path) = components.split_last().unwrap();
let upath = UHierPath( let upath = UHierPath(
iter::once(UNode::new("NATIVE").unwrap()) iter::once("NATIVE".parse().unwrap())
.chain(dir_path.iter().map(|component| { .chain(
UNode::new(component.as_os_str().to_string_lossy().to_string()).unwrap() dir_path
})) .iter()
.map(|component| component.as_os_str().to_string_lossy().parse().unwrap()),
)
.collect(), .collect(),
); );
let resolved_path = match resolve_cache { let resolved_path = match resolve_cache {