1use std::{
2 convert::TryFrom,
3 fmt::{self, Display},
4 num::ParseIntError,
5 ops::{Deref, DerefMut},
6 str::FromStr,
7};
8
9#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
14pub struct Pic(pub isize);
15
16impl Pic {
17 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 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}