#![warn(missing_docs)]
use core::fmt;
use core::ops::{Add, AddAssign, Sub, SubAssign};
use crate::layout::{Offset, Rect};
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Position {
pub x: u16,
pub y: u16,
}
impl Position {
pub const ORIGIN: Self = Self::new(0, 0);
pub const MIN: Self = Self::ORIGIN;
pub const MAX: Self = Self::new(u16::MAX, u16::MAX);
pub const fn new(x: u16, y: u16) -> Self {
Self { x, y }
}
#[must_use = "method returns the modified value"]
pub fn offset(self, offset: Offset) -> Self {
self + offset
}
}
impl From<(u16, u16)> for Position {
fn from((x, y): (u16, u16)) -> Self {
Self { x, y }
}
}
impl From<Position> for (u16, u16) {
fn from(position: Position) -> Self {
(position.x, position.y)
}
}
impl From<Rect> for Position {
fn from(rect: Rect) -> Self {
rect.as_position()
}
}
impl fmt::Display for Position {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({}, {})", self.x, self.y)
}
}
impl Add<Offset> for Position {
type Output = Self;
fn add(self, offset: Offset) -> Self {
let max = i32::from(u16::MAX);
let x = i32::from(self.x).saturating_add(offset.x).clamp(0, max) as u16;
let y = i32::from(self.y).saturating_add(offset.y).clamp(0, max) as u16;
Self { x, y }
}
}
impl Add<Position> for Offset {
type Output = Position;
fn add(self, position: Position) -> Position {
position + self
}
}
impl Sub<Offset> for Position {
type Output = Self;
fn sub(self, offset: Offset) -> Self {
let max = i32::from(u16::MAX);
let x = i32::from(self.x).saturating_sub(offset.x).clamp(0, max) as u16;
let y = i32::from(self.y).saturating_sub(offset.y).clamp(0, max) as u16;
Self { x, y }
}
}
impl AddAssign<Offset> for Position {
fn add_assign(&mut self, offset: Offset) {
*self = *self + offset;
}
}
impl SubAssign<Offset> for Position {
fn sub_assign(&mut self, offset: Offset) {
*self = *self - offset;
}
}
#[cfg(test)]
mod tests {
use alloc::string::ToString;
use super::*;
#[test]
fn new() {
let position = Position::new(1, 2);
assert_eq!(position, Position { x: 1, y: 2 });
}
#[test]
fn from_tuple() {
let position = Position::from((1, 2));
assert_eq!(position, Position { x: 1, y: 2 });
}
#[test]
fn into_tuple() {
let position = Position::new(1, 2);
let (x, y) = position.into();
assert_eq!(x, 1);
assert_eq!(y, 2);
}
#[test]
fn from_rect() {
let rect = Rect::new(1, 2, 3, 4);
let position = Position::from(rect);
assert_eq!(position, Position { x: 1, y: 2 });
}
#[test]
fn to_string() {
let position = Position::new(1, 2);
assert_eq!(position.to_string(), "(1, 2)");
}
#[test]
fn offset_moves_position() {
let position = Position::new(2, 3).offset(Offset::new(5, 7));
assert_eq!(position, Position::new(7, 10));
}
#[test]
fn offset_clamps_to_bounds() {
let position = Position::new(1, 1).offset(Offset::MAX);
assert_eq!(position, Position::MAX);
}
#[test]
fn add_and_subtract_offset() {
let position = Position::new(10, 10) + Offset::new(-3, 4) - Offset::new(5, 20);
assert_eq!(position, Position::new(2, 0));
}
#[test]
fn add_assign_and_sub_assign_offset() {
let mut position = Position::new(5, 5);
position += Offset::new(2, 3);
position -= Offset::new(10, 1);
assert_eq!(position, Position::new(0, 7));
}
}