dialogue functions

This commit is contained in:
Max Bradbury 2020-04-06 17:18:17 +01:00
parent 8b383bc982
commit 82a7bc0763
2 changed files with 35 additions and 2 deletions

View File

@ -24,13 +24,13 @@ a library for parsing Bitsy game data.
* exit to
* ending from
* ending to
* dialogue from
* dialogue to
## todo
### functions
* dialogue from
* dialogue to
* room from
* room to
* variable from

View File

@ -640,6 +640,39 @@ fn test_ending_to_string() {
);
}
fn dialogue_from_string(string: String) -> Dialogue {
let lines: Vec<&str> = string.split("\n").collect();
let id = lines[0].replace("DLG ", "").to_string();
let contents = lines[1..].join("\n");
Dialogue { id, contents }
}
#[test]
fn test_dialogue_from_string() {
assert_eq!(
dialogue_from_string("DLG h\nhello\ngoodbye".to_string()),
Dialogue { id: "h".to_string(), contents: "hello\ngoodbye".to_string()}
)
}
fn dialogue_to_string(dialogue: Dialogue) -> String {
format!("DLG {}\n{}", dialogue.id, dialogue.contents)
}
#[test]
fn test_dialogue_to_string() {
assert_eq!(
dialogue_to_string(
Dialogue {
id: "y".to_string(),
contents: "This is a bit of dialogue,\nblah blah\nblah blah".to_string()
}
),
"DLG y\nThis is a bit of dialogue,\nblah blah\nblah blah".to_string()
);
}
fn room_from_string(string: String) -> Room {
// todo handle room_format?
let mut lines: Vec<&str> = string.split("\n").collect();