battleship game in rust

Format document

Changed files
+28 -14
src
+28 -14
src/parser.rs
···
impl Vector2u16 {
pub fn new(x: u16, y: u16) -> Self {
-
Vector2u16 {x: x, y: y}
+
Vector2u16 { x: x, y: y }
}
}
···
impl Boat {
pub fn new(first_pos: Vector2u16, last_pos: Vector2u16) -> Self {
-
Boat { first_pos: first_pos, last_pos: last_pos }
+
Boat {
+
first_pos: first_pos,
+
last_pos: last_pos,
+
}
}
}
···
return Err(ParserError::InvalidCoordCount);
}
-
-
Ok(Boat::new(create_coord(coordinates[0])?, create_coord(coordinates[1])?))
+
Ok(Boat::new(
+
create_coord(coordinates[0])?,
+
create_coord(coordinates[1])?,
+
))
}
fn create_coord(coord_str: &str) -> Result<Vector2u16, ParserError> {
···
}
Ok(Vector2u16::new(col, row))
-
}
#[cfg(test)]
···
assert_eq!(coord2.y, 0);
let coord3 = create_coord("H8")?;
-
+
assert_eq!(coord3.x, 7);
assert_eq!(coord3.y, 7);
···
#[test]
fn test_create_ship() -> Result<(), ParserError> {
let ship = create_ship("C4:B1")?;
-
+
assert_eq!(ship.first_pos.x, 2);
assert_eq!(ship.first_pos.y, 3);
assert_eq!(ship.last_pos.x, 1);
···
let ships = parse_ships(&mut &file[..])?;
-
assert_eq!(ships[0], Boat::new(Vector2u16::new(1, 3), Vector2u16::new(2, 1)));
-
assert_eq!(ships[1], Boat::new(Vector2u16::new(3, 0), Vector2u16::new(5, 2)));
-
assert_eq!(ships[2], Boat::new(Vector2u16::new(5, 0), Vector2u16::new(5, 4)));
-
assert_eq!(ships[3], Boat::new(Vector2u16::new(0, 0), Vector2u16::new(0, 1)));
+
assert_eq!(
+
ships[0],
+
Boat::new(Vector2u16::new(1, 3), Vector2u16::new(2, 1))
+
);
+
assert_eq!(
+
ships[1],
+
Boat::new(Vector2u16::new(3, 0), Vector2u16::new(5, 2))
+
);
+
assert_eq!(
+
ships[2],
+
Boat::new(Vector2u16::new(5, 0), Vector2u16::new(5, 4))
+
);
+
assert_eq!(
+
ships[3],
+
Boat::new(Vector2u16::new(0, 0), Vector2u16::new(0, 1))
+
);
Ok(())
}
-
-
-
}
+
}