1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//! The smallest units of data, consiting of a Subject, a Property and a Value

use crate::errors::AtomicResult;
use crate::storelike::Property;
use crate::values::Value;
use serde::Serialize;

/// The Atom is the (non-validated) string representation of a piece of data.
/// It's RichAtom sibling provides some extra methods.
#[derive(Clone, Debug, Serialize)]
pub struct Atom {
    pub subject: String,
    pub property: String,
    pub value: String,
}

impl Atom {
    pub fn new(subject: String, property: String, value: String) -> Self {
        Atom {
            subject,
            property,
            value,
        }
    }
}

/// A more heavyweight atom that is validated,
/// converted to a native value and has various property details.
#[derive(Clone, Debug, Serialize)]
pub struct RichAtom {
    pub subject: String,
    pub property: Property,
    pub value: String,
    pub native_value: Value,
}

impl RichAtom {
    pub fn new(subject: String, property: Property, value: String) -> AtomicResult<Self> {
        Ok(RichAtom {
            subject,
            property: property.clone(),
            value: value.clone(),
            native_value: Value::new(&value, &property.data_type)?,
        })
    }
}

impl From<RichAtom> for Atom {
    fn from(richatom: RichAtom) -> Self {
        Atom::new(
            richatom.subject,
            richatom.property.subject,
            richatom.value,
        )
    }
}