error handling for Item
This commit is contained in:
parent
68ecc64c7b
commit
889328f9a9
|
@ -221,7 +221,13 @@ impl Game {
|
||||||
sprites.push(sprite);
|
sprites.push(sprite);
|
||||||
}
|
}
|
||||||
} else if segment.starts_with("ITM ") {
|
} else if segment.starts_with("ITM ") {
|
||||||
items.push(Item::from(segment));
|
let result = Item::from_str(&segment);
|
||||||
|
|
||||||
|
if let Ok(item) = result {
|
||||||
|
items.push(item);
|
||||||
|
} else {
|
||||||
|
warnings.push(result.unwrap_err());
|
||||||
|
}
|
||||||
} else if segment.starts_with("DLG ") {
|
} else if segment.starts_with("DLG ") {
|
||||||
let result = Dialogue::from_str(&segment);
|
let result = Dialogue::from_str(&segment);
|
||||||
|
|
||||||
|
|
28
src/item.rs
28
src/item.rs
|
@ -1,5 +1,6 @@
|
||||||
use crate::{optional_data_line, AnimationFrames, Image};
|
use crate::{optional_data_line, AnimationFrames, Image};
|
||||||
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 Item {
|
pub struct Item {
|
||||||
|
@ -22,11 +23,13 @@ impl Item {
|
||||||
fn colour_line(&self) -> String {
|
fn colour_line(&self) -> String {
|
||||||
optional_data_line("COL", self.colour_id.as_ref())
|
optional_data_line("COL", self.colour_id.as_ref())
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl From<String> for Item {
|
pub fn from_str(str: &str) -> Result<Item, crate::Error> {
|
||||||
fn from(string: String) -> Item {
|
let mut lines: Vec<&str> = str.lines().collect();
|
||||||
let mut lines: Vec<&str> = string.lines().collect();
|
|
||||||
|
if lines.is_empty() || !lines[0].starts_with("ITM ") {
|
||||||
|
return Err(crate::Error::Item);
|
||||||
|
}
|
||||||
|
|
||||||
let id = lines[0].replace("ITM ", "");
|
let id = lines[0].replace("ITM ", "");
|
||||||
let mut name = None;
|
let mut name = None;
|
||||||
|
@ -52,19 +55,14 @@ impl From<String> for Item {
|
||||||
&lines[1..].join("\n")
|
&lines[1..].join("\n")
|
||||||
);
|
);
|
||||||
|
|
||||||
Item {
|
Ok(Item { id, name, animation_frames, dialogue_id, colour_id })
|
||||||
id,
|
|
||||||
name,
|
|
||||||
animation_frames,
|
|
||||||
dialogue_id,
|
|
||||||
colour_id,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToString for Item {
|
impl fmt::Display for Item {
|
||||||
fn to_string(&self) -> String {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
format!(
|
write!(
|
||||||
|
f,
|
||||||
"ITM {}\n{}{}{}{}",
|
"ITM {}\n{}{}{}{}",
|
||||||
self.id,
|
self.id,
|
||||||
self.animation_frames.to_string(),
|
self.animation_frames.to_string(),
|
||||||
|
@ -81,7 +79,7 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn item_from_string() {
|
fn item_from_string() {
|
||||||
let output = Item::from(include_str!("test-resources/item").to_string());
|
let output = Item::from_str(include_str!("test-resources/item")).unwrap();
|
||||||
let expected = mock::item();
|
let expected = mock::item();
|
||||||
assert_eq!(output, expected);
|
assert_eq!(output, expected);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue