2021-07-26 21:00:05 +02:00
|
|
|
use crate::addressing::{Address, Addressable};
|
2021-07-26 21:14:12 +02:00
|
|
|
use crate::database::inner::models;
|
2022-01-26 16:55:23 +01:00
|
|
|
use crate::util::hash::{b58_decode, hash, Hash, Hashable};
|
2021-07-26 21:00:05 +02:00
|
|
|
use anyhow::{anyhow, Result};
|
2022-03-01 21:00:29 +01:00
|
|
|
use regex::Regex;
|
2021-07-26 21:00:05 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-02-12 14:51:28 +01:00
|
|
|
use std::convert::TryFrom;
|
2021-07-26 21:00:05 +02:00
|
|
|
use std::io::{Cursor, Write};
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
pub struct Entry {
|
|
|
|
pub entity: Address,
|
|
|
|
pub attribute: String,
|
|
|
|
pub value: EntryValue,
|
|
|
|
}
|
|
|
|
|
2022-01-21 17:59:53 +01:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct ImmutableEntry(pub Entry);
|
|
|
|
|
2021-07-26 21:00:05 +02:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
pub struct InvariantEntry {
|
|
|
|
pub attribute: String,
|
|
|
|
pub value: EntryValue,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
|
|
#[serde(tag = "t", content = "c")]
|
|
|
|
pub enum EntryValue {
|
2022-01-28 18:17:14 +01:00
|
|
|
String(String),
|
|
|
|
Number(f64),
|
2021-07-26 21:00:05 +02:00
|
|
|
Address(Address),
|
2022-02-19 14:59:13 +01:00
|
|
|
Null,
|
2021-07-26 21:00:05 +02:00
|
|
|
Invalid,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<&models::Entry> for Entry {
|
|
|
|
type Error = anyhow::Error;
|
|
|
|
|
|
|
|
fn try_from(e: &models::Entry) -> Result<Self, Self::Error> {
|
2022-01-28 18:17:14 +01:00
|
|
|
if let Some(value_str) = &e.value_str {
|
|
|
|
Ok(Entry {
|
|
|
|
entity: Address::decode(&e.entity)?,
|
|
|
|
attribute: e.attribute.clone(),
|
|
|
|
value: value_str.parse()?,
|
|
|
|
})
|
|
|
|
} 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),
|
|
|
|
})
|
|
|
|
} else {
|
2022-03-18 22:22:32 +01:00
|
|
|
Ok(Entry {
|
|
|
|
entity: Address::decode(&e.entity)?,
|
|
|
|
attribute: e.attribute.clone(),
|
|
|
|
value: EntryValue::Number(f64::NAN),
|
|
|
|
})
|
2022-01-28 18:17:14 +01:00
|
|
|
}
|
2021-07-26 21:00:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<&Entry> for models::Entry {
|
|
|
|
type Error = anyhow::Error;
|
|
|
|
|
|
|
|
fn try_from(e: &Entry) -> Result<Self, Self::Error> {
|
2022-02-19 17:30:55 +01:00
|
|
|
if e.attribute.is_empty() {
|
|
|
|
return Err(anyhow!("Attribute cannot be empty."));
|
|
|
|
}
|
2022-02-07 18:25:17 +01:00
|
|
|
let base_entry = models::Entry {
|
|
|
|
identity: e.address()?.encode()?,
|
2022-02-07 18:33:57 +01:00
|
|
|
entity_searchable: match &e.entity {
|
|
|
|
Address::Attribute(attr) => Some(attr.clone()),
|
|
|
|
Address::Url(url) => Some(url.clone()),
|
|
|
|
_ => None,
|
|
|
|
},
|
2022-02-07 18:25:17 +01:00
|
|
|
entity: e.entity.encode()?,
|
|
|
|
attribute: e.attribute.clone(),
|
|
|
|
value_str: None,
|
|
|
|
value_num: None,
|
|
|
|
immutable: false,
|
|
|
|
};
|
|
|
|
|
2022-01-28 18:17:14 +01:00
|
|
|
match e.value {
|
|
|
|
EntryValue::Number(n) => Ok(models::Entry {
|
|
|
|
value_str: None,
|
|
|
|
value_num: Some(n),
|
2022-02-07 18:25:17 +01:00
|
|
|
..base_entry
|
2022-01-28 18:17:14 +01:00
|
|
|
}),
|
|
|
|
_ => Ok(models::Entry {
|
|
|
|
value_str: Some(e.value.to_string()?),
|
|
|
|
value_num: None,
|
2022-02-07 18:25:17 +01:00
|
|
|
..base_entry
|
2022-01-28 18:17:14 +01:00
|
|
|
}),
|
|
|
|
}
|
2022-01-21 17:59:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<&ImmutableEntry> for models::Entry {
|
|
|
|
type Error = anyhow::Error;
|
|
|
|
|
|
|
|
fn try_from(e: &ImmutableEntry) -> Result<Self, Self::Error> {
|
2022-02-07 18:25:17 +01:00
|
|
|
Ok(models::Entry {
|
|
|
|
immutable: true,
|
|
|
|
..models::Entry::try_from(&e.0)?
|
|
|
|
})
|
2021-07-26 21:00:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<&InvariantEntry> for Entry {
|
|
|
|
type Error = anyhow::Error;
|
|
|
|
|
|
|
|
fn try_from(invariant: &InvariantEntry) -> Result<Self, Self::Error> {
|
|
|
|
Ok(Entry {
|
|
|
|
entity: invariant.entity()?,
|
|
|
|
attribute: invariant.attribute.clone(),
|
|
|
|
value: invariant.value.clone(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl InvariantEntry {
|
|
|
|
pub fn entity(&self) -> Result<Address> {
|
|
|
|
let mut entity = Cursor::new(vec![0u8; 0]);
|
|
|
|
entity.write_all(self.attribute.as_bytes())?;
|
|
|
|
entity.write_all(self.value.to_string()?.as_bytes())?;
|
|
|
|
Ok(Address::Hash(hash(entity.into_inner())))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for Entry {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "{} | {} | {}", self.entity, self.attribute, self.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Hashable for Entry {
|
|
|
|
fn hash(self: &Entry) -> Result<Hash> {
|
|
|
|
let mut result = Cursor::new(vec![0u8; 0]);
|
|
|
|
result.write_all(self.entity.encode()?.as_slice())?;
|
|
|
|
result.write_all(self.attribute.as_bytes())?;
|
|
|
|
result.write_all(self.value.to_string()?.as_bytes())?;
|
|
|
|
Ok(hash(result.get_ref()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Hashable for InvariantEntry {
|
|
|
|
fn hash(&self) -> Result<Hash> {
|
|
|
|
Entry::try_from(self)?.hash()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Addressable for Entry {}
|
|
|
|
impl Addressable for InvariantEntry {}
|
|
|
|
|
|
|
|
impl EntryValue {
|
|
|
|
pub fn to_string(&self) -> Result<String> {
|
|
|
|
let (type_char, content) = match self {
|
2022-01-28 18:17:14 +01:00
|
|
|
EntryValue::String(value) => ('S', value.to_owned()),
|
|
|
|
EntryValue::Number(n) => ('N', n.to_string()),
|
2021-07-26 21:00:05 +02:00
|
|
|
EntryValue::Address(address) => ('O', address.to_string()),
|
2022-02-19 14:59:13 +01:00
|
|
|
EntryValue::Null => ('X', "".to_string()),
|
2022-02-13 12:28:15 +01:00
|
|
|
EntryValue::Invalid => return Err(anyhow!("Cannot serialize invalid value.")),
|
2021-07-26 21:00:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(format!("{}{}", type_char, content))
|
|
|
|
}
|
2022-03-01 21:00:29 +01:00
|
|
|
|
|
|
|
pub fn guess_from<S: AsRef<str>>(string: S) -> Self {
|
|
|
|
let string = string.as_ref();
|
|
|
|
match string.parse::<f64>() {
|
|
|
|
Ok(num) => EntryValue::Number(num),
|
|
|
|
Err(_) => {
|
|
|
|
lazy_static! {
|
|
|
|
static ref URL_REGEX: Regex = Regex::new("^[a-zA-Z0-9_]://").unwrap();
|
|
|
|
}
|
|
|
|
if URL_REGEX.is_match(string) {
|
|
|
|
EntryValue::Address(Address::Url(string.to_string()))
|
|
|
|
} else {
|
|
|
|
EntryValue::String(string.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-26 21:00:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl std::str::FromStr for EntryValue {
|
|
|
|
type Err = std::convert::Infallible;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
if s.len() < 2 {
|
2022-02-19 14:59:13 +01:00
|
|
|
match s.chars().next() {
|
|
|
|
Some('S') => Ok(EntryValue::String("".into())),
|
|
|
|
Some('X') => Ok(EntryValue::Null),
|
|
|
|
_ => Ok(EntryValue::Invalid),
|
2022-02-13 12:28:15 +01:00
|
|
|
}
|
2021-07-26 21:00:05 +02:00
|
|
|
} else {
|
|
|
|
let (type_char, content) = s.split_at(1);
|
|
|
|
match (type_char, content) {
|
2022-01-28 18:17:14 +01:00
|
|
|
("S", content) => Ok(EntryValue::String(String::from(content))),
|
|
|
|
("N", content) => {
|
|
|
|
if let Ok(n) = content.parse::<f64>() {
|
|
|
|
Ok(EntryValue::Number(n))
|
2021-07-26 21:00:05 +02:00
|
|
|
} else {
|
|
|
|
Ok(EntryValue::Invalid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
("O", content) => {
|
2022-01-26 16:55:23 +01:00
|
|
|
if let Ok(addr) = b58_decode(content).and_then(|v| Address::decode(&v)) {
|
2021-07-26 21:00:05 +02:00
|
|
|
Ok(EntryValue::Address(addr))
|
|
|
|
} else {
|
|
|
|
Ok(EntryValue::Invalid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => Ok(EntryValue::Invalid),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-28 18:17:14 +01:00
|
|
|
|
2022-02-13 12:37:16 +01:00
|
|
|
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()),
|
2022-02-19 14:59:13 +01:00
|
|
|
EntryValue::Null => ("NULL", "NULL".to_string()),
|
2022-02-13 12:37:16 +01:00
|
|
|
EntryValue::Invalid => ("INVALID", "INVALID".to_string()),
|
|
|
|
};
|
|
|
|
write!(f, "{}: {}", entry_type, entry_value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-13 12:55:49 +01:00
|
|
|
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)
|
2022-02-10 11:38:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-28 18:17:14 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use anyhow::Result;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_value_from_to_string() -> Result<()> {
|
|
|
|
let entry = EntryValue::String("hello".to_string());
|
|
|
|
let encoded = entry.to_string()?;
|
|
|
|
let decoded = encoded.parse::<EntryValue>()?;
|
|
|
|
assert_eq!(entry, decoded);
|
|
|
|
|
|
|
|
let entry = EntryValue::Number(1337.93);
|
|
|
|
let encoded = entry.to_string()?;
|
|
|
|
let decoded = encoded.parse::<EntryValue>()?;
|
|
|
|
assert_eq!(entry, decoded);
|
|
|
|
|
|
|
|
let entry = EntryValue::Address(Address::Url("https://upendproject.net".to_string()));
|
|
|
|
let encoded = entry.to_string()?;
|
|
|
|
let decoded = encoded.parse::<EntryValue>()?;
|
|
|
|
assert_eq!(entry, decoded);
|
|
|
|
|
2022-02-13 12:28:15 +01:00
|
|
|
let entry = EntryValue::String("".to_string());
|
|
|
|
let encoded = entry.to_string()?;
|
|
|
|
let decoded = encoded.parse::<EntryValue>()?;
|
|
|
|
assert_eq!(entry, decoded);
|
|
|
|
|
2022-02-19 14:59:13 +01:00
|
|
|
let entry = EntryValue::Null;
|
|
|
|
let encoded = entry.to_string()?;
|
|
|
|
let decoded = encoded.parse::<EntryValue>()?;
|
|
|
|
assert_eq!(entry, decoded);
|
|
|
|
|
2022-01-28 18:17:14 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-02-13 12:55:49 +01:00
|
|
|
|
|
|
|
#[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());
|
|
|
|
}
|
2022-01-28 18:17:14 +01:00
|
|
|
}
|