From 5f1bdebeb4c7362c0edbc6dbbf3f081208ec9210 Mon Sep 17 00:00:00 2001 From: Louis Capitanchik <contact@louiscap.co> Date: Sat, 16 Nov 2024 23:52:12 +0000 Subject: [PATCH] Switch from_ldtk_array to use new try_from impl --- src/layout.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/layout.rs b/src/layout.rs index 03686d2..78badce 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -1,4 +1,4 @@ -use std::fmt::{write, Debug, Formatter}; +use std::fmt::{Debug, Formatter}; use crate::utils::IntoTile; /// 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; #[derive(Debug, Clone, Copy, Eq, PartialEq)] pub struct 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") } } @@ -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; fn try_from(value: &[T]) -> Result<Self, Self::Error> { if is_square(value.len()) { @@ -213,11 +213,7 @@ impl TileMatcher { /// Load data from an LDTK JSON file. Supports arbitrary sized matchers for any square grid. /// Other sizes of matcher will result in `None` pub fn from_ldtk_array(value: Vec<i64>) -> Option<Self> { - if is_square(value.len()) { - Some(Self(transpose(value.as_slice()).map(TileStatus::from))) - } else { - None - } + Self::try_from(value.as_slice()).ok() } } -- GitLab