ensure UNode is non-empty
parent
816f04fc86
commit
fbee24978b
|
@ -16,7 +16,23 @@ use crate::database::lang::{EntryQuery, Query, QueryComponent, QueryPart};
|
|||
use crate::database::{bulk_retrieve_objects, insert_entry, query, DbPool};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct UNode(pub String);
|
||||
pub struct UNode(String);
|
||||
|
||||
impl UNode {
|
||||
pub fn new<T: Into<String>>(s: T) -> Result<Self> {
|
||||
let s = s.into();
|
||||
|
||||
if s.is_empty() {
|
||||
return Err(anyhow!("UNode can not be empty."));
|
||||
}
|
||||
|
||||
Ok(Self(s))
|
||||
}
|
||||
|
||||
pub fn as_ref(&self) -> &String {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct UPath(pub Vec<UNode>);
|
||||
|
@ -28,19 +44,13 @@ impl std::str::FromStr for UPath {
|
|||
if string.is_empty() {
|
||||
Ok(UPath(vec![]))
|
||||
} else {
|
||||
let result: Vec<UNode> = string
|
||||
let result: Result<Vec<UNode>> = string
|
||||
.trim_end_matches('/')
|
||||
.split('/')
|
||||
.map(|part| UNode(String::from(part)))
|
||||
.map(|part| UNode::new(String::from(part)))
|
||||
.collect();
|
||||
|
||||
for directory in &result {
|
||||
if directory.0.is_empty() {
|
||||
return Err(anyhow!("INVALID PATH: Directory name cannot be empty!"));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(UPath(result))
|
||||
Ok(UPath(result?))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -153,7 +163,9 @@ pub fn fetch_or_create_dir<C: Connection<Backend = Sqlite>>(
|
|||
Query::SingleQuery(QueryPart::Matches(EntryQuery {
|
||||
entity: QueryComponent::Any,
|
||||
attribute: QueryComponent::Exact(String::from(HIER_HAS_ATTR)),
|
||||
value: QueryComponent::Exact(EntryValue::Value(Value::String(directory.0.clone()))),
|
||||
value: QueryComponent::Exact(EntryValue::Value(Value::String(
|
||||
directory.as_ref().clone(),
|
||||
))),
|
||||
})),
|
||||
)?
|
||||
.into_iter()
|
||||
|
@ -200,7 +212,7 @@ pub fn fetch_or_create_dir<C: Connection<Backend = Sqlite>>(
|
|||
let directory_entry = Entry {
|
||||
entity: new_directory_address.clone(),
|
||||
attribute: String::from(HIER_HAS_ATTR),
|
||||
value: EntryValue::Value(Value::String(directory.0)),
|
||||
value: EntryValue::Value(Value::String(directory.as_ref().clone())),
|
||||
};
|
||||
insert_entry(connection, directory_entry)?;
|
||||
|
||||
|
@ -260,6 +272,15 @@ mod tests {
|
|||
|
||||
use super::{UNode, UPath};
|
||||
|
||||
#[test]
|
||||
fn test_unode_nonempty() {
|
||||
let node = UNode::new("foobar");
|
||||
assert!(node.is_ok());
|
||||
|
||||
let node = UNode::new("");
|
||||
assert!(node.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_path_codec() {
|
||||
let path = UPath(vec![
|
||||
|
|
|
@ -335,12 +335,10 @@ fn _process_directory_entry<P: AsRef<Path>>(
|
|||
let (filename, dir_path) = components.split_last().unwrap();
|
||||
|
||||
let upath = UPath(
|
||||
iter::once(UNode("NATIVE".to_string()))
|
||||
.chain(
|
||||
dir_path
|
||||
.iter()
|
||||
.map(|component| UNode(component.as_os_str().to_string_lossy().to_string())),
|
||||
)
|
||||
iter::once(UNode::new("NATIVE").unwrap())
|
||||
.chain(dir_path.iter().map(|component| {
|
||||
UNode::new(component.as_os_str().to_string_lossy().to_string()).unwrap()
|
||||
}))
|
||||
.collect(),
|
||||
);
|
||||
let resolved_path = resolve_path(&db_pool.write().unwrap().get()?, &upath, true)?;
|
||||
|
|
Loading…
Reference in New Issue