Skip to content

Commit

Permalink
Merge pull request #5 from LilyIsTrans/serde-support
Browse files Browse the repository at this point in the history
Add serde support
  • Loading branch information
JiatLn authored Mar 12, 2024
2 parents 3733994 + 297328a commit 6df7153
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ keywords = ["color", "art", "color-space", "color-art"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = ["serde"]
serde = ["dep:serde"]

[dependencies]
rand = "0.8"
lazy_static = "1.4"
thiserror = "1.0.47"
serde = { version = "1.0.193", features = ["derive"] , optional = true}

[dev-dependencies]
serde_json = "1.0.108"
4 changes: 4 additions & 0 deletions src/color/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ pub mod vec_of;

use std::fmt::Display;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Color is a struct that represents a color.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(PartialEq, Debug, Clone, Copy)]
pub struct Color {
pub(crate) rgb: [f64; 3],
Expand Down
4 changes: 4 additions & 0 deletions src/color_calc/blend.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use crate::{utils::blend_fn::*, Color, ColorSpace};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// ### blend mode enum
///
/// The blend mode defines the formula that must be used to mix the colors with the backdrop.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum BlendMode {
/// ### normal blend mode
///
Expand Down
4 changes: 4 additions & 0 deletions src/color_space/space.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Color space enum.
#[derive(Clone, Copy, PartialEq, Debug, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ColorSpace {
/// RGB color space.
///
Expand Down
4 changes: 4 additions & 0 deletions src/error/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use thiserror::Error;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Error info enum
#[derive(Error, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Error {
#[error("{0}")]
ColorParserError(String),
Expand Down
6 changes: 6 additions & 0 deletions src/parser/core.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use crate::{ColorSpace, Error};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Token {
pub kind: TokenKind,
pub value: String,
}

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum TokenKind {
Value,
Identifier,
Expand All @@ -18,6 +23,7 @@ pub enum TokenKind {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Parser {
pub tokens: Vec<Token>,
pub current: usize,
Expand Down
31 changes: 31 additions & 0 deletions tests/serde.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#![cfg(feature = "serde")]
// Disable this entire file if "serde" is disabled

use color_art::Color;
use thiserror::Error;

#[derive(Error, Debug)]
enum SerdeTestError {
#[error("Serde JSON Error: {0}")]
SerdeJSONError(#[from] serde_json::Error),

#[error("{0}")]
Custom(&'static str),
}

#[test]
fn test_serde_color_json() -> Result<(), SerdeTestError> {
let color = Color::random();

let serial = serde_json::to_string(&color)?;

let deserial = serde_json::from_str(&serial)?;

if color == deserial {
Ok(())
} else {
Err(SerdeTestError::Custom(
"Deserialized color doesn't match serialized color!",
))
}
}

0 comments on commit 6df7153

Please sign in to comment.