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
#[derive(Debug, Eq, PartialEq)]
pub struct Ending {
@ -5,32 +9,36 @@ pub struct Ending {
dialogue: String,
}
impl From<String> for Ending {
#[inline]
fn from(string: String) -> Ending {
let lines: Vec<&str> = string.lines().collect();
impl Error for Ending {}
impl FromStr for Ending {
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 dialogue = lines[1..].join("\n");
Ending { id, dialogue }
Ok(Ending { id, dialogue })
}
}
impl ToString for Ending {
impl fmt::Display for Ending {
#[inline]
fn to_string(&self) -> String {
format!("END {}\n{}", self.id, self.dialogue)
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f,"END {}\n{}", self.id, self.dialogue)
}
}
#[cfg(test)]
mod test {
use crate::ending::Ending;
use std::str::FromStr;
#[test]
fn test_ending_from_string() {
assert_eq!(
Ending::from(include_str!("test-resources/ending").to_string()),
Ending::from_str(include_str!("test-resources/ending")).unwrap(),
Ending {
id: "a".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 std::error::Error;
use loe::TransformMode;
use std::str::FromStr;
/// in very early versions of Bitsy, room tiles were defined as single characters
/// so, only 36 tiles total. later versions are comma-separated
@ -169,7 +170,10 @@ impl Game {
} else if segment.starts_with("DLG ") {
dialogues.push(Dialogue::from(segment));
} 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 ") {
variables.push(Variable::from(segment));
} else if segment.starts_with("FONT ") {