Skip to content

Commit

Permalink
add CsvDecodingError for improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
smackysnacks committed May 28, 2024
1 parent af005c6 commit a82ddc4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
33 changes: 26 additions & 7 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -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),
Expand All @@ -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<dyn std::error::Error + Send + Sync + 'static>,
},
/// There was an invalid tile in the map parsed.
Expand All @@ -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`].
Expand Down
8 changes: 6 additions & 2 deletions src/layers/tile/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions tests/lib.rs
Original file line number Diff line number Diff line change
@@ -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> {
Expand Down

0 comments on commit a82ddc4

Please sign in to comment.