2020-05-31 15:12:23 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
2020-04-12 13:20:53 +00:00
|
|
|
pub struct Variable {
|
2020-04-12 16:13:08 +00:00
|
|
|
pub id: String,
|
|
|
|
pub initial_value: String,
|
2020-04-12 13:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<String> for Variable {
|
|
|
|
fn from(string: String) -> Variable {
|
2020-04-26 19:44:44 +00:00
|
|
|
let id_value: Vec<&str> = string.lines().collect();
|
2020-10-15 22:29:48 +00:00
|
|
|
let id = id_value[0].replace("VAR ", "");
|
2020-04-26 19:44:44 +00:00
|
|
|
|
|
|
|
let initial_value = if id_value.len() == 1 {
|
|
|
|
"".to_string()
|
|
|
|
} else {
|
|
|
|
id_value[1..].join("")
|
|
|
|
};
|
2020-04-12 13:20:53 +00:00
|
|
|
|
|
|
|
Variable { id, initial_value }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToString for Variable {
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
format!("VAR {}\n{}", self.id, self.initial_value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2020-10-09 10:57:02 +00:00
|
|
|
use crate::Variable;
|
2020-04-12 13:20:53 +00:00
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
#[test]
|
2020-07-26 11:37:41 +00:00
|
|
|
fn variable_from_string() {
|
2020-04-19 07:13:55 +00:00
|
|
|
assert_eq!(
|
|
|
|
Variable::from("VAR a\n42".to_string()),
|
|
|
|
Variable {
|
|
|
|
id: "a".to_string(),
|
|
|
|
initial_value: "42".to_string()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-07-26 11:37:41 +00:00
|
|
|
fn variable_to_string() {
|
2020-04-19 07:13:55 +00:00
|
|
|
let output = Variable {
|
|
|
|
id: "c".to_string(),
|
|
|
|
initial_value: "57".to_string(),
|
|
|
|
}
|
|
|
|
.to_string();
|
|
|
|
let expected = "VAR c\n57".to_string();
|
|
|
|
assert_eq!(output, expected);
|
2020-04-18 15:58:30 +00:00
|
|
|
}
|
2020-04-12 13:20:53 +00:00
|
|
|
}
|