bitsy-parser/src/variable.rs

45 lines
1021 B
Rust
Raw Normal View History

2020-04-12 13:20:53 +00:00
#[derive(Debug, Eq, PartialEq)]
pub struct Variable {
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 {
let id_value: Vec<&str> = string.split('\n').collect();
let id = id_value[0].replace("VAR ", "").to_string();
let initial_value = id_value[1].to_string();
Variable { id, initial_value }
}
}
impl ToString for Variable {
#[inline]
fn to_string(&self) -> String {
format!("VAR {}\n{}", self.id, self.initial_value)
}
}
#[test]
fn test_variable_from_string() {
assert_eq!(
Variable::from("VAR a\n42".to_string()),
2020-04-18 15:58:30 +00:00
Variable {
id: "a".to_string(),
initial_value: "42".to_string()
}
2020-04-12 13:20:53 +00:00
);
}
#[test]
fn test_variable_to_string() {
2020-04-18 15:58:30 +00:00
let output = Variable {
id: "c".to_string(),
initial_value: "57".to_string(),
}
.to_string();
2020-04-12 13:20:53 +00:00
let expected = "VAR c\n57".to_string();
assert_eq!(output, expected);
}