34 lines
736 B
Rust
34 lines
736 B
Rust
|
#[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())
|
||
|
}
|