use crate::Object;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Rotation {
#[default]
None,
Clockwise90,
Half,
Clockwise270,
}
impl Rotation {
#[must_use]
pub const fn degrees(self) -> u16 {
match self {
Self::None => 0,
Self::Clockwise90 => 90,
Self::Half => 180,
Self::Clockwise270 => 270,
}
}
#[must_use]
pub const fn from_degrees(degrees: i64) -> Option<Self> {
match degrees.rem_euclid(360) {
0 => Some(Self::None),
90 => Some(Self::Clockwise90),
180 => Some(Self::Half),
270 => Some(Self::Clockwise270),
_ => None,
}
}
#[must_use]
pub const fn from_canonical_degrees(degrees: u16) -> Option<Self> {
match degrees {
0 => Some(Self::None),
90 => Some(Self::Clockwise90),
180 => Some(Self::Half),
270 => Some(Self::Clockwise270),
_ => None,
}
}
#[must_use]
pub fn from_object(value: &Object) -> Option<Self> {
let degrees = match *value {
Object::Integer(i) => i,
#[expect(
clippy::cast_possible_truncation,
reason = "only integral values in i64 range get here"
)]
Object::Real(r) if r.fract() == 0.0 && r.abs() < 1e15 => r as i64,
_ => return None,
};
Self::from_degrees(degrees)
}
#[must_use]
pub const fn is_quarter_turn(self) -> bool {
matches!(self, Self::Clockwise90 | Self::Clockwise270)
}
}
#[cfg(test)]
mod tests {
use super::Rotation;
use crate::Object;
#[test]
fn from_degrees_should_accept_every_multiple_of_90() {
let read: Vec<_> = [0, 90, -90, 450, 45].into_iter().map(Rotation::from_degrees).collect();
assert_eq!(
read,
[
Some(Rotation::None),
Some(Rotation::Clockwise90),
Some(Rotation::Clockwise270),
Some(Rotation::Clockwise90),
None
]
);
}
#[test]
fn from_object_should_read_integers_and_integral_reals_only() {
let values = [
Object::Integer(90),
Object::Real(90.0),
Object::Real(-180.0),
Object::Real(90.5),
Object::Real(1e300),
Object::Name("Rotate".into()),
];
let read: Vec<_> = values.iter().map(Rotation::from_object).collect();
assert_eq!(
read,
[
Some(Rotation::Clockwise90),
Some(Rotation::Clockwise90),
Some(Rotation::Half),
None,
None,
None
]
);
}
#[test]
fn from_canonical_degrees_should_refuse_other_spellings() {
let read: Vec<_> = [270, 450, 45]
.into_iter()
.map(Rotation::from_canonical_degrees)
.collect();
assert_eq!(read, [Some(Rotation::Clockwise270), None, None]);
}
#[test]
fn degrees_should_round_trip_through_from_degrees() {
let all = [
Rotation::None,
Rotation::Clockwise90,
Rotation::Half,
Rotation::Clockwise270,
];
let back: Vec<_> = all
.iter()
.map(|r| Rotation::from_degrees(i64::from(r.degrees())))
.collect();
assert_eq!(back, all.map(Some));
}
}