From 5d6e58091850278ea32db5030e11ac8ce1b1acda Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Sun, 12 Apr 2020 13:21:27 +0100 Subject: [PATCH] position module --- src/lib.rs | 36 ++---------------------------------- src/position.rs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 34 deletions(-) create mode 100644 src/position.rs diff --git a/src/lib.rs b/src/lib.rs index 7468e6a..f317528 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,19 +2,15 @@ pub mod colour; pub mod palette; pub mod image; pub mod mocks; +pub mod position; pub mod tile; use colour::Colour; use palette::Palette; use image::Image; +use position::Position; use tile::Tile; -#[derive(Debug, Eq, PartialEq)] -pub struct Position { - x: u8, - y: u8, -} - #[derive(Debug, Eq, PartialEq)] pub struct Instance { position: Position, @@ -367,34 +363,6 @@ impl AnimationFrames for Vec { } } -impl From for Position { - fn from(string: String) -> Position { - // e.g. "2,5" - let xy: Vec<&str> = string.split(',').collect(); - let x = xy[0].parse().unwrap(); - let y = xy[1].parse().unwrap(); - - Position { x, y } - } -} - -#[test] -fn test_position_from_string() { - assert_eq!(Position::from("4,12".to_string()), Position { x: 4, y: 12 }); -} - -impl ToString for Position { - #[inline] - fn to_string(&self) -> String { - format!("{},{}", self.x, self.y) - } -} - -#[test] -fn test_position_to_string() { - assert_eq!(Position { x: 4, y: 12 }.to_string(), "4,12".to_string()) -} - impl From for Avatar { fn from(string: String) -> Avatar { let string = string.replace("SPR A\n", ""); diff --git a/src/position.rs b/src/position.rs new file mode 100644 index 0000000..ce2ca19 --- /dev/null +++ b/src/position.rs @@ -0,0 +1,34 @@ + +#[derive(Debug, Eq, PartialEq)] +pub struct Position { + pub(crate) x: u8, + pub(crate) y: u8, +} + +impl From for Position { + fn from(string: String) -> Position { + // e.g. "2,5" + let xy: Vec<&str> = string.split(',').collect(); + let x = xy[0].parse().unwrap(); + let y = xy[1].parse().unwrap(); + + Position { x, y } + } +} + +impl ToString for Position { + #[inline] + fn to_string(&self) -> String { + format!("{},{}", self.x, self.y) + } +} + +#[test] +fn test_position_from_string() { + assert_eq!(Position::from("4,12".to_string()), Position { x: 4, y: 12 }); +} + +#[test] +fn test_position_to_string() { + assert_eq!(Position { x: 4, y: 12 }.to_string(), "4,12".to_string()) +}