surrealml_core/storage/header/
output.rs

1//! Defines the struct housing data around the outputs of the model.
2use super::normalisers::wrapper::NormaliserType;
3use crate::{
4    safe_eject_option,
5    errors::error::{
6        SurrealError,
7        SurrealErrorStatus
8    }
9};
10
11
12/// Houses data around the outputs of the model.
13/// 
14/// # Fields
15/// * `name` - The name of the output.
16/// * `normaliser` - The normaliser to be applied to the output if there is one.
17#[derive(Debug, PartialEq)]
18pub struct Output {
19    pub name: Option<String>,
20    pub normaliser: Option<NormaliserType>,
21}
22
23impl Output {
24
25    /// Creates a new instance of the Output struct with no normaliser or name.
26    /// 
27    /// # Returns
28    /// A new instance of the Output struct with no normaliser or name.
29    pub fn fresh() -> Self {
30        Output {
31            name: None,
32            normaliser: None,
33        }
34    }
35
36    /// Creates a new instance of the Output struct without a normaliser.
37    /// 
38    /// # Arguments
39    /// * `name` - The name of the output.
40    pub fn new(name: String) -> Self {
41        Output {
42            name: Some(name),
43            normaliser: None,
44        }
45    }
46
47    /// Adds a normaliser to the output.
48    /// 
49    /// # Arguments
50    /// * `normaliser` - The normaliser to be applied to the output.
51    pub fn add_normaliser(&mut self, normaliser: NormaliserType) {
52        self.normaliser = Some(normaliser);
53    }
54
55    /// Converts the output struct to a string.
56    /// 
57    /// # Returns
58    /// * `String` - The output struct as a string.
59    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    /// Converts a string to an instance of the Output struct.
80    /// 
81    /// # Arguments
82    /// * `data` - The string to be converted into an instance of the Output struct.
83    /// 
84    /// # Returns
85    /// * `Output` - The string as an instance of the Output struct.
86    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        // with no normaliser
120        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}