Skip to content
Snippets Groups Projects
  • Alejandro Perea's avatar
    Hide GIDs as internal details (#135) · df98e9f3
    Alejandro Perea authored
    
    * Partial commit
    
    * Partial commit
    
    * Partial commit
    
    * Some suggested changes and fixed tests (except infinite)
    
    * Replaced `tile` member in `LayerTileRef` with a function to get an
      `Option<&Tile>`.
    
    * Replaced `Map::get_tile_by_gid` with `Map::get_tileset_for_gid`, which
      just returns the `Option<&Tileset>`. It also does a reverse search,
      which fixes the lookup in case an external tileset has grown since the
      map was last saved.
    
    * Replaced `Tileset::get_tile_by_gid` with `Tileset::get_tile`, since
      subtracting of the `first_gid` already happens when creating the
      `LayerTileRef`. Also, we eventually should remove `first_gid` from
      `Tileset`, since it should be possible to share a single tileset
      instance betweeen several maps.
    
    * Pre-allocate the tiles hash map for the expected number of tiles and
      use `or_default` instead of `or_insert_with(Default::default)`.
    
    * [nonbuilding] Move ownership of tilesets
    - Moves the ownership of tilesets from Map to an object implementing `TilesetCache`
    
    * Clean up
    
    * More cleanup
    
    * Organize layers into modules
    
    * Further modularization
    
    * Add layer wrappers
    
    * Implement `Clone + PartialEq + Debug` for wrappers
    
    * Fix example
    
    * Fix all tests except for test_infinite_tileset
    
    * Move layer utils to its own module
    
    * Better `Map::layers` documentation
    
    * `TilesetCache` -> `cache::ResourceCache`
    
    * Add `ResourcePath`, rename and add errors
    
    * Interface changes
    - Move embedded tilesets from cache to map
    - Store `Option<LayerTileData>` instead of `LayerTileData`
    
    * parser ->`&mut impl Iterator<Item=XmlEventResult>`
    
    * Document that tilesets are ordered by first GID
    
    * Fix the layer tiles using GIDs issue
    
    * Run `cargo fix`
    
    * Fix `test_infinite_tileset` tests
    
    * Implement a way to access object tile data
    
    * Rename `TiledWrapper` to `MapWrapper`
    
    * More efficient `get_or_try_insert_tileset_with`
    
    * Add `ResourcePathBuf`, use `Rc<Tileset>` in `Map`
    
    * Remove `MapTileset::first_gid`
    
    * Run `cargo fix`
    
    * Remove unrelated `Tileset` changes
    
    * Requested changes
    
    * Avoid reference counting when accessing tiles
    
    * Store tile data instead of (u32, u32)` in objects
    
    * Remove unneeded functions from `ResourceCache`
    
    * Address PR comments
    
    * Misc improvements in `layers::tile`
    
    * Improve example
    
    * Convert `LayerTile` properties to fields
    
    Co-authored-by: default avatarThorbjørn Lindeijer <bjorn@lindeijer.nl>
    Unverified
    df98e9f3
error.rs 2.97 KiB
use std::{fmt, path::PathBuf};

#[derive(Debug, Copy, Clone)]
pub enum ParseTileError {
    ColorError,
    OrientationError,
}

/// Errors which occured when parsing the file
#[derive(Debug)]
pub enum TiledError {
    /// A attribute was missing, had the wrong type of wasn't formated
    /// correctly.
    MalformedAttributes(String),
    /// An error occured when decompressing using the
    /// [flate2](https://github.com/alexcrichton/flate2-rs) crate.
    DecompressingError(std::io::Error),
    Base64DecodingError(base64::DecodeError),
    XmlDecodingError(xml::reader::Error),
    PrematureEnd(String),
    /// Tried to parse external data of an object without a file location,
    /// e.g. by using Map::parse_reader.
    SourceRequired {
        object_to_parse: String,
    },
    /// The path given is invalid because it isn't contained in any folder.
    PathIsNotFile,
    CouldNotOpenFile {
        path: PathBuf,
        err: std::io::Error,
    },
    /// There was an invalid tile in the map parsed.
    InvalidTileFound,
    Other(String),
}

impl fmt::Display for TiledError {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        match self {
            TiledError::MalformedAttributes(s) => write!(fmt, "{}", s),
            TiledError::DecompressingError(e) => write!(fmt, "{}", e),
            TiledError::Base64DecodingError(e) => write!(fmt, "{}", e),
            TiledError::XmlDecodingError(e) => write!(fmt, "{}", e),
            TiledError::PrematureEnd(e) => write!(fmt, "{}", e),
            TiledError::SourceRequired {
                ref object_to_parse,
            } => {
                write!(fmt, "Tried to parse external {} without a file location, e.g. by using Map::parse_reader.", object_to_parse)
            }
            TiledError::PathIsNotFile => {
                write!(
                    fmt,
                    "The path given is invalid because it isn't contained in any folder."
                )
            }
            TiledError::CouldNotOpenFile { path, err } => {
                write!(
                    fmt,
                    "Could not open '{}'. Error: {}",
                    path.to_string_lossy(),
                    err
                )
            }
            TiledError::InvalidTileFound => write!(fmt, "Invalid tile found in map being parsed"),
            TiledError::Other(s) => write!(fmt, "{}", s),
        }
    }
}

// This is a skeleton implementation, which should probably be extended in the future.
impl std::error::Error for TiledError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            TiledError::DecompressingError(e) => Some(e as &dyn std::error::Error),
            TiledError::Base64DecodingError(e) => Some(e as &dyn std::error::Error),
            TiledError::XmlDecodingError(e) => Some(e as &dyn std::error::Error),
            TiledError::CouldNotOpenFile { err, .. } => Some(err as &dyn std::error::Error),
            _ => None,
        }
    }
}