surrealml_core/storage/header/
output.rs1use super::normalisers::wrapper::NormaliserType;
3use crate::{
4 safe_eject_option,
5 errors::error::{
6 SurrealError,
7 SurrealErrorStatus
8 }
9};
10
11
12#[derive(Debug, PartialEq)]
18pub struct Output {
19 pub name: Option<String>,
20 pub normaliser: Option<NormaliserType>,
21}
22
23impl Output {
24
25 pub fn fresh() -> Self {
30 Output {
31 name: None,
32 normaliser: None,
33 }
34 }
35
36 pub fn new(name: String) -> Self {
41 Output {
42 name: Some(name),
43 normaliser: None,
44 }
45 }
46
47 pub fn add_normaliser(&mut self, normaliser: NormaliserType) {
52 self.normaliser = Some(normaliser);
53 }
54
55 pub fn to_string(&self) -> String {
60
61 if &self.name == &None && &self.normaliser == &None {
62 return "".to_string();
63 }
64
65 let name = match &self.name {
66 Some(name) => name.clone(),
67 None => "none".to_string(),
68 };
69 let mut buffer = vec![
70 name.clone(),
71 ];
72 match &self.normaliser {
73 Some(normaliser) => buffer.push(normaliser.to_string()),
74 None => buffer.push("none".to_string()),
75 }
76 buffer.join("=>")
77 }
78
79 pub fn from_string(data: String) -> Result<Self, SurrealError> {
87 if data.contains("=>") == false {
88 return Ok(Output::fresh())
89 }
90 let mut buffer = data.split("=>");
91
92 let name = safe_eject_option!(buffer.next());
93 let name = match name {
94 "none" => None,
95 _ => Some(name.to_string()),
96 };
97
98 let normaliser = safe_eject_option!(buffer.next());
99 let normaliser = match normaliser {
100 "none" => None,
101 _ => Some(NormaliserType::from_string(data).unwrap().0),
102 };
103 return Ok(Output {
104 name,
105 normaliser
106 })
107 }
108}
109
110
111#[cfg(test)]
112pub mod tests {
113
114 use super::*;
115
116 #[test]
117 fn test_output_to_string() {
118
119 let mut output = Output::new("test".to_string());
121 assert_eq!(output.to_string(), "test=>none");
122
123 let normaliser_data = "a=>linear_scaling(0.0,1.0)".to_string();
124 let normaliser = NormaliserType::from_string(normaliser_data).unwrap();
125
126 output.add_normaliser(normaliser.0);
127 assert_eq!(output.to_string(), "test=>linear_scaling(0,1)");
128 }
129
130 #[test]
131 fn test_from_string() {
132 let data = "test=>linear_scaling(0,1)".to_string();
133 let output = Output::from_string(data).unwrap();
134
135 assert_eq!(output.name.unwrap(), "test");
136 assert_eq!(output.normaliser.is_some(), true);
137 assert_eq!(output.normaliser.unwrap().to_string(), "linear_scaling(0,1)");
138 }
139
140 #[test]
141 fn test_from_string_with_no_normaliser() {
142 let data = "test=>none".to_string();
143 let output = Output::from_string(data).unwrap();
144
145 assert_eq!(output.name.unwrap(), "test");
146 assert_eq!(output.normaliser.is_none(), true);
147 }
148
149 #[test]
150 fn test_from_string_with_no_name() {
151 let data = "none=>none".to_string();
152 let output = Output::from_string(data).unwrap();
153
154 assert_eq!(output.name.is_none(), true);
155 assert_eq!(output.normaliser.is_none(), true);
156 }
157
158 #[test]
159 fn test_from_string_with_empty_string() {
160 let data = "".to_string();
161 let output = Output::from_string(data).unwrap();
162
163 assert_eq!(output.name.is_none(), true);
164 assert_eq!(output.normaliser.is_none(), true);
165 }
166
167 #[test]
168 fn test_to_string_with_no_data() {
169 let output = Output::fresh();
170 assert_eq!(output.to_string(), "");
171 }
172}