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
use crate::sketchbook::utils::assert_name_valid;
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Error, Formatter};

/// A type safe object for a Boolean variable of a `ModelState`.
///
/// Currently, it only stores the variable's `name` and `annotation`.
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Variable {
    name: String,
    annotation: String,
}

impl Variable {
    /// Create new `Variable` instance with an annotation.
    pub fn new_annotated(name_str: &str, annotation: &str) -> Result<Variable, String> {
        assert_name_valid(name_str)?;
        Ok(Variable {
            name: name_str.to_string(),
            annotation: annotation.to_string(),
        })
    }

    /// Create new `Variable` instance. Annotation is left empty.
    pub fn new(name_str: &str) -> Result<Variable, String> {
        Self::new_annotated(name_str, "")
    }

    /// Human-readable name of this variable.
    pub fn get_name(&self) -> &str {
        &self.name
    }

    /// Annotation of the variable.
    pub fn get_annotation(&self) -> &str {
        &self.annotation
    }

    /// Rename this variable.
    pub fn set_name(&mut self, new_name: &str) -> Result<(), String> {
        assert_name_valid(new_name)?;
        self.name = new_name.to_string();
        Ok(())
    }

    /// Change annotation of this variable.
    pub fn set_annotation(&mut self, annotation: &str) {
        self.annotation = annotation.to_string();
    }
}

impl Display for Variable {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
        write!(f, "{}", self.name)
    }
}

#[cfg(test)]
mod tests {
    use crate::sketchbook::model::Variable;

    #[test]
    fn test_variable() {
        let var_name = "v a r i a b l e 123".to_string();
        let mut var = Variable::new(var_name.as_str()).unwrap();

        assert_eq!(var.get_name(), &var_name);
        assert_eq!(var.to_string(), var_name);

        let new_name = "v a r 123".to_string();
        var.set_name(&new_name).unwrap();
        assert_eq!(var.get_name(), &new_name);
    }

    #[test]
    fn test_invalid_variable() {
        let var_name = "v\na\nr\n".to_string();
        let var = Variable::new(var_name.as_str());

        assert!(var.is_err());
    }
}