better error handling for Sprite
This commit is contained in:
parent
fb290f07f4
commit
8f558a908f
|
@ -1,6 +1,8 @@
|
||||||
use crate::{optional_data_line, AnimationFrames, Image, Position};
|
use crate::{optional_data_line, AnimationFrames, Image, Position};
|
||||||
use crate::image::animation_frames_from_str;
|
use crate::image::animation_frames_from_str;
|
||||||
|
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
pub struct Sprite {
|
pub struct Sprite {
|
||||||
pub id: String,
|
pub id: String,
|
||||||
|
@ -51,14 +53,14 @@ impl Sprite {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct SpriteMissingRoomPosition;
|
|
||||||
// todo "malformed sprite ID" or something
|
|
||||||
|
|
||||||
impl Sprite {
|
impl Sprite {
|
||||||
pub(crate) fn from(string: String) -> Result<Sprite, SpriteMissingRoomPosition> {
|
pub fn from(string: String) -> Result<Sprite, crate::Error> {
|
||||||
let mut lines: Vec<&str> = string.lines().collect();
|
let mut lines: Vec<&str> = string.lines().collect();
|
||||||
|
|
||||||
|
if lines.is_empty() || !lines[0].starts_with("SPR ") {
|
||||||
|
return Err(crate::Error::Sprite);
|
||||||
|
}
|
||||||
|
|
||||||
let id = lines[0].replace("SPR ", "");
|
let id = lines[0].replace("SPR ", "");
|
||||||
let mut name = None;
|
let mut name = None;
|
||||||
let mut dialogue_id: Option<String> = None;
|
let mut dialogue_id: Option<String> = None;
|
||||||
|
@ -80,13 +82,13 @@ impl Sprite {
|
||||||
room_id = Some(room_position[0].to_string());
|
room_id = Some(room_position[0].to_string());
|
||||||
|
|
||||||
if room_position.len() < 2 {
|
if room_position.len() < 2 {
|
||||||
return Err(SpriteMissingRoomPosition);
|
return Err(crate::Error::Sprite);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Ok(pos) = Position::from_str(room_position[1]) {
|
if let Ok(pos) = Position::from_str(room_position[1]) {
|
||||||
position = Some(pos);
|
position = Some(pos);
|
||||||
} else {
|
} else {
|
||||||
return Err(SpriteMissingRoomPosition);
|
return Err(crate::Error::Sprite);
|
||||||
}
|
}
|
||||||
} else if last_line.starts_with("COL") {
|
} else if last_line.starts_with("COL") {
|
||||||
colour_id = Some(last_line.replace("COL ", "").parse().unwrap());
|
colour_id = Some(last_line.replace("COL ", "").parse().unwrap());
|
||||||
|
@ -117,9 +119,10 @@ impl Sprite {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToString for Sprite {
|
impl fmt::Display for Sprite {
|
||||||
fn to_string(&self) -> String {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
format!(
|
write!(
|
||||||
|
f,
|
||||||
"SPR {}\n{}{}{}{}{}{}",
|
"SPR {}\n{}{}{}{}{}{}",
|
||||||
self.id,
|
self.id,
|
||||||
self.animation_frames.to_string(),
|
self.animation_frames.to_string(),
|
||||||
|
|
Loading…
Reference in New Issue