86 lines
2.7 KiB
Rust
86 lines
2.7 KiB
Rust
use crate::inner::models;
|
|
use anyhow::{anyhow, Result};
|
|
use std::convert::TryFrom;
|
|
use upend_base::addressing::{Address, Addressable};
|
|
use upend_base::entry::{Entry, EntryValue, ImmutableEntry};
|
|
|
|
impl TryFrom<&models::Entry> for Entry {
|
|
type Error = anyhow::Error;
|
|
|
|
fn try_from(e: &models::Entry) -> Result<Self, Self::Error> {
|
|
if let Some(value_str) = &e.value_str {
|
|
Ok(Entry {
|
|
entity: Address::decode(&e.entity)?,
|
|
attribute: e.attribute.clone(),
|
|
value: value_str.parse()?,
|
|
provenance: e.provenance.clone(),
|
|
timestamp: e.timestamp,
|
|
})
|
|
} else if let Some(value_num) = e.value_num {
|
|
Ok(Entry {
|
|
entity: Address::decode(&e.entity)?,
|
|
attribute: e.attribute.clone(),
|
|
value: EntryValue::Number(value_num),
|
|
provenance: e.provenance.clone(),
|
|
timestamp: e.timestamp,
|
|
})
|
|
} else {
|
|
Ok(Entry {
|
|
entity: Address::decode(&e.entity)?,
|
|
attribute: e.attribute.clone(),
|
|
value: EntryValue::Number(f64::NAN),
|
|
provenance: e.provenance.clone(),
|
|
timestamp: e.timestamp,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
impl TryFrom<&Entry> for models::Entry {
|
|
type Error = anyhow::Error;
|
|
|
|
fn try_from(e: &Entry) -> Result<Self, Self::Error> {
|
|
if e.attribute.is_empty() {
|
|
return Err(anyhow!("Attribute cannot be empty."));
|
|
}
|
|
let base_entry = models::Entry {
|
|
identity: e.address()?.encode()?,
|
|
entity_searchable: match &e.entity {
|
|
Address::Attribute(attr) => Some(attr.clone()),
|
|
Address::Url(url) => Some(url.to_string()),
|
|
_ => None,
|
|
},
|
|
entity: e.entity.encode()?,
|
|
attribute: e.attribute.clone(),
|
|
value_str: None,
|
|
value_num: None,
|
|
immutable: false,
|
|
provenance: e.provenance.clone(),
|
|
timestamp: e.timestamp,
|
|
};
|
|
|
|
match e.value {
|
|
EntryValue::Number(n) => Ok(models::Entry {
|
|
value_str: None,
|
|
value_num: Some(n),
|
|
..base_entry
|
|
}),
|
|
_ => Ok(models::Entry {
|
|
value_str: Some(e.value.to_string()?),
|
|
value_num: None,
|
|
..base_entry
|
|
}),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl TryFrom<&ImmutableEntry> for models::Entry {
|
|
type Error = anyhow::Error;
|
|
|
|
fn try_from(e: &ImmutableEntry) -> Result<Self, Self::Error> {
|
|
Ok(models::Entry {
|
|
immutable: true,
|
|
..models::Entry::try_from(&e.0)?
|
|
})
|
|
}
|
|
}
|