impl From<AsRef<str>> for EntryValue, replace where appropriate

feat/vaults
Tomáš Mládek 2022-02-13 12:37:16 +01:00
parent e81d520993
commit b417dad0ec
No known key found for this signature in database
GPG Key ID: 65E225C8B3E2ED8A
5 changed files with 31 additions and 35 deletions

View File

@ -1,5 +1,5 @@
use crate::addressing::Address;
use crate::database::entry::{EntryValue, InvariantEntry};
use crate::database::entry::{InvariantEntry};
pub const TYPE_TYPE_VAL: &str = "TYPE";
pub const TYPE_BASE_ATTR: &str = "TYPE";
@ -17,12 +17,12 @@ pub const ADDED_ATTR: &str = "ADDED";
lazy_static! {
pub static ref TYPE_INVARIANT: InvariantEntry = InvariantEntry {
attribute: String::from(TYPE_BASE_ATTR),
value: EntryValue::String(String::from(TYPE_TYPE_VAL)),
value: TYPE_TYPE_VAL.into(),
};
pub static ref TYPE_ADDR: Address = TYPE_INVARIANT.entity().unwrap();
pub static ref HIER_INVARIANT: InvariantEntry = InvariantEntry {
attribute: String::from(TYPE_BASE_ATTR),
value: EntryValue::String(String::from(HIER_TYPE_VAL)),
value: HIER_TYPE_VAL.into(),
};
pub static ref HIER_ADDR: Address = HIER_INVARIANT.entity().unwrap();
}

View File

@ -158,18 +158,6 @@ impl EntryValue {
}
}
impl std::fmt::Display for EntryValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let (entry_type, entry_value) = match self {
EntryValue::Address(address) => ("ADDRESS", address.to_string()),
EntryValue::String(string) => ("STRING", string.to_owned()),
EntryValue::Number(n) => ("NUMBER", n.to_string()),
EntryValue::Invalid => ("INVALID", "INVALID".to_string()),
};
write!(f, "{}: {}", entry_type, entry_value)
}
}
impl std::str::FromStr for EntryValue {
type Err = std::convert::Infallible;
@ -204,9 +192,24 @@ impl std::str::FromStr for EntryValue {
}
}
impl From<String> for EntryValue {
fn from(str: String) -> Self {
Self::String(str)
impl std::fmt::Display for EntryValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let (entry_type, entry_value) = match self {
EntryValue::Address(address) => ("ADDRESS", address.to_string()),
EntryValue::String(string) => ("STRING", string.to_owned()),
EntryValue::Number(n) => ("NUMBER", n.to_string()),
EntryValue::Invalid => ("INVALID", "INVALID".to_string()),
};
write!(f, "{}: {}", entry_type, entry_value)
}
}
impl<S> From<S> for EntryValue
where
S: AsRef<str>,
{
fn from(str: S) -> Self {
Self::String(str.as_ref().to_string())
}
}
@ -237,6 +240,8 @@ mod tests {
let decoded = encoded.parse::<EntryValue>()?;
assert_eq!(entry, decoded);
assert_eq!(EntryValue::String(String::from("UPEND")), "UPEND".into());
Ok(())
}
}

View File

@ -452,10 +452,7 @@ mod test {
);
query.to_sqlite_predicates()?;
let values = vec![
EntryValue::String("FOO".to_string()),
EntryValue::String("BAR".to_string()),
];
let values: Vec<EntryValue> = vec!["FOO".into(), "BAR".into()];
let query = format!(
"(matches ? ? (in {}))",
values
@ -496,10 +493,7 @@ mod test {
query.to_sqlite_predicates()?;
// Invalid queries
let values = vec![
EntryValue::String("FOO".to_string()),
EntryValue::Number(1337.93),
];
let values: Vec<EntryValue> = vec!["FOO".into(), EntryValue::Number(1337.93)];
let query = format!(
"(matches ? ? (in {}))",
values
@ -522,10 +516,7 @@ mod test {
.err()
.ok_or(anyhow!("Failed to reject mixed query."))?;
let values = vec![
EntryValue::Number(1337.93),
EntryValue::String("FOO".to_string()),
];
let values = vec![EntryValue::Number(1337.93), "FOO".into()];
let query = format!(
"(matches ? ? (in {}))",
values

View File

@ -34,7 +34,7 @@ const FILE_SIZE_KEY: &str = "FILE_SIZE";
lazy_static! {
static ref BLOB_TYPE_INVARIANT: InvariantEntry = InvariantEntry {
attribute: String::from(TYPE_BASE_ATTR),
value: EntryValue::String(String::from(BLOB_TYPE)),
value: BLOB_TYPE.into(),
};
static ref BLOB_TYPE_ADDR: Address = BLOB_TYPE_INVARIANT.entity().unwrap();
}
@ -410,7 +410,7 @@ fn insert_file_with_metadata(
let mime_entry = mime_type.map(|mime_type| Entry {
entity: blob_address.clone(),
attribute: FILE_MIME_KEY.to_string(),
value: EntryValue::String(mime_type),
value: mime_type.into(),
});
let added_entry = Entry {
@ -464,7 +464,7 @@ fn insert_file_with_metadata(
let label_entry = Entry {
entity: blob_address.clone(),
attribute: LABEL_ATTR.to_string(),
value: EntryValue::String(filename.as_os_str().to_string_lossy().to_string()),
value: filename.as_os_str().to_string_lossy().into(),
};
let label_entry_addr = connection.insert_entry(label_entry)?;

View File

@ -341,7 +341,7 @@ pub async fn put_object(
Address::Attribute(attribute) => vec![Entry {
entity: address.clone(),
attribute: LABEL_ATTR.to_string(),
value: EntryValue::String(format!("ATTRIBUTE: {attribute}")),
value: format!("ATTRIBUTE: {attribute}").into(),
}],
Address::Url(url) => {
#[cfg(feature = "extractors-web")]
@ -353,7 +353,7 @@ pub async fn put_object(
vec![Entry {
entity: address.clone(),
attribute: LABEL_ATTR.to_string(),
value: EntryValue::String(url.clone()),
value: url.clone().into(),
}]
}
};