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)]
pub struct UNode(String);
impl UNode {
pub fn new<T: Into<String>>(s: T) -> Result<Self> {
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<Self, Self::Err> {
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 {
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<Vec<UNode>> = 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::<UNode>();
assert!(node.is_ok());
let node = UNode::new("");
let node = "".parse::<UNode>();
assert!(node.is_err());
}

View File

@ -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 {