laminapdf_syntax/
rotation.rs1use crate::Object;
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
11pub enum Rotation {
12 #[default]
14 None,
15 Clockwise90,
17 Half,
19 Clockwise270,
21}
22
23impl Rotation {
24 #[must_use]
28 pub const fn degrees(self) -> u16 {
29 match self {
30 Self::None => 0,
31 Self::Clockwise90 => 90,
32 Self::Half => 180,
33 Self::Clockwise270 => 270,
34 }
35 }
36
37 #[must_use]
41 pub const fn from_degrees(degrees: i64) -> Option<Self> {
42 match degrees.rem_euclid(360) {
43 0 => Some(Self::None),
44 90 => Some(Self::Clockwise90),
45 180 => Some(Self::Half),
46 270 => Some(Self::Clockwise270),
47 _ => None,
48 }
49 }
50
51 #[must_use]
57 pub const fn from_canonical_degrees(degrees: u16) -> Option<Self> {
58 match degrees {
59 0 => Some(Self::None),
60 90 => Some(Self::Clockwise90),
61 180 => Some(Self::Half),
62 270 => Some(Self::Clockwise270),
63 _ => None,
64 }
65 }
66
67 #[must_use]
72 pub fn from_object(value: &Object) -> Option<Self> {
73 let degrees = match *value {
74 Object::Integer(i) => i,
75 #[expect(
76 clippy::cast_possible_truncation,
77 reason = "only integral values in i64 range get here"
78 )]
79 Object::Real(r) if r.fract() == 0.0 && r.abs() < 1e15 => r as i64,
80 _ => return None,
81 };
82 Self::from_degrees(degrees)
83 }
84
85 #[must_use]
89 pub const fn is_quarter_turn(self) -> bool {
90 matches!(self, Self::Clockwise90 | Self::Clockwise270)
91 }
92}
93
94#[cfg(test)]
95mod tests {
96 use super::Rotation;
97 use crate::Object;
98
99 #[test]
100 fn from_degrees_should_accept_every_multiple_of_90() {
101 let read: Vec<_> = [0, 90, -90, 450, 45].into_iter().map(Rotation::from_degrees).collect();
102 assert_eq!(
103 read,
104 [
105 Some(Rotation::None),
106 Some(Rotation::Clockwise90),
107 Some(Rotation::Clockwise270),
108 Some(Rotation::Clockwise90),
109 None
110 ]
111 );
112 }
113
114 #[test]
115 fn from_object_should_read_integers_and_integral_reals_only() {
116 let values = [
117 Object::Integer(90),
118 Object::Real(90.0),
119 Object::Real(-180.0),
120 Object::Real(90.5),
121 Object::Real(1e300),
122 Object::Name("Rotate".into()),
123 ];
124 let read: Vec<_> = values.iter().map(Rotation::from_object).collect();
125 assert_eq!(
126 read,
127 [
128 Some(Rotation::Clockwise90),
129 Some(Rotation::Clockwise90),
130 Some(Rotation::Half),
131 None,
132 None,
133 None
134 ]
135 );
136 }
137
138 #[test]
139 fn from_canonical_degrees_should_refuse_other_spellings() {
140 let read: Vec<_> = [270, 450, 45]
141 .into_iter()
142 .map(Rotation::from_canonical_degrees)
143 .collect();
144 assert_eq!(read, [Some(Rotation::Clockwise270), None, None]);
145 }
146
147 #[test]
148 fn degrees_should_round_trip_through_from_degrees() {
149 let all = [
150 Rotation::None,
151 Rotation::Clockwise90,
152 Rotation::Half,
153 Rotation::Clockwise270,
154 ];
155 let back: Vec<_> = all
156 .iter()
157 .map(|r| Rotation::from_degrees(i64::from(r.degrees())))
158 .collect();
159 assert_eq!(back, all.map(Some));
160 }
161}