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 ") {
|
||||
tiles.push(Tile::from(segment));
|
||||
} 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";
|
||||
|
||||
sprites.push(sprite);
|
||||
} else {
|
||||
warnings.push(result.unwrap_err());
|
||||
}
|
||||
} else if segment.starts_with("ITM ") {
|
||||
let result = Item::from_str(&segment);
|
||||
|
|
|
@ -54,8 +54,8 @@ impl Sprite {
|
|||
}
|
||||
|
||||
impl Sprite {
|
||||
pub fn from(string: String) -> Result<Sprite, crate::Error> {
|
||||
let mut lines: Vec<&str> = string.lines().collect();
|
||||
pub fn from_str(str: &str) -> Result<Sprite, crate::Error> {
|
||||
let mut lines: Vec<&str> = str.lines().collect();
|
||||
|
||||
if lines.is_empty() || !lines[0].starts_with("SPR ") {
|
||||
return Err(crate::Error::Sprite);
|
||||
|
@ -141,8 +141,7 @@ mod test {
|
|||
|
||||
#[test]
|
||||
fn sprite_from_string() {
|
||||
let string = include_str!("test-resources/sprite").to_string();
|
||||
let output = Sprite::from(string).unwrap();
|
||||
let output = Sprite::from_str(include_str!("test-resources/sprite")).unwrap();
|
||||
let expected = mock::sprite();
|
||||
|
||||
assert_eq!(output, expected);
|
||||
|
@ -150,6 +149,6 @@ mod test {
|
|||
|
||||
#[test]
|
||||
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