ratatui/layout/
position.rs1#![warn(missing_docs)]
2use std::fmt;
3
4use crate::layout::Rect;
5
6#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
27#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
28pub struct Position {
29 pub x: u16,
34
35 pub y: u16,
40}
41
42impl Position {
43 pub const ORIGIN: Self = Self { x: 0, y: 0 };
45
46 pub const fn new(x: u16, y: u16) -> Self {
48 Self { x, y }
49 }
50}
51
52impl From<(u16, u16)> for Position {
53 fn from((x, y): (u16, u16)) -> Self {
54 Self { x, y }
55 }
56}
57
58impl From<Position> for (u16, u16) {
59 fn from(position: Position) -> Self {
60 (position.x, position.y)
61 }
62}
63
64impl From<Rect> for Position {
65 fn from(rect: Rect) -> Self {
66 rect.as_position()
67 }
68}
69
70impl fmt::Display for Position {
71 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
72 write!(f, "({}, {})", self.x, self.y)
73 }
74}
75
76#[cfg(test)]
77mod tests {
78 use super::*;
79
80 #[test]
81 fn new() {
82 let position = Position::new(1, 2);
83 assert_eq!(position.x, 1);
84 assert_eq!(position.y, 2);
85 }
86
87 #[test]
88 fn from_tuple() {
89 let position = Position::from((1, 2));
90 assert_eq!(position.x, 1);
91 assert_eq!(position.y, 2);
92 }
93
94 #[test]
95 fn into_tuple() {
96 let position = Position::new(1, 2);
97 let (x, y) = position.into();
98 assert_eq!(x, 1);
99 assert_eq!(y, 2);
100 }
101
102 #[test]
103 fn from_rect() {
104 let rect = Rect::new(1, 2, 3, 4);
105 let position = Position::from(rect);
106 assert_eq!(position.x, 1);
107 assert_eq!(position.y, 2);
108 }
109
110 #[test]
111 fn to_string() {
112 let position = Position::new(1, 2);
113 assert_eq!(position.to_string(), "(1, 2)");
114 }
115}