Skip to content

Commit

Permalink
refactor!: use thiserror instead of anyhow
Browse files Browse the repository at this point in the history
  • Loading branch information
JiatLn committed Aug 30, 2023
1 parent 07ae49d commit 7eca95b
Show file tree
Hide file tree
Showing 10 changed files with 378 additions and 277 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ keywords = ["color", "art", "color-space", "color-art"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0"
rand = "0.8"
lazy_static = "1.4"
thiserror = "1.0.47"
13 changes: 6 additions & 7 deletions src/color/from_num.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use anyhow::{anyhow, Result};

use crate::Color;
use crate::{Color, Error};

impl Color {
/// Returns the numeric representation of the hexadecimal color.
Expand All @@ -13,11 +11,12 @@ impl Color {
/// let color = Color::from_num(0xff3399).unwrap();
/// assert_eq!(color.hex(), "#f39");
/// ```
pub fn from_num(num: u32) -> Result<Self> {
pub fn from_num(num: u32) -> Result<Self, Error> {
if num > 0xffffff {
return Err(anyhow!(
"Invalid color number, must be between 0 and 16777215"
));
return Err(Error::InvalidParamsError(format!(
"Invalid color number, must be between 0 and 16777215, but got {}",
num
)));
}
let r = ((num >> 16) & 0xff) as f64;
let g = ((num >> 8) & 0xff) as f64;
Expand Down
29 changes: 18 additions & 11 deletions src/color/from_space.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::{conversion, data::hex_of_name, Color, ColorSpace};
use anyhow::{Ok, Result};
use crate::{conversion, data::hex_of_name, Color, ColorSpace, Error};

impl Color {
/// Create a color from RGB values.
Expand All @@ -18,7 +17,7 @@ impl Color {
/// let color = Color::from_rgb(255, 51, 153).unwrap();
/// assert_eq!(color.hex(), "#f39");
/// ```
pub fn from_rgb<T>(r: T, g: T, b: T) -> Result<Self>
pub fn from_rgb<T>(r: T, g: T, b: T) -> Result<Self, Error>
where
T: Into<f64>,
{
Expand All @@ -44,7 +43,7 @@ impl Color {
/// let color = Color::from_rgba(255, 51, 153, 0.5).unwrap();
/// assert_eq!(color.rgba(), "rgba(255, 51, 153, 0.5)");
/// ```
pub fn from_rgba<T>(r: T, g: T, b: T, a: f64) -> Result<Self>
pub fn from_rgba<T>(r: T, g: T, b: T, a: f64) -> Result<Self, Error>
where
T: Into<f64>,
{
Expand All @@ -64,7 +63,7 @@ impl Color {
/// let color = Color::from_hsl(330.0, 1.0, 0.6).unwrap();
/// assert_eq!(color.hex(), "#f39");
/// ```
pub fn from_hsl(h: f64, s: f64, l: f64) -> Result<Self> {
pub fn from_hsl(h: f64, s: f64, l: f64) -> Result<Self, Error> {
let hsl = vec![h, s, l];
ColorSpace::HSL.valid(&hsl)?;
let rgb = conversion::hsl::hsl2rgb(&hsl);
Expand All @@ -83,7 +82,7 @@ impl Color {
/// let color = Color::from_hsv(38.82, 1.0, 1.0).unwrap();
/// assert_eq!(color.hex(), "#ffa500");
/// ```
pub fn from_hsv(h: f64, s: f64, v: f64) -> Result<Self> {
pub fn from_hsv(h: f64, s: f64, v: f64) -> Result<Self, Error> {
let hsv = vec![h, s, v];
ColorSpace::HSV.valid(&hsv)?;
let rgb = conversion::hsv::hsv2rgb(&hsv);
Expand All @@ -102,7 +101,7 @@ impl Color {
/// let color = Color::from_cmyk(0.0, 0.8, 0.4, 0.0).unwrap();
/// assert_eq!(color.hex(), "#f39");
/// ```
pub fn from_cmyk(c: f64, m: f64, y: f64, k: f64) -> Result<Self> {
pub fn from_cmyk(c: f64, m: f64, y: f64, k: f64) -> Result<Self, Error> {
let cmyk = vec![c, m, y, k];
ColorSpace::CMYK.valid(&cmyk)?;
let rgb = conversion::cmyk::cmyk2rgb(&cmyk);
Expand All @@ -124,12 +123,17 @@ impl Color {
/// let color = Color::from_hex("#ff339933").unwrap();
/// assert_eq!(color.hex(), "#f393");
/// ```
pub fn from_hex(hex_str: &str) -> Result<Self> {
pub fn from_hex(hex_str: &str) -> Result<Self, Error> {
ColorSpace::valid_hex(hex_str)?;
let color_vec = match hex_str.len() {
4 | 7 => conversion::hex::hex2rgb(hex_str),
5 | 9 => conversion::hex::hex2rgba(hex_str),
_ => anyhow::bail!("Got a error hex string!"),
_ => {
return Err(Error::InvalidParamsError(format!(
"Got a error hex string of '{}'!",
hex_str
)))
}
};
let r = color_vec[0];
let g = color_vec[1];
Expand Down Expand Up @@ -159,11 +163,14 @@ impl Color {
/// let color = Color::from_name("水绿").unwrap();
/// assert_eq!(color.hex(), "#8cc269");
/// ```
pub fn from_name(name: &str) -> Result<Self> {
pub fn from_name(name: &str) -> Result<Self, Error> {
let found = hex_of_name(name);
match found {
Some(hex) => Color::from_hex(hex),
None => anyhow::bail!("Invalid color name: {}", name),
None => Err(Error::InvalidParamsError(format!(
"Invalid color name: {}",
name
))),
}
}
}
Expand Down
14 changes: 9 additions & 5 deletions src/color/from_str.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::{conversion, data::hex_of_name, parser, Color, ColorSpace};
use anyhow::Result;
use crate::{conversion, data::hex_of_name, parser, Color, ColorSpace, Error};
use std::str::FromStr;

impl FromStr for Color {
type Err = anyhow::Error;
type Err = Error;
/// Creates a new [`Color`] from a string.
///
/// # Examples
Expand Down Expand Up @@ -40,7 +39,7 @@ impl FromStr for Color {
/// let color = Color::from_str(s).unwrap();
/// assert_eq!(color, Color::new(140, 194, 105, 1.0));
/// ```
fn from_str(s: &str) -> Result<Self> {
fn from_str(s: &str) -> Result<Self, Error> {
let input = s.trim().to_lowercase();

let (color_space, color_vec) = if input.starts_with('#') {
Expand Down Expand Up @@ -95,6 +94,8 @@ fn convert_color_vec_by_color_space(color_vec: &[f64], color_space: &ColorSpace)

#[cfg(test)]
mod tests {
use crate::Error;

use super::*;

#[test]
Expand Down Expand Up @@ -205,7 +206,10 @@ mod tests {
let s = "#gggggg";
let color = Color::from_str(s);
match color {
Err(e) => assert_eq!(e.to_string(), "Invalid hex string: #gggggg"),
Err(err) => assert_eq!(
err,
Error::ColorParserError("Invalid hex string of '#gggggg'".to_string())
),
_ => panic!("Should have failed"),
}

Expand Down
9 changes: 5 additions & 4 deletions src/color_generator/mix.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::Color;
use anyhow::{bail, Ok, Result};
use crate::{Color, Error};

impl Color {
/// Mix two colors with a weight.
Expand All @@ -20,9 +19,11 @@ impl Color {
/// let color3 = Color::mix(&color1, &color2, 0.5).unwrap();
/// assert_eq!(color3.hex(), "#594d85");
/// ```
pub fn mix(color1: &Color, color2: &Color, weight: f64) -> Result<Self> {
pub fn mix(color1: &Color, color2: &Color, weight: f64) -> Result<Self, Error> {
if !(0.0..=1.0).contains(&weight) {
bail!("weight must be between 0.0 and 1.0");
return Err(Error::InvalidParamsError(
"weight must be between 0.0 and 1.0".to_string(),
));
}
let (w1, w2) = (weight, 1.0 - weight);
let r = color1.rgb[0] * w1 + color2.rgb[0] * w2;
Expand Down
Loading

0 comments on commit 7eca95b

Please sign in to comment.