diff --git a/src/error.rs b/src/error.rs index 838274e..66b28cc 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,20 +1,39 @@ +use std::num::ParseIntError; use std::{fmt, path::PathBuf}; -/// Errors which occured when parsing the file +/// Errors that can occur while decoding csv data. +#[derive(Clone, Debug, PartialEq, Eq)] +#[non_exhaustive] +pub enum CsvDecodingError { + /// An error occurred when parsing tile data from a csv encoded dataset. + TileDataParseError(ParseIntError), +} + +impl fmt::Display for CsvDecodingError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + CsvDecodingError::TileDataParseError(e) => write!(f, "{}", e), + } + } +} + +impl std::error::Error for CsvDecodingError {} + +/// Errors which occurred when parsing the file #[derive(Debug)] #[non_exhaustive] pub enum Error { /// A attribute was missing, had the wrong type of wasn't formated /// correctly. MalformedAttributes(String), - /// An error occured when decompressing using the + /// An error occurred when decompressing using the /// [flate2](https://github.com/alexcrichton/flate2-rs) crate. DecompressingError(std::io::Error), - /// An error occured when decoding a base64 encoded dataset. + /// An error occurred when decoding a base64 encoded dataset. Base64DecodingError(base64::DecodeError), /// An error occurred when decoding a csv encoded dataset. - CsvDecodingError(String), - /// An error occured when parsing a XML file, such as a TMX or TSX file. + CsvDecodingError(CsvDecodingError), + /// An error occurred when parsing an XML file, such as a TMX or TSX file. XmlDecodingError(xml::reader::Error), /// The XML stream ended before the document was fully parsed. PrematureEnd(String), @@ -25,7 +44,7 @@ pub enum Error { ResourceLoadingError { /// The path to the file that was unable to be opened. path: PathBuf, - /// The error that occured when trying to open the file. + /// The error that occurred when trying to open the file. err: Box, }, /// There was an invalid tile in the map parsed. @@ -41,7 +60,7 @@ pub enum Error { /// /// [`PropertyValue`]: crate::PropertyValue InvalidPropertyValue { - /// A description of the error that occured. + /// A description of the error that occurred. description: String, }, /// Found an unknown property value type while parsing a [`PropertyValue`]. diff --git a/src/layers/tile/util.rs b/src/layers/tile/util.rs index 98bbc6a..486134f 100644 --- a/src/layers/tile/util.rs +++ b/src/layers/tile/util.rs @@ -3,7 +3,7 @@ use std::{convert::TryInto, io::Read}; use base64::Engine; use xml::reader::XmlEvent; -use crate::{util::XmlEventResult, Error, LayerTileData, MapTilesetGid, Result}; +use crate::{util::XmlEventResult, CsvDecodingError, Error, LayerTileData, MapTilesetGid, Result}; pub(crate) fn parse_data_line( encoding: Option, @@ -74,7 +74,11 @@ fn decode_csv( for v in s.split(',') { match v.trim().parse() { Ok(bits) => tiles.push(LayerTileData::from_bits(bits, tilesets)), - Err(e) => return Err(Error::CsvDecodingError(e.to_string())), + Err(e) => { + return Err(Error::CsvDecodingError( + CsvDecodingError::TileDataParseError(e), + )) + } } } return Ok(tiles); diff --git a/tests/lib.rs b/tests/lib.rs index 753326f..4b41ccb 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -1,8 +1,8 @@ use std::path::PathBuf; + use tiled::{ - Color, FiniteTileLayer, GroupLayer, HorizontalAlignment, Layer, LayerType, Loader, Map, - ObjectLayer, ObjectShape, PropertyValue, ResourceCache, TileLayer, TilesetLocation, - VerticalAlignment, WangId, + Color, FiniteTileLayer, HorizontalAlignment, LayerType, Loader, Map, ObjectShape, + PropertyValue, ResourceCache, TileLayer, TilesetLocation, VerticalAlignment, WangId, }; fn as_finite<'map>(data: TileLayer<'map>) -> FiniteTileLayer<'map> {