diff --git a/README.md b/README.md index f628d28..f027ed7 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/main.rs b/src/main.rs index 3460100..dade814 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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();