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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//! The smallest units of data, consisting of a Subject, a Property and a Value
use crate::{
errors::AtomicResult,
values::{ReferenceString, SortableValue, Value},
};
/// The Atom is the smallest meaningful piece of data.
/// It describes how one value relates to a subject.
/// A [Resource] can be converted into a bunch of Atoms.
#[derive(Clone, Debug)]
pub struct Atom {
/// The URL where the resource is located
pub subject: String,
pub property: String,
pub value: Value,
}
impl Atom {
pub fn new(subject: String, property: String, value: Value) -> Self {
Atom {
subject,
property,
value,
}
}
/// If the Atom's Value is an Array, this will try to convert it into a set of Subjects.
/// Used for indexing.
pub fn values_to_subjects(&self) -> AtomicResult<Vec<String>> {
let base_path = format!("{} {}", self.subject, self.property);
self.value.to_subjects(Some(base_path))
}
/// Converts one Atom to a series of stringified values that can be indexed.
pub fn to_indexable_atoms(&self) -> Vec<IndexAtom> {
let sort_value = self.value.to_sortable_string();
let index_atoms = match &self.value.to_reference_index_strings() {
Some(v) => v,
None => return vec![],
}
.iter()
.map(|v| IndexAtom {
ref_value: v.into(),
sort_value: sort_value.clone(),
subject: self.subject.clone(),
property: self.property.clone(),
})
.collect();
index_atoms
}
}
/// Differs from a regular [Atom], since the value here is always a string,
/// and in the case of ResourceArrays, only a _single_ subject is used for each atom.
/// One IndexAtom for every member of the ResourceArray is created.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IndexAtom {
pub subject: String,
pub property: String,
pub ref_value: ReferenceString,
pub sort_value: SortableValue,
}
impl std::fmt::Display for Atom {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
fmt.write_str(&format!(
"<{}> <{}> '{}'",
self.subject, self.property, self.value
))?;
Ok(())
}
}