position module

This commit is contained in:
Max Bradbury 2020-04-12 13:21:27 +01:00
parent a1782a6bed
commit 5d6e580918
2 changed files with 36 additions and 34 deletions

View File

@ -2,19 +2,15 @@ pub mod colour;
pub mod palette; pub mod palette;
pub mod image; pub mod image;
pub mod mocks; pub mod mocks;
pub mod position;
pub mod tile; pub mod tile;
use colour::Colour; use colour::Colour;
use palette::Palette; use palette::Palette;
use image::Image; use image::Image;
use position::Position;
use tile::Tile; use tile::Tile;
#[derive(Debug, Eq, PartialEq)]
pub struct Position {
x: u8,
y: u8,
}
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
pub struct Instance { pub struct Instance {
position: Position, position: Position,
@ -367,34 +363,6 @@ impl AnimationFrames for Vec<Image> {
} }
} }
impl From<String> 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<String> for Avatar { impl From<String> for Avatar {
fn from(string: String) -> Avatar { fn from(string: String) -> Avatar {
let string = string.replace("SPR A\n", ""); let string = string.replace("SPR A\n", "");

34
src/position.rs Normal file
View File

@ -0,0 +1,34 @@
#[derive(Debug, Eq, PartialEq)]
pub struct Position {
pub(crate) x: u8,
pub(crate) y: u8,
}
impl From<String> 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())
}