refactor(layout): move Margin to margin.rs (#740)

This commit is contained in:
Josh McKinney 2024-01-04 20:34:42 -08:00 committed by GitHub
parent da6c299804
commit 8724aeb9e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 45 deletions

View file

@ -1,11 +1,4 @@
use std::{
cell::RefCell,
collections::HashMap,
fmt::{self, Display},
num::NonZeroUsize,
rc::Rc,
sync::OnceLock,
};
use std::{cell::RefCell, collections::HashMap, num::NonZeroUsize, rc::Rc, sync::OnceLock};
use cassowary::{
strength::{MEDIUM, REQUIRED, STRONG, WEAK},
@ -17,9 +10,11 @@ use lru::LruCache;
use strum::{Display, EnumString};
mod constraint;
mod margin;
mod rect;
pub use constraint::Constraint;
pub use margin::Margin;
pub use rect::*;
type Cache = LruCache<(Rect, Layout), Rc<[Rect]>>;
@ -106,12 +101,6 @@ pub struct Layout {
segment_size: SegmentSize,
}
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)]
pub struct Margin {
pub horizontal: u16,
pub vertical: u16,
}
/// A simple size struct
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)]
pub struct Size {
@ -591,21 +580,6 @@ impl Layout {
}
}
impl Margin {
pub const fn new(horizontal: u16, vertical: u16) -> Margin {
Margin {
horizontal,
vertical,
}
}
}
impl Display for Margin {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}x{}", self.horizontal, self.vertical)
}
}
impl From<(u16, u16)> for Size {
fn from((width, height): (u16, u16)) -> Self {
Size { width, height }
@ -880,22 +854,6 @@ mod tests {
assert_eq!("".parse::<Direction>(), Err(ParseError::VariantNotFound));
}
#[test]
fn margin_to_string() {
assert_eq!(Margin::new(1, 2).to_string(), "1x2");
}
#[test]
fn margin_new() {
assert_eq!(
Margin::new(1, 2),
Margin {
horizontal: 1,
vertical: 2
}
);
}
#[test]
fn alignment_to_string() {
assert_eq!(Alignment::Left.to_string(), "Left");

43
src/layout/margin.rs Normal file
View file

@ -0,0 +1,43 @@
use std::fmt::{self, Display};
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)]
pub struct Margin {
pub horizontal: u16,
pub vertical: u16,
}
impl Margin {
pub const fn new(horizontal: u16, vertical: u16) -> Margin {
Margin {
horizontal,
vertical,
}
}
}
impl Display for Margin {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}x{}", self.horizontal, self.vertical)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn margin_to_string() {
assert_eq!(Margin::new(1, 2).to_string(), "1x2");
}
#[test]
fn margin_new() {
assert_eq!(
Margin::new(1, 2),
Margin {
horizontal: 1,
vertical: 2
}
);
}
}