Skip to content

Commit

Permalink
add serde support as feature
Browse files Browse the repository at this point in the history
  • Loading branch information
iulianR committed Oct 28, 2019
1 parent 7cc4edb commit 22099ad
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 3 deletions.
80 changes: 80 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ readme = "README.md"
keywords = ["encoder", "decoder", "riot-games", "runeterra", "legends-of-runeterra"]
categories = ["encoding"]

[features]
default = ["serde"]

[dependencies]
data-encoding = "2.1.2"
varint = "0.9.0"
lazy_static = "1.4.0"

serde = { version = "1.0", optional = true, features = ["derive"] }

[dev-dependencies]
serde_json = "1.0"
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ To use `lordeckcodes`, add this to your `Cargo.toml`:
lordeckcodes = "0.1"
```

## Serde support

Serde support is optional and disabled by default. To enable use the feature `serde`.

```toml
[dependencies]
lordeckcodes = { version = "0.1", features = ["serde"] }
```

## Examples
Obtain a deck from the provided code:

Expand Down
5 changes: 5 additions & 0 deletions src/card.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::error::LorError;
use std::collections::HashMap;

Expand All @@ -14,6 +17,7 @@ lazy_static! {
};
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(PartialEq, Hash, Debug, Clone, Ord, PartialOrd, Eq)]
pub struct Card {
pub(crate) set: u32,
Expand Down Expand Up @@ -45,6 +49,7 @@ impl Card {
}

/// Stores card-related information.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq, Hash, Clone, Ord, PartialOrd, Eq)]
pub struct CardCodeAndCount {
pub(crate) card: Card,
Expand Down
4 changes: 4 additions & 0 deletions src/deck.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::card::{Card, CardCodeAndCount};
use crate::error::LorError;

/// Holds a set of [`CardCodeAndCount`].
///
/// [`CardCodeAndCount`]: struct.CardCodeAndCount.html
///
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
pub struct Deck(Vec<CardCodeAndCount>);

Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,5 @@ mod error;
/// Provides encode and decode API calls.
pub mod encoder;

pub use self::deck::Deck;
pub use self::card::CardCodeAndCount;

pub use self::deck::Deck;
33 changes: 32 additions & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::fs::File;
use std::io::{BufRead, BufReader};

use lordeckcodes::{CardCodeAndCount, encoder};
use lordeckcodes::Deck;
use lordeckcodes::{encoder, CardCodeAndCount};
use serde_json::Error;

#[test]
fn basic_decode_test() {
Expand Down Expand Up @@ -279,6 +280,36 @@ fn garbage_decoding() {
assert!(encoder::deck_from_code(bad_encoding_empty).is_err());
}

#[test]
fn deck_serialize_deserialize() {
let deck = Deck::from_vec(vec![
CardCodeAndCount::from_data("01SI015", 3).unwrap(),
CardCodeAndCount::from_data("01SI044", 3).unwrap(),
CardCodeAndCount::from_data("01SI048", 3).unwrap(),
CardCodeAndCount::from_data("01SI054", 3).unwrap(),
CardCodeAndCount::from_data("01FR003", 3).unwrap(),
CardCodeAndCount::from_data("01FR012", 3).unwrap(),
CardCodeAndCount::from_data("01FR020", 3).unwrap(),
CardCodeAndCount::from_data("01FR024", 3).unwrap(),
CardCodeAndCount::from_data("01FR033", 3).unwrap(),
CardCodeAndCount::from_data("01FR036", 3).unwrap(),
CardCodeAndCount::from_data("01FR039", 3).unwrap(),
CardCodeAndCount::from_data("01FR052", 3).unwrap(),
CardCodeAndCount::from_data("01SI005", 2).unwrap(),
CardCodeAndCount::from_data("01FR004", 2).unwrap(),
]);
let deck_json = serde_json::to_string(&deck);
assert!(deck_json.is_ok());

let code = encoder::code_from_deck(&deck);
assert!(code.is_ok());

let deck_from_json: Result<Deck, Error> = serde_json::from_str(&deck_json.unwrap());
assert!(deck_from_json.is_ok());

assert!(verify_rehydration(&deck, &deck_from_json.unwrap()));
}

fn verify_rehydration(d: &Deck, other: &Deck) -> bool {
if d.cards().len() != other.cards().len() {
return false;
Expand Down

0 comments on commit 22099ad

Please sign in to comment.