From 11a04fb18f5de9adb05450fdfec98dace1686dbb Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Mon, 6 Apr 2020 19:11:41 +0100 Subject: [PATCH] variable functions --- README.md | 4 ++-- src/main.rs | 32 +++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f027ed7..1b9bc7c 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,8 @@ a library for parsing Bitsy game data. * ending to * dialogue from * dialogue to +* variable from +* variable to ## todo @@ -33,8 +35,6 @@ a library for parsing Bitsy game data. * room from * room to -* variable from -* variable to * game from * game to diff --git a/src/main.rs b/src/main.rs index dade814..cc01a35 100644 --- a/src/main.rs +++ b/src/main.rs @@ -97,7 +97,7 @@ struct Room { #[derive(Debug, Eq, PartialEq)] struct Variable { id: String, - initial_value: i64, + initial_value: String, } #[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 { // todo handle room_format? let mut lines: Vec<&str> = string.split("\n").collect();