Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make serde support optional #6

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ jobs:
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- name: Run tests (all features)
run: cargo test --verbose --all-features
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "steamid-ng"
version = "0.3.4"
version = "0.4.0"
authors = ["Moses Miller <Majora320@gmail.com>"]
description = "An easy-to-use steamid type with functions to parse and render steam2 and steam3 IDs"
repository = "https://github.com/Majora320/steamid-ng"
Expand All @@ -13,11 +13,14 @@ rust-version = "1.56"

[dependencies]
enum_primitive = "0.1.1"
serde = { version = "1.0.106", features = ["derive"] }
serde = { version = "1.0.106", features = ["derive"], optional = true }

[dev-dependencies]
serde_json = "1.0.51"

[badges]
maintenance = { status = "passively-maintained" }
travis-ci = { repository = "Majora320/steamid-ng", branch = "master" }

[package.metadata.docs.rs]
features = ["serde"]
42 changes: 5 additions & 37 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
//!
//! Keep in mind that the SteamID type does no validation.

#[cfg(feature = "serde")]
mod serde_support;

#[macro_use]
extern crate enum_primitive;

Expand All @@ -37,12 +40,9 @@ use std::{
};

use enum_primitive::FromPrimitive;
use serde::{
de::{self, Visitor},
Deserialize, Deserializer, Serialize,
};

#[derive(Clone, Copy, PartialEq, Eq, Hash, Default, Serialize)]
#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct SteamID(u64);

fn digit_from_ascii(byte: u8) -> Option<u8> {
Expand Down Expand Up @@ -317,38 +317,6 @@ impl FromStr for SteamID {
}
}

pub struct SteamIDVisitor;
impl<'de> Visitor<'de> for SteamIDVisitor {
type Value = SteamID;

fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
formatter.write_str("a SteamID")
}

fn visit_str<E>(self, value: &str) -> Result<SteamID, E>
where
E: de::Error,
{
SteamID::from_str(value).map_err(|_| E::custom(format!("Invalid SteamID: {}", value)))
}

fn visit_u64<E>(self, value: u64) -> Result<SteamID, E>
where
E: de::Error,
{
Ok(value.into())
}
}

impl<'de> Deserialize<'de> for SteamID {
fn deserialize<D>(deserializer: D) -> Result<SteamID, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_any(SteamIDVisitor)
}
}

impl Debug for SteamID {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(
Expand Down
44 changes: 44 additions & 0 deletions src/serde_support.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use std::{
fmt::{self, Formatter},
str::FromStr,
};

use crate::SteamID;

use serde::{
de::{self, Visitor},
Deserialize, Deserializer,
};

struct SteamIDVisitor;

impl<'de> Visitor<'de> for SteamIDVisitor {
type Value = SteamID;

fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
formatter.write_str("a SteamID")
}

fn visit_str<E>(self, value: &str) -> Result<SteamID, E>
where
E: de::Error,
{
SteamID::from_str(value).map_err(|_| E::custom(format!("Invalid SteamID: {}", value)))
}

fn visit_u64<E>(self, value: u64) -> Result<SteamID, E>
where
E: de::Error,
{
Ok(value.into())
}
}

impl<'de> Deserialize<'de> for SteamID {
fn deserialize<D>(deserializer: D) -> Result<SteamID, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_any(SteamIDVisitor)
}
}
104 changes: 56 additions & 48 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,61 +146,69 @@ fn test_from_steam3() {
}

#[test]
#[cfg_attr(not(feature = "serde"), ignore)]
fn test_serde() {
let s = SteamID::new(1234, Instance::Console, AccountType::Chat, Universe::Beta);
let serialized: String = serde_json::to_string(&s).unwrap();
let deserialized: SteamID = serde_json::from_str(&serialized).unwrap();
assert_eq!(s, deserialized);

let deserialized: SteamID = serde_json::from_str("\"STEAM_0:0:4491990\"").unwrap();
// Prevent rustfmt bug where it adds an extra comma even though assert_eq! is a macro
#[cfg_attr(rustfmt, rustfmt_skip)]
assert_eq!(
deserialized,
SteamID::new(
8983980,
#[cfg(not(feature = "serde"))]
{
panic!("Test only enabled with the 'serde' feature");
}
#[cfg(feature = "serde")]
{
let s = SteamID::new(1234, Instance::Console, AccountType::Chat, Universe::Beta);
let serialized: String = serde_json::to_string(&s).unwrap();
let deserialized: SteamID = serde_json::from_str(&serialized).unwrap();
assert_eq!(s, deserialized);

let deserialized: SteamID = serde_json::from_str("\"STEAM_0:0:4491990\"").unwrap();
// Prevent rustfmt bug where it adds an extra comma even though assert_eq! is a macro
#[cfg_attr(rustfmt, rustfmt_skip)]
assert_eq!(
deserialized,
SteamID::new(
8983980,
Instance::Desktop,
AccountType::Individual,
Universe::Public,
)
);

let deserialized: SteamID = serde_json::from_str("\"[U:1:123]\"").unwrap();
#[cfg_attr(rustfmt, rustfmt_skip)]
assert_eq!(
deserialized,
SteamID::new(
123,
Instance::Desktop,
AccountType::Individual,
Universe::Public,
)
);

let deserialized: SteamID = serde_json::from_str("103582791432294076").unwrap();
#[cfg_attr(rustfmt, rustfmt_skip)]
assert_eq!(
deserialized,
SteamID::new(2772668, Instance::All, AccountType::Clan, Universe::Public)
);

let serialized: String = serde_json::to_string(&SteamID::new(
8983981,
Instance::Desktop,
AccountType::Individual,
Universe::Public,
)
);
))
.unwrap();
assert_eq!(serialized, "76561197969249709");

let deserialized: SteamID = serde_json::from_str("\"[U:1:123]\"").unwrap();
#[cfg_attr(rustfmt, rustfmt_skip)]
assert_eq!(
deserialized,
SteamID::new(
let serialized: String = serde_json::to_string(&SteamID::new(
123,
Instance::Desktop,
AccountType::Individual,
Instance::Web,
AccountType::AnonGameServer,
Universe::Public,
)
);

let deserialized: SteamID = serde_json::from_str("103582791432294076").unwrap();
#[cfg_attr(rustfmt, rustfmt_skip)]
assert_eq!(
deserialized,
SteamID::new(2772668, Instance::All, AccountType::Clan, Universe::Public)
);

let serialized: String = serde_json::to_string(&SteamID::new(
8983981,
Instance::Desktop,
AccountType::Individual,
Universe::Public,
))
.unwrap();
assert_eq!(serialized, "76561197969249709");

let serialized: String = serde_json::to_string(&SteamID::new(
123,
Instance::Web,
AccountType::AnonGameServer,
Universe::Public,
))
.unwrap();
assert_eq!(serialized, "90072009727279227");
))
.unwrap();
assert_eq!(serialized, "90072009727279227");
}
}

#[test]
Expand Down