use crate::layout::Position;
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Offset {
pub x: i32,
pub y: i32,
}
impl Offset {
pub const ZERO: Self = Self::new(0, 0);
pub const MIN: Self = Self::new(i32::MIN, i32::MIN);
pub const MAX: Self = Self::new(i32::MAX, i32::MAX);
pub const fn new(x: i32, y: i32) -> Self {
Self { x, y }
}
}
impl From<Position> for Offset {
fn from(position: Position) -> Self {
Self {
x: i32::from(position.x),
y: i32::from(position.y),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_sets_components() {
assert_eq!(Offset::new(-3, 7), Offset { x: -3, y: 7 });
}
#[test]
fn constants_match_expected_values() {
assert_eq!(Offset::ZERO, Offset::new(0, 0));
assert_eq!(Offset::MIN, Offset::new(i32::MIN, i32::MIN));
assert_eq!(Offset::MAX, Offset::new(i32::MAX, i32::MAX));
}
#[test]
fn from_position_converts_coordinates() {
let position = Position::new(4, 9);
let offset = Offset::from(position);
assert_eq!(offset, Offset::new(4, 9));
}
}