2020-04-12 13:20:53 +00:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
|
|
|
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-04-12 13:20:53 +00:00
|
|
|
let id = id_value[0].replace("VAR ", "").to_string();
|
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 {
|
|
|
|
#[inline]
|
|
|
|
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 {
|
|
|
|
use crate::variable::Variable;
|
2020-04-12 13:20:53 +00:00
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
#[test]
|
|
|
|
fn test_variable_from_string() {
|
|
|
|
assert_eq!(
|
|
|
|
Variable::from("VAR a\n42".to_string()),
|
|
|
|
Variable {
|
|
|
|
id: "a".to_string(),
|
|
|
|
initial_value: "42".to_string()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_variable_to_string() {
|
|
|
|
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
|
|
|
}
|