errors.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. use crate::image::Pixel;
  2. use std::error::Error;
  3. use std::fmt::{Display, Formatter, Result};
  4. #[derive(Debug)]
  5. pub struct IncompleteMappingError {
  6. pub path: std::path::PathBuf,
  7. pub missing_colors: Vec<Pixel>,
  8. }
  9. impl Display for IncompleteMappingError {
  10. fn fmt(&self, f: &mut Formatter<'_>) -> Result {
  11. writeln!(
  12. f,
  13. "`{:?}` is missing entries for the following pixel colors:",
  14. self.path
  15. )?;
  16. for color in self.missing_colors.iter() {
  17. writeln!(f, " - {:?}", color)?;
  18. }
  19. Ok(())
  20. }
  21. }
  22. impl Error for IncompleteMappingError {}
  23. #[derive(Debug)]
  24. pub struct ThymeFileStructureError {
  25. pub message: String,
  26. }
  27. impl ThymeFileStructureError {
  28. pub fn new(msg: impl Into<String>) -> Self {
  29. ThymeFileStructureError {
  30. message: msg.into(),
  31. }
  32. }
  33. }
  34. impl Display for ThymeFileStructureError {
  35. fn fmt(&self, f: &mut Formatter<'_>) -> Result {
  36. writeln!(f, "{}", self.message)
  37. }
  38. }
  39. impl Error for ThymeFileStructureError {}
  40. #[derive(Debug)]
  41. pub enum ThymeFileError {
  42. StructureError(ThymeFileStructureError),
  43. ZipError(zip::result::ZipError),
  44. JsonError(serde_json::Error),
  45. }
  46. impl Display for ThymeFileError {
  47. fn fmt(&self, f: &mut Formatter<'_>) -> Result {
  48. match self {
  49. ThymeFileError::StructureError(err) => write!(f, "Error parsing file: {}", err),
  50. ThymeFileError::ZipError(err) => write!(f, "Error with zip wrapper: {}", err),
  51. ThymeFileError::JsonError(err) => write!(f, "Error parsing JSON: {}", err),
  52. }
  53. }
  54. }
  55. impl Error for ThymeFileError {}
  56. impl From<zip::result::ZipError> for ThymeFileError {
  57. fn from(err: zip::result::ZipError) -> Self {
  58. ThymeFileError::ZipError(err)
  59. }
  60. }
  61. impl From<serde_json::Error> for ThymeFileError {
  62. fn from(err: serde_json::Error) -> Self {
  63. ThymeFileError::JsonError(err)
  64. }
  65. }
  66. impl From<ThymeFileStructureError> for ThymeFileError {
  67. fn from(err: ThymeFileStructureError) -> Self {
  68. ThymeFileError::StructureError(err)
  69. }
  70. }