error handling for endings
This commit is contained in:
parent
7896ef1232
commit
dba84e01fa
|
@ -1,6 +1,4 @@
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::error::Error;
|
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
// same as a dialogue basically
|
// same as a dialogue basically
|
||||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
|
@ -9,13 +7,14 @@ pub struct Ending {
|
||||||
pub dialogue: String,
|
pub dialogue: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Error for Ending {}
|
impl Ending {
|
||||||
|
pub fn from_str(s: &str) -> Result<Self, crate::Error> {
|
||||||
impl FromStr for Ending {
|
|
||||||
type Err = String;
|
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
||||||
let lines: Vec<&str> = s.lines().collect();
|
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 id = lines[0].replace("END ", "");
|
||||||
let dialogue = lines[1..].join("\n");
|
let dialogue = lines[1..].join("\n");
|
||||||
|
|
||||||
|
@ -32,7 +31,6 @@ impl fmt::Display for Ending {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use crate::Ending;
|
use crate::Ending;
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn ending_from_string() {
|
fn ending_from_string() {
|
||||||
|
|
|
@ -2,7 +2,6 @@ use crate::{Dialogue, Ending, Font, Image, Item, Palette, Room, Sprite, TextDire
|
||||||
|
|
||||||
use loe::TransformMode;
|
use loe::TransformMode;
|
||||||
|
|
||||||
use std::str::FromStr;
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::borrow::BorrowMut;
|
use std::borrow::BorrowMut;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
@ -226,8 +225,11 @@ 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 ") {
|
||||||
if let Ok(ending) = Ending::from_str(&segment) {
|
let result = Ending::from_str(&segment);
|
||||||
|
if let Ok(ending) = result {
|
||||||
endings.push(ending);
|
endings.push(ending);
|
||||||
|
} else {
|
||||||
|
warnings.push(result.unwrap_err());
|
||||||
}
|
}
|
||||||
} else if segment.starts_with("VAR ") {
|
} else if segment.starts_with("VAR ") {
|
||||||
variables.push(Variable::from(segment));
|
variables.push(Variable::from(segment));
|
||||||
|
|
Loading…
Reference in New Issue