Skip to content
Snippets Groups Projects
Verified Commit 5f1bdebe authored by Louis's avatar Louis :fire:
Browse files

Switch from_ldtk_array to use new try_from impl

parent d4f55411
No related branches found
No related tags found
No related merge requests found
use std::fmt::{write, Debug, Formatter}; use std::fmt::{Debug, Formatter};
use crate::utils::IntoTile; use crate::utils::IntoTile;
/// The size of the grid that can be matched; equal to the length of one side of the square grid /// The size of the grid that can be matched; equal to the length of one side of the square grid
...@@ -11,7 +11,7 @@ const GRID_CENTER: usize = (TILE_GRID_SIZE - 1) / 2; ...@@ -11,7 +11,7 @@ const GRID_CENTER: usize = (TILE_GRID_SIZE - 1) / 2;
#[derive(Debug, Clone, Copy, Eq, PartialEq)] #[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct NotSquareError; pub struct NotSquareError;
impl std::fmt::Display for NotSquareError { impl std::fmt::Display for NotSquareError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "Input is not a square grid") write!(f, "Input is not a square grid")
} }
} }
...@@ -149,7 +149,7 @@ impl Debug for TileLayout { ...@@ -149,7 +149,7 @@ impl Debug for TileLayout {
} }
} }
impl <T> TryFrom<&[T]> for TileLayout where T: IntoTile + Copy + Default + Debug { impl <T> TryFrom<&[T]> for TileLayout where T: IntoTile + Copy + Default {
type Error = NotSquareError; type Error = NotSquareError;
fn try_from(value: &[T]) -> Result<Self, Self::Error> { fn try_from(value: &[T]) -> Result<Self, Self::Error> {
if is_square(value.len()) { if is_square(value.len()) {
...@@ -213,11 +213,7 @@ impl TileMatcher { ...@@ -213,11 +213,7 @@ impl TileMatcher {
/// Load data from an LDTK JSON file. Supports arbitrary sized matchers for any square grid. /// Load data from an LDTK JSON file. Supports arbitrary sized matchers for any square grid.
/// Other sizes of matcher will result in `None` /// Other sizes of matcher will result in `None`
pub fn from_ldtk_array(value: Vec<i64>) -> Option<Self> { pub fn from_ldtk_array(value: Vec<i64>) -> Option<Self> {
if is_square(value.len()) { Self::try_from(value.as_slice()).ok()
Some(Self(transpose(value.as_slice()).map(TileStatus::from)))
} else {
None
}
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment