diff --git a/Cargo.lock b/Cargo.lock index cab3d16..3e00069 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,6 +10,11 @@ name = "data-encoding" version = "2.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "itoa" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "lazy_static" version = "1.4.0" @@ -21,9 +26,75 @@ version = "0.1.0" dependencies = [ "data-encoding 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)", "varint 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "proc-macro2" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "quote" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "ryu" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "serde" +version = "1.0.101" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "serde_derive 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_derive" +version = "1.0.101" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_json" +version = "1.0.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", + "ryu 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "syn" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-xid" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "varint" version = "0.9.0" @@ -35,5 +106,14 @@ dependencies = [ [metadata] "checksum bit_utils 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f8114dca45a04a4bdaf03de9976a3131cf954827757f971d84cd431a9f2cc26d" "checksum data-encoding 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f4f47ca1860a761136924ddd2422ba77b2ea54fe8cc75b9040804a0d9d32ad97" +"checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f" "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +"checksum proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "9c9e470a8dc4aeae2dee2f335e8f533e2d4b347e1434e5671afc49b054592f27" +"checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe" +"checksum ryu 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "bfa8506c1de11c9c4e4c38863ccbe02a305c8188e85a05a784c9e11e1c3910c8" +"checksum serde 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)" = "9796c9b7ba2ffe7a9ce53c2287dfc48080f4b2b362fcc245a259b3a7201119dd" +"checksum serde_derive 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)" = "4b133a43a1ecd55d4086bd5b4dc6c1751c68b1bfbeba7a5040442022c7e7c02e" +"checksum serde_json 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)" = "2f72eb2a68a7dc3f9a691bfda9305a1c017a6215e5a4545c258500d2099a37c2" +"checksum syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "66850e97125af79138385e9b88339cbcd037e3f28ceab8c5ad98e64f0f1f80bf" +"checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" "checksum varint 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c37be5674e0be9c72790681999b0e6f09f03d3fc5d8904cfe2db615aad65887b" diff --git a/Cargo.toml b/Cargo.toml index 5c5fdab..f340c79 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/README.md b/README.md index 4366b39..5a0cfdf 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/card.rs b/src/card.rs index 4e8454e..c6c6531 100644 --- a/src/card.rs +++ b/src/card.rs @@ -1,3 +1,6 @@ +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + use crate::error::LorError; use std::collections::HashMap; @@ -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, @@ -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, diff --git a/src/deck.rs b/src/deck.rs index 4c7f823..f8bec5c 100644 --- a/src/deck.rs +++ b/src/deck.rs @@ -1,3 +1,6 @@ +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + use crate::card::{Card, CardCodeAndCount}; use crate::error::LorError; @@ -5,6 +8,7 @@ use crate::error::LorError; /// /// [`CardCodeAndCount`]: struct.CardCodeAndCount.html /// +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Debug)] pub struct Deck(Vec); diff --git a/src/lib.rs b/src/lib.rs index 5225e9e..6ab2798 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/tests/test.rs b/tests/test.rs index 411c4bd..38dff58 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -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() { @@ -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 = 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;