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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//! Defines the struct housing data around the outputs of the model.
use super::normalisers::wrapper::NormaliserType;
use crate::{
    safe_eject_option,
    errors::error::{
        SurrealError,
        SurrealErrorStatus
    }
};


/// Houses data around the outputs of the model.
/// 
/// # Fields
/// * `name` - The name of the output.
/// * `normaliser` - The normaliser to be applied to the output if there is one.
#[derive(Debug, PartialEq)]
pub struct Output {
    pub name: Option<String>,
    pub normaliser: Option<NormaliserType>,
}

impl Output {

    /// Creates a new instance of the Output struct with no normaliser or name.
    /// 
    /// # Returns
    /// A new instance of the Output struct with no normaliser or name.
    pub fn fresh() -> Self {
        Output {
            name: None,
            normaliser: None,
        }
    }

    /// Creates a new instance of the Output struct without a normaliser.
    /// 
    /// # Arguments
    /// * `name` - The name of the output.
    pub fn new(name: String) -> Self {
        Output {
            name: Some(name),
            normaliser: None,
        }
    }

    /// Adds a normaliser to the output.
    /// 
    /// # Arguments
    /// * `normaliser` - The normaliser to be applied to the output.
    pub fn add_normaliser(&mut self, normaliser: NormaliserType) {
        self.normaliser = Some(normaliser);
    }

    /// Converts the output struct to a string.
    /// 
    /// # Returns
    /// * `String` - The output struct as a string.
    pub fn to_string(&self) -> String {

        if &self.name == &None && &self.normaliser == &None {
            return "".to_string();
        }

        let name = match &self.name {
            Some(name) => name.clone(),
            None => "none".to_string(),
        };
        let mut buffer = vec![
            name.clone(),
        ];
        match &self.normaliser {
            Some(normaliser) => buffer.push(normaliser.to_string()),
            None => buffer.push("none".to_string()),
        }
        buffer.join("=>")
    }

    /// Converts a string to an instance of the Output struct.
    /// 
    /// # Arguments
    /// * `data` - The string to be converted into an instance of the Output struct.
    /// 
    /// # Returns
    /// * `Output` - The string as an instance of the Output struct.
    pub fn from_string(data: String) -> Result<Self, SurrealError> {
        if data.contains("=>") == false {
            return Ok(Output::fresh())
        }
        let mut buffer = data.split("=>");

        let name = safe_eject_option!(buffer.next());
        let name = match name {
            "none" => None,
            _ => Some(name.to_string()),
        };

        let normaliser = safe_eject_option!(buffer.next());
        let normaliser = match normaliser {
            "none" => None,
            _ => Some(NormaliserType::from_string(data).unwrap().0),
        };
        return Ok(Output {
            name,
            normaliser
        })
    }
}


#[cfg(test)]
pub mod tests {

    use super::*;

    #[test]
    fn test_output_to_string() {

        // with no normaliser
        let mut output = Output::new("test".to_string());
        assert_eq!(output.to_string(), "test=>none");

        let normaliser_data = "a=>linear_scaling(0.0,1.0)".to_string();
        let normaliser = NormaliserType::from_string(normaliser_data).unwrap();
        
        output.add_normaliser(normaliser.0);
        assert_eq!(output.to_string(), "test=>linear_scaling(0,1)");
    }

    #[test]
    fn test_from_string() {
        let data = "test=>linear_scaling(0,1)".to_string();
        let output = Output::from_string(data).unwrap();

        assert_eq!(output.name.unwrap(), "test");
        assert_eq!(output.normaliser.is_some(), true);
        assert_eq!(output.normaliser.unwrap().to_string(), "linear_scaling(0,1)");
    }

    #[test]
    fn test_from_string_with_no_normaliser() {
        let data = "test=>none".to_string();
        let output = Output::from_string(data).unwrap();

        assert_eq!(output.name.unwrap(), "test");
        assert_eq!(output.normaliser.is_none(), true);
    }

    #[test]
    fn test_from_string_with_no_name() {
        let data = "none=>none".to_string();
        let output = Output::from_string(data).unwrap();

        assert_eq!(output.name.is_none(), true);
        assert_eq!(output.normaliser.is_none(), true);
    }

    #[test]
    fn test_from_string_with_empty_string() {
        let data = "".to_string();
        let output = Output::from_string(data).unwrap();

        assert_eq!(output.name.is_none(), true);
        assert_eq!(output.normaliser.is_none(), true);
    }

    #[test]
    fn test_to_string_with_no_data() {
        let output = Output::fresh();
        assert_eq!(output.to_string(), "");
    }
}