also impl From<f64> and From<Address>

feat/vaults
Tomáš Mládek 2022-02-13 12:55:49 +01:00
parent b417dad0ec
commit 0ef7ea5b3a
No known key found for this signature in database
GPG Key ID: 65E225C8B3E2ED8A
3 changed files with 45 additions and 25 deletions

View File

@ -204,12 +204,27 @@ impl std::fmt::Display for EntryValue {
}
}
impl<S> From<S> for EntryValue
where
S: AsRef<str>,
{
fn from(str: S) -> Self {
Self::String(str.as_ref().to_string())
impl From<&str> for EntryValue {
fn from(str: &str) -> Self {
Self::String(str.to_string())
}
}
impl From<String> for EntryValue {
fn from(str: String) -> Self {
Self::String(str)
}
}
impl From<f64> for EntryValue {
fn from(num: f64) -> Self {
Self::Number(num)
}
}
impl From<Address> for EntryValue {
fn from(address: Address) -> Self {
Self::Address(address)
}
}
@ -240,8 +255,14 @@ mod tests {
let decoded = encoded.parse::<EntryValue>()?;
assert_eq!(entry, decoded);
assert_eq!(EntryValue::String(String::from("UPEND")), "UPEND".into());
Ok(())
}
#[test]
fn test_into() {
assert_eq!(EntryValue::String(String::from("UPEND")), "UPEND".into());
assert_eq!(EntryValue::Number(1337.93), 1337.93.into());
let addr = Address::Url("https://upendproject.net".into());
assert_eq!(EntryValue::Address(addr.clone()), addr.into());
}
}

View File

@ -98,7 +98,7 @@ pub fn list_roots(connection: &UpEndConnection) -> Result<Vec<Address>> {
connection.query(Query::SingleQuery(QueryPart::Matches(EntryQuery {
entity: QueryComponent::Any,
attribute: QueryComponent::Exact(IS_OF_TYPE_ATTR.to_string()),
value: QueryComponent::Exact(EntryValue::Address(HIER_ADDR.clone())),
value: QueryComponent::Exact(HIER_ADDR.clone().into()),
})))?;
// TODO: this is horrible
@ -144,7 +144,7 @@ pub fn fetch_or_create_dir(
.query(Query::SingleQuery(QueryPart::Matches(EntryQuery {
entity: QueryComponent::Any,
attribute: QueryComponent::Exact(String::from(LABEL_ATTR)),
value: QueryComponent::Exact(EntryValue::String(directory.as_ref().clone())),
value: QueryComponent::Exact(directory.as_ref().clone().into()),
})))?
.into_iter()
.map(|e: Entry| e.entity);
@ -174,14 +174,14 @@ pub fn fetch_or_create_dir(
let type_entry = Entry {
entity: new_directory_address.clone(),
attribute: String::from(IS_OF_TYPE_ATTR),
value: EntryValue::Address(HIER_ADDR.clone()),
value: HIER_ADDR.clone().into(),
};
connection.insert_entry(type_entry)?;
let directory_entry = Entry {
entity: new_directory_address.clone(),
attribute: String::from(LABEL_ATTR),
value: EntryValue::String(directory.as_ref().clone()),
value: directory.as_ref().clone().into(),
};
connection.insert_entry(directory_entry)?;
@ -189,7 +189,7 @@ pub fn fetch_or_create_dir(
let has_entry = Entry {
entity: parent,
attribute: String::from(HIER_HAS_ATTR),
value: EntryValue::Address(new_directory_address.clone()),
value: new_directory_address.clone().into(),
};
connection.insert_entry(has_entry)?;
}

View File

@ -10,7 +10,7 @@ use crate::database::constants::{
ADDED_ATTR, HIER_HAS_ATTR, IS_OF_TYPE_ATTR, LABEL_ATTR, TYPE_ADDR, TYPE_BASE_ATTR,
TYPE_HAS_ATTR,
};
use crate::database::entry::{Entry, EntryValue, InvariantEntry};
use crate::database::entry::{Entry, InvariantEntry};
use crate::database::hierarchies::{
resolve_path, resolve_path_cached, ResolveCache, UHierPath, UNode,
};
@ -398,13 +398,13 @@ fn insert_file_with_metadata(
let type_entry = Entry {
entity: blob_address.clone(),
attribute: String::from(IS_OF_TYPE_ATTR),
value: EntryValue::Address(BLOB_TYPE_ADDR.clone()),
value: BLOB_TYPE_ADDR.clone().into(),
};
let size_entry = Entry {
entity: blob_address.clone(),
attribute: FILE_SIZE_KEY.to_string(),
value: EntryValue::Number(size as f64),
value: (size as f64).into(),
};
let mime_entry = mime_type.map(|mime_type| Entry {
@ -416,12 +416,11 @@ fn insert_file_with_metadata(
let added_entry = Entry {
entity: blob_address.clone(),
attribute: ADDED_ATTR.to_string(),
value: EntryValue::Number(
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs() as f64,
),
value: (SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs() as f64)
.into(),
};
// Add the appropriate entries w/r/t virtual filesystem location
@ -457,21 +456,21 @@ fn insert_file_with_metadata(
let dir_has_entry = Entry {
entity: parent_dir.clone(),
attribute: HIER_HAS_ATTR.to_string(),
value: EntryValue::Address(blob_address.clone()),
value: blob_address.clone().into(),
};
let dir_has_entry_addr = connection.insert_entry(dir_has_entry)?;
let label_entry = Entry {
entity: blob_address.clone(),
attribute: LABEL_ATTR.to_string(),
value: filename.as_os_str().to_string_lossy().into(),
value: filename.as_os_str().to_string_lossy().to_string().into(),
};
let label_entry_addr = connection.insert_entry(label_entry)?;
let alias_entry = Entry {
entity: dir_has_entry_addr,
attribute: ALIAS_KEY.to_string(),
value: EntryValue::Address(label_entry_addr),
value: label_entry_addr.into(),
};
connection.insert_entry(alias_entry)?;