From b9fecbe8facbb2c9a6d567d66420d4aae287ba82 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Mon, 6 Apr 2020 14:11:56 +0100 Subject: [PATCH] ending functions --- README.md | 4 ++-- src/main.rs | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 01613b1..f628d28 100644 --- a/README.md +++ b/README.md @@ -22,13 +22,13 @@ a library for parsing Bitsy game data. * item to * exit from * exit to +* ending from +* ending to ## todo ### functions -* ending from -* ending to * dialogue from * dialogue to * room from diff --git a/src/main.rs b/src/main.rs index 9033fa0..1f63097 100644 --- a/src/main.rs +++ b/src/main.rs @@ -79,7 +79,8 @@ struct Exit { // same as a dialogue basically #[derive(Debug, Eq, PartialEq)] struct Ending { - contents: String, + id: String, + dialogue: String, } #[derive(Debug, Eq, PartialEq)] @@ -599,11 +600,37 @@ fn test_exit_to_string() { } fn ending_from_string(string: String) -> Ending { - Ending { contents: "".to_string() } + let string = string.replace("END ", ""); + let id_dialogue: Vec<&str> = string.split('\n').collect(); + let id = id_dialogue[0].to_string(); + let dialogue = id_dialogue[1].to_string(); + + Ending { id, dialogue } +} + +#[test] +fn test_ending_from_string() { + assert_eq!( + ending_from_string("END a\nThis is a long line of dialogue. Blah blah blah".to_string()), + Ending { id: "a".to_string(), dialogue: "This is a long line of dialogue. Blah blah blah".to_string() } + ); } fn ending_to_string(ending: Ending) -> String { - "".to_string() + format!("END {}\n{}", ending.id, ending.dialogue) +} + +#[test] +fn test_ending_to_string() { + assert_eq!( + ending_to_string( + Ending { + id: "7".to_string(), + dialogue: "This is another long ending. So long, farewell, etc.".to_string() + } + ), + "END 7\nThis is another long ending. So long, farewell, etc.".to_string() + ); } fn room_from_string(string: String) -> Room {