redo ending transformations

This commit is contained in:
Max Bradbury 2020-04-29 21:17:29 +01:00
parent e760027ae4
commit e9f1a1afcf
2 changed files with 22 additions and 10 deletions

View File

@ -1,3 +1,7 @@
use std::fmt;
use std::error::Error;
use std::str::FromStr;
// same as a dialogue basically // same as a dialogue basically
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
pub struct Ending { pub struct Ending {
@ -5,32 +9,36 @@ pub struct Ending {
dialogue: String, dialogue: String,
} }
impl From<String> for Ending { impl Error for Ending {}
#[inline]
fn from(string: String) -> Ending { impl FromStr for Ending {
let lines: Vec<&str> = string.lines().collect(); type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let lines: Vec<&str> = s.lines().collect();
let id = lines[0].replace("END ", "").to_string(); let id = lines[0].replace("END ", "").to_string();
let dialogue = lines[1..].join("\n"); let dialogue = lines[1..].join("\n");
Ending { id, dialogue } Ok(Ending { id, dialogue })
} }
} }
impl ToString for Ending { impl fmt::Display for Ending {
#[inline] #[inline]
fn to_string(&self) -> String { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
format!("END {}\n{}", self.id, self.dialogue) write!(f,"END {}\n{}", self.id, self.dialogue)
} }
} }
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::ending::Ending; use crate::ending::Ending;
use std::str::FromStr;
#[test] #[test]
fn test_ending_from_string() { fn test_ending_from_string() {
assert_eq!( assert_eq!(
Ending::from(include_str!("test-resources/ending").to_string()), Ending::from_str(include_str!("test-resources/ending")).unwrap(),
Ending { Ending {
id: "a".to_string(), id: "a".to_string(),
dialogue: "This is a long line of dialogue. Blah blah blah".to_string() dialogue: "This is a long line of dialogue. Blah blah blah".to_string()

View File

@ -1,6 +1,7 @@
use crate::{Dialogue, Ending, Font, Item, Palette, Room, Sprite, TextDirection, Tile, ToBase36, Variable, transform_line_endings, segments_from_string, is_string_numeric}; use crate::{Dialogue, Ending, Font, Item, Palette, Room, Sprite, TextDirection, Tile, ToBase36, Variable, transform_line_endings, segments_from_string, is_string_numeric};
use std::error::Error; use std::error::Error;
use loe::TransformMode; use loe::TransformMode;
use std::str::FromStr;
/// in very early versions of Bitsy, room tiles were defined as single characters /// in very early versions of Bitsy, room tiles were defined as single characters
/// so, only 36 tiles total. later versions are comma-separated /// so, only 36 tiles total. later versions are comma-separated
@ -169,7 +170,10 @@ impl Game {
} else if segment.starts_with("DLG ") { } else if segment.starts_with("DLG ") {
dialogues.push(Dialogue::from(segment)); dialogues.push(Dialogue::from(segment));
} else if segment.starts_with("END ") { } else if segment.starts_with("END ") {
endings.push(Ending::from(segment)); let ending = Ending::from_str(&segment);
if ending.is_ok() {
endings.push(ending.unwrap());
}
} else if segment.starts_with("VAR ") { } else if segment.starts_with("VAR ") {
variables.push(Variable::from(segment)); variables.push(Variable::from(segment));
} else if segment.starts_with("FONT ") { } else if segment.starts_with("FONT ") {