feat(card_game): add optional serde feature for Deck, Suit, Rank, Card

Adds a `serde` Cargo feature that gates #[derive(Serialize, Deserialize)]
on the four primitive types: Deck, Suit, Rank, and Card.

- Deck / Suit / Rank: plain enum derives — serialize as variant name strings
- Card: newtype over NonZeroU8 with #[serde(transparent)], serializes as
  the raw packed byte (deck<<6 | suit<<4 | rank)

Without the feature the crate remains no_std-compatible and pulls no
additional dependencies. With it, serde 1.x (derive only, no std) is
added as an optional dep.

Downstream consumers enable with:
  card_game = { ..., features = ["serde"] }
This commit is contained in:
funman300
2026-06-08 10:49:53 -07:00
parent ccfcfd6ad3
commit 0cdfa5446a
2 changed files with 9 additions and 0 deletions
+4
View File
@@ -8,8 +8,12 @@ description = "Card game library."
authors = ["Rhys Lloyd <krakow20@gmail.com>"] authors = ["Rhys Lloyd <krakow20@gmail.com>"]
keywords = ["card", "cards", "solitaire", "klondike"] keywords = ["card", "cards", "solitaire", "klondike"]
[features]
serde = ["dep:serde"]
[dependencies] [dependencies]
arrayvec = { version = "0.7.6", registry = "Quaternions", features = ["len_u8"], default-features = false } arrayvec = { version = "0.7.6", registry = "Quaternions", features = ["len_u8"], default-features = false }
serde = { version = "1", optional = true, default-features = false, features = ["derive"] }
[lints] [lints]
workspace = true workspace = true
+5
View File
@@ -28,6 +28,7 @@ pub trait Game: Clone {
/// card_game supports up to 4 identifiably separate decks. /// card_game supports up to 4 identifiably separate decks.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Deck { pub enum Deck {
Deck1 = 0b00, Deck1 = 0b00,
Deck2 = 0b01, Deck2 = 0b01,
@@ -48,6 +49,7 @@ impl Deck {
} }
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Suit { pub enum Suit {
Spades = 0b00, Spades = 0b00,
Hearts = 0b01, Hearts = 0b01,
@@ -77,6 +79,7 @@ impl Suit {
} }
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Rank { pub enum Rank {
Ace = 1, Ace = 1,
Two = 2, Two = 2,
@@ -146,6 +149,8 @@ impl Rank {
/// 2 bits for suit ID /// 2 bits for suit ID
/// 4 bits for card Value /// 4 bits for card Value
#[derive(Clone, Debug, Eq, Hash, PartialEq)] #[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); pub struct Card(core::num::NonZeroU8);
impl Card { impl Card {
pub const fn new(deck: Deck, suit: Suit, rank: Rank) -> Self { pub const fn new(deck: Deck, suit: Suit, rank: Rank) -> Self {