error handling for endings

This commit is contained in:
Max Bradbury 2020-10-18 12:27:54 +01:00
parent 7896ef1232
commit dba84e01fa
2 changed files with 11 additions and 11 deletions

View File

@ -1,6 +1,4 @@
use std::fmt;
use std::error::Error;
use std::str::FromStr;
// same as a dialogue basically
#[derive(Clone, Debug, Eq, PartialEq)]
@ -9,13 +7,14 @@ pub struct Ending {
pub dialogue: String,
}
impl Error for Ending {}
impl FromStr for Ending {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
impl Ending {
pub fn from_str(s: &str) -> Result<Self, crate::Error> {
let lines: Vec<&str> = s.lines().collect();
if lines.is_empty() || !lines[0].starts_with("END ") {
return Err(crate::Error::Ending);
}
let id = lines[0].replace("END ", "");
let dialogue = lines[1..].join("\n");
@ -32,7 +31,6 @@ impl fmt::Display for Ending {
#[cfg(test)]
mod test {
use crate::Ending;
use std::str::FromStr;
#[test]
fn ending_from_string() {

View File

@ -2,7 +2,6 @@ use crate::{Dialogue, Ending, Font, Image, Item, Palette, Room, Sprite, TextDire
use loe::TransformMode;
use std::str::FromStr;
use std::collections::HashMap;
use std::borrow::BorrowMut;
use std::fmt;
@ -226,8 +225,11 @@ impl Game {
} else if segment.starts_with("DLG ") {
dialogues.push(Dialogue::from(segment));
} else if segment.starts_with("END ") {
if let Ok(ending) = Ending::from_str(&segment) {
let result = Ending::from_str(&segment);
if let Ok(ending) = result {
endings.push(ending);
} else {
warnings.push(result.unwrap_err());
}
} else if segment.starts_with("VAR ") {
variables.push(Variable::from(segment));