timsrust_core/coordinates/
charge.rs1use crate::CoordinateError;
2
3#[derive(Debug, Clone, Copy, PartialOrd, PartialEq)]
4pub struct Charge(std::num::NonZeroI8);
5
6impl std::fmt::Display for Charge {
7 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
8 write!(f, "{}", i8::from(*self))
9 }
10}
11
12impl From<Charge> for i8 {
13 fn from(value: Charge) -> Self {
14 value.0.get()
15 }
16}
17
18impl From<Charge> for i16 {
19 fn from(value: Charge) -> Self {
20 value.0.get() as i16
21 }
22}
23
24impl From<Charge> for i32 {
25 fn from(value: Charge) -> Self {
26 value.0.get() as i32
27 }
28}
29
30impl From<Charge> for i64 {
31 fn from(value: Charge) -> Self {
32 value.0.get() as i64
33 }
34}
35
36impl From<Charge> for isize {
37 fn from(value: Charge) -> Self {
38 value.0.get() as isize
39 }
40}
41
42impl TryFrom<i8> for Charge {
43 type Error = CoordinateError;
44 fn try_from(value: i8) -> Result<Self, Self::Error> {
45 let x = std::num::NonZeroI8::new(value).ok_or_else(|| {
46 CoordinateError::new("Charge cannot be zero".to_string())
47 })?;
48 Ok(Self(x))
49 }
50}
51
52impl TryFrom<i16> for Charge {
53 type Error = CoordinateError;
54 fn try_from(value: i16) -> Result<Self, Self::Error> {
55 let x = i8::try_from(value)
56 .map_err(|e| CoordinateError::new(e.to_string()))?;
57 Charge::try_from(x)
58 }
59}
60
61impl TryFrom<i32> for Charge {
62 type Error = CoordinateError;
63 fn try_from(value: i32) -> Result<Self, Self::Error> {
64 let x = i8::try_from(value)
65 .map_err(|e| CoordinateError::new(e.to_string()))?;
66 Charge::try_from(x)
67 }
68}
69
70impl TryFrom<i64> for Charge {
71 type Error = CoordinateError;
72 fn try_from(value: i64) -> Result<Self, Self::Error> {
73 let x = i8::try_from(value)
74 .map_err(|e| CoordinateError::new(e.to_string()))?;
75 Charge::try_from(x)
76 }
77}
78
79impl TryFrom<isize> for Charge {
80 type Error = CoordinateError;
81 fn try_from(value: isize) -> Result<Self, Self::Error> {
82 let x = i8::try_from(value)
83 .map_err(|e| CoordinateError::new(e.to_string()))?;
84 Charge::try_from(x)
85 }
86}
87
88impl TryFrom<u8> for Charge {
89 type Error = CoordinateError;
90 fn try_from(value: u8) -> Result<Self, Self::Error> {
91 let x = i8::try_from(value)
92 .map_err(|e| CoordinateError::new(e.to_string()))?;
93 Charge::try_from(x)
94 }
95}
96
97impl TryFrom<u16> for Charge {
98 type Error = CoordinateError;
99 fn try_from(value: u16) -> Result<Self, Self::Error> {
100 let x = i8::try_from(value)
101 .map_err(|e| CoordinateError::new(e.to_string()))?;
102 Charge::try_from(x)
103 }
104}
105
106impl TryFrom<u32> for Charge {
107 type Error = CoordinateError;
108 fn try_from(value: u32) -> Result<Self, Self::Error> {
109 let x = i8::try_from(value)
110 .map_err(|e| CoordinateError::new(e.to_string()))?;
111 Charge::try_from(x)
112 }
113}
114
115impl TryFrom<u64> for Charge {
116 type Error = CoordinateError;
117 fn try_from(value: u64) -> Result<Self, Self::Error> {
118 let x = i8::try_from(value)
119 .map_err(|e| CoordinateError::new(e.to_string()))?;
120 Charge::try_from(x)
121 }
122}
123
124impl TryFrom<usize> for Charge {
125 type Error = CoordinateError;
126 fn try_from(value: usize) -> Result<Self, Self::Error> {
127 let x = i8::try_from(value)
128 .map_err(|e| CoordinateError::new(e.to_string()))?;
129 Charge::try_from(x)
130 }
131}