diff --git a/card_game/Cargo.toml b/card_game/Cargo.toml index 051d596..7423e64 100644 --- a/card_game/Cargo.toml +++ b/card_game/Cargo.toml @@ -8,8 +8,12 @@ description = "Card game library." authors = ["Rhys Lloyd "] keywords = ["card", "cards", "solitaire", "klondike"] +[features] +serde = ["dep:serde"] + [dependencies] arrayvec = { version = "0.7.6", registry = "Quaternions", features = ["len_u8"], default-features = false } +serde = { version = "1", optional = true, default-features = false, features = ["derive"] } [lints] workspace = true diff --git a/card_game/src/lib.rs b/card_game/src/lib.rs index ab698e0..fcf94fa 100644 --- a/card_game/src/lib.rs +++ b/card_game/src/lib.rs @@ -28,6 +28,7 @@ pub trait Game: Clone { /// card_game supports up to 4 identifiably separate decks. #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum Deck { Deck1 = 0b00, Deck2 = 0b01, @@ -48,6 +49,7 @@ impl Deck { } #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum Suit { Spades = 0b00, Hearts = 0b01, @@ -77,6 +79,7 @@ impl Suit { } #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum Rank { Ace = 1, Two = 2, @@ -146,6 +149,8 @@ impl Rank { /// 2 bits for suit ID /// 4 bits for card Value #[derive(Clone, Debug, Eq, Hash, PartialEq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", serde(transparent))] pub struct Card(core::num::NonZeroU8); impl Card { pub const fn new(deck: Deck, suit: Suit, rank: Rank) -> Self {