variable functions

This commit is contained in:
Max Bradbury 2020-04-06 19:11:41 +01:00
parent 82a7bc0763
commit 11a04fb18f
2 changed files with 33 additions and 3 deletions

View File

@ -26,6 +26,8 @@ a library for parsing Bitsy game data.
* ending to * ending to
* dialogue from * dialogue from
* dialogue to * dialogue to
* variable from
* variable to
## todo ## todo
@ -33,8 +35,6 @@ a library for parsing Bitsy game data.
* room from * room from
* room to * room to
* variable from
* variable to
* game from * game from
* game to * game to

View File

@ -97,7 +97,7 @@ struct Room {
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
struct Variable { struct Variable {
id: String, id: String,
initial_value: i64, initial_value: String,
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -673,6 +673,36 @@ fn test_dialogue_to_string() {
); );
} }
fn variable_from_string(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 }
}
#[test]
fn test_variable_from_string() {
assert_eq!(
variable_from_string("VAR a\n42".to_string()),
Variable { id: "a".to_string(), initial_value: "42".to_string()}
);
}
fn variable_to_string(variable: Variable) -> String {
format!("VAR {}\n{}", variable.id, variable.initial_value)
}
#[test]
fn test_variable_to_string() {
let output = variable_to_string(
Variable { id: "c".to_string(), initial_value: "57".to_string() }
);
let expected = "VAR c\n57".to_string();
assert_eq!(output, expected);
}
fn room_from_string(string: String) -> Room { fn room_from_string(string: String) -> Room {
// todo handle room_format? // todo handle room_format?
let mut lines: Vec<&str> = string.split("\n").collect(); let mut lines: Vec<&str> = string.split("\n").collect();