From ef310d6fb7be988e1ae11cdae782e6ff7bdfac58 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Thu, 30 Apr 2020 19:44:20 +0100 Subject: [PATCH] add dialogue name for 7.0 --- src/dialogue.rs | 43 ++++++++++++++++++++++++++++++++++--------- src/mock.rs | 2 ++ 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/dialogue.rs b/src/dialogue.rs index ad282ac..e243e6c 100644 --- a/src/dialogue.rs +++ b/src/dialogue.rs @@ -1,24 +1,39 @@ +use crate::optional_data_line; + #[derive(Debug, Eq, PartialEq)] pub struct Dialogue { pub id: String, pub contents: String, + pub name: Option, } impl From for Dialogue { #[inline] fn from(string: String) -> Dialogue { - let lines: Vec<&str> = string.lines().collect(); + let mut lines: Vec<&str> = string.lines().collect(); let id = lines[0].replace("DLG ", "").to_string(); + + let name = if lines.last().unwrap().starts_with("NAME ") { + Some(lines.pop().unwrap().replace("NAME ", "")) + } else { + None + }; + let contents = lines[1..].join("\n"); - Dialogue { id, contents } + Dialogue { id, contents, name } } } impl ToString for Dialogue { #[inline] fn to_string(&self) -> String { - format!("DLG {}\n{}", self.id, self.contents) + format!( + "DLG {}\n{}{}", + self.id, + self.contents, + optional_data_line("NAME", self.name.as_ref()) + ) } } @@ -28,19 +43,29 @@ mod test { #[test] fn test_dialogue_from_string() { - assert_eq!( - Dialogue::from("DLG h\nhello\ngoodbye".to_string()), - Dialogue { id: "h".to_string(), contents: "hello\ngoodbye".to_string()} - ) + let output = Dialogue::from( + "DLG h\nhello\nNAME not a dialogue name\nNAME a dialogue name".to_string() + ); + + let expected = Dialogue { + id: "h".to_string(), + contents: "hello\nNAME not a dialogue name".to_string(), + name: Some("a dialogue name".to_string()) + }; + + assert_eq!(output, expected); } #[test] fn test_dialogue_to_string() { let output = Dialogue { id: "y".to_string(), - contents: "This is a bit of dialogue,\nblah blah\nblah blah".to_string() + contents: "This is a bit of dialogue,\nblah blah\nblah blah".to_string(), + name: Some("a dialogue name".to_string()) }.to_string(); - let expected = "DLG y\nThis is a bit of dialogue,\nblah blah\nblah blah".to_string(); + + let expected = "DLG y\nThis is a bit of dialogue,\nblah blah\nblah blah\nNAME a dialogue name".to_string(); + assert_eq!(output, expected); } } diff --git a/src/mock.rs b/src/mock.rs index 9f81b64..6dbd0e0 100644 --- a/src/mock.rs +++ b/src/mock.rs @@ -816,10 +816,12 @@ pub fn game_default() -> Game { Dialogue { id: "SPR_0".to_string(), contents: "I'm a cat".to_string(), + name: None }, Dialogue { id: "ITM_0".to_string(), contents: "You found a nice warm cup of tea".to_string(), + name: None }, ], endings: vec![],