sprite: from_str and error handling
This commit is contained in:
parent
8f558a908f
commit
e9738b98b1
|
@ -213,12 +213,14 @@ impl Game {
|
||||||
} else if segment.starts_with("TIL ") {
|
} else if segment.starts_with("TIL ") {
|
||||||
tiles.push(Tile::from(segment));
|
tiles.push(Tile::from(segment));
|
||||||
} else if segment.starts_with("SPR ") {
|
} else if segment.starts_with("SPR ") {
|
||||||
let sprite = Sprite::from(segment);
|
let result = Sprite::from_str(&segment);
|
||||||
|
|
||||||
if let Ok(sprite) = sprite {
|
if let Ok(sprite) = result {
|
||||||
// avatar_exists |= sprite.id == "A";
|
// avatar_exists |= sprite.id == "A";
|
||||||
|
|
||||||
sprites.push(sprite);
|
sprites.push(sprite);
|
||||||
|
} else {
|
||||||
|
warnings.push(result.unwrap_err());
|
||||||
}
|
}
|
||||||
} else if segment.starts_with("ITM ") {
|
} else if segment.starts_with("ITM ") {
|
||||||
let result = Item::from_str(&segment);
|
let result = Item::from_str(&segment);
|
||||||
|
|
|
@ -54,8 +54,8 @@ impl Sprite {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Sprite {
|
impl Sprite {
|
||||||
pub fn from(string: String) -> Result<Sprite, crate::Error> {
|
pub fn from_str(str: &str) -> Result<Sprite, crate::Error> {
|
||||||
let mut lines: Vec<&str> = string.lines().collect();
|
let mut lines: Vec<&str> = str.lines().collect();
|
||||||
|
|
||||||
if lines.is_empty() || !lines[0].starts_with("SPR ") {
|
if lines.is_empty() || !lines[0].starts_with("SPR ") {
|
||||||
return Err(crate::Error::Sprite);
|
return Err(crate::Error::Sprite);
|
||||||
|
@ -141,8 +141,7 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn sprite_from_string() {
|
fn sprite_from_string() {
|
||||||
let string = include_str!("test-resources/sprite").to_string();
|
let output = Sprite::from_str(include_str!("test-resources/sprite")).unwrap();
|
||||||
let output = Sprite::from(string).unwrap();
|
|
||||||
let expected = mock::sprite();
|
let expected = mock::sprite();
|
||||||
|
|
||||||
assert_eq!(output, expected);
|
assert_eq!(output, expected);
|
||||||
|
@ -150,6 +149,6 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn sprite_to_string() {
|
fn sprite_to_string() {
|
||||||
assert_eq!(mock::sprite().to_string(), include_str!("test-resources/sprite").to_string());
|
assert_eq!(mock::sprite().to_string(), include_str!("test-resources/sprite"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue