lf2_parse/frame/
pic.rs

1use std::{
2    convert::TryFrom,
3    fmt::{self, Display},
4    num::ParseIntError,
5    ops::{Deref, DerefMut},
6    str::FromStr,
7};
8
9/// Represents the sprite number to use.
10///
11/// This uses an `isize` as a negative number indicates the sprite should be
12/// flipped.
13#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
14pub struct Pic(pub isize);
15
16impl Pic {
17    /// Returns the absolute value of this pic number.
18    pub fn abs(self) -> usize {
19        TryFrom::<isize>::try_from(self.0.abs()).unwrap_or_else(|e| {
20            panic!("Failed to convert `Pic` to `usize`. {}", e);
21        })
22    }
23
24    /// Returns `true` if the object's facing direction should change.
25    pub fn facing_switch(self) -> bool {
26        self.0 < 0
27    }
28}
29
30impl Deref for Pic {
31    type Target = isize;
32
33    fn deref(&self) -> &Self::Target {
34        &self.0
35    }
36}
37
38impl DerefMut for Pic {
39    fn deref_mut(&mut self) -> &mut Self::Target {
40        &mut self.0
41    }
42}
43
44impl Display for Pic {
45    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
46        write!(f, "{}", self.0)
47    }
48}
49
50impl FromStr for Pic {
51    type Err = ParseIntError;
52
53    fn from_str(s: &str) -> Result<Pic, ParseIntError> {
54        s.parse::<isize>().map(Self)
55    }
56}