Skip to content

Commit

Permalink
Derive thiserror::Error for HexColorError (again) (bevyengine#4847)
Browse files Browse the repository at this point in the history
This was first done in 7b4e3a5, but was then reverted when the new
renderer for 0.6 was merged (ffecb05).

I'm assuming it was simply a mistake when merging.

# Objective

- Same as bevyengine#2740, I think it was reverted by mistake when merging.

> # Objective
>
> - Make it easy to use HexColorError with `thiserror`, i.e. converting it into other error types.
> 
> Makes this possible:
> 
> ```rust
> #[derive(Debug, thiserror::Error)]
> pub enum LdtkError {
>     #[error("An error occured while deserializing")]
>     Json(#[from] serde_json::Error),
>     #[error("An error occured while parsing a color")]
>     HexColor(#[from] bevy::render::color::HexColorError),
> }
> ```
> 
> ## Solution
> 
> - Derive thiserror::Error the same way we do elsewhere (see query.rs for instance)
  • Loading branch information
johanhelsing authored and ItsDoot committed Feb 1, 2023
1 parent 59a732e commit 0243e3a
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions crates/bevy_render/src/color/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use bevy_math::{Vec3, Vec4};
use bevy_reflect::{FromReflect, Reflect, ReflectDeserialize};
use serde::{Deserialize, Serialize};
use std::ops::{Add, AddAssign, Mul, MulAssign};
use thiserror::Error;

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Reflect, FromReflect)]
#[reflect(PartialEq, Serialize, Deserialize)]
Expand Down Expand Up @@ -1170,10 +1171,12 @@ impl MulAssign<[f32; 3]> for Color {
}
}

#[derive(Debug)]
#[derive(Debug, Error)]
pub enum HexColorError {
#[error("Unexpected length of hex string")]
Length,
Hex(hex::FromHexError),
#[error("Error parsing hex value")]
Hex(#[from] hex::FromHexError),
}

fn decode_rgb(data: &[u8]) -> Result<Color, HexColorError> {
Expand Down

0 comments on commit 0243e3a

Please sign in to comment.