transformation_file/state/
mod.rs

1use indexmap::IndexMap;
2use said::derivation::HashFunctionCode;
3use said::version::SerializationInfo;
4use said::{sad::SerializationFormats, sad::SAD};
5use serde::{Deserialize, Serialize};
6
7#[derive(SAD, Serialize, Debug, Deserialize, Clone)]
8#[version(protocol = "OCAT", major = 1, minor = 0)]
9// #[said(format = "JSON")]
10pub struct Transformation {
11    #[said]
12    #[serde(rename = "d")]
13    pub said: Option<said::SelfAddressingIdentifier>,
14    pub source: Option<String>,
15    pub target: Option<String>,
16    pub attributes: IndexMap<String, String>,
17}
18
19impl Default for Transformation {
20    fn default() -> Self {
21        Self::new()
22    }
23}
24
25impl Transformation {
26    pub fn new() -> Self {
27        Self {
28            said: None,
29            source: None,
30            target: None,
31            attributes: IndexMap::new(),
32        }
33    }
34
35    pub fn set_source(&mut self, source: String) {
36        self.source = Some(source);
37    }
38    pub fn set_target(&mut self, target: String) {
39        self.target = Some(target);
40    }
41
42    pub fn rename(&mut self, attributes: IndexMap<String, String>) {
43        attributes.into_iter().for_each(|(k, v)| {
44            self.attributes.insert(k, v);
45        });
46    }
47
48    pub fn link(&mut self, attributes: IndexMap<String, String>) {
49        attributes.into_iter().for_each(|(k, v)| {
50            self.attributes.insert(k, v);
51        });
52    }
53
54    pub fn fill_said(&mut self) {
55        let code = HashFunctionCode::Blake3_256;
56        let format = SerializationFormats::JSON;
57        self.compute_digest(&code, &format);
58    }
59}