Skip to content

Commit 8873d83

Browse files
committed
feat: add optional JSON serialization.
1 parent 8e5ad56 commit 8873d83

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description = "Get server addresses from QuakeWorld master servers."
44
keywords = ["masters", "quake", "quakeworld", "servers"]
55
repository = "https://github.com/quakeworld/masterstat"
66
authors = ["Viktor Persson <viktor.persson@arcsin.se>"]
7-
version = "0.5.0"
7+
version = "0.6.0"
88
edition = "2024"
99
license = "MIT"
1010
include = [
@@ -24,8 +24,12 @@ futures = "0.3.31"
2424
tokio = { version = "1.44.1", features = ["macros", "net", "rt-multi-thread", "sync", "time"] }
2525
tinyudp = "0.5.1"
2626

27+
serde = { optional = true, version = "1.0.219", features = ["derive"] }
28+
serde_json = { optional = true, version = "1.0.140" }
29+
2730
[dev-dependencies]
2831
pretty_assertions = "1.4.1"
2932

3033
[features]
3134
ci = []
35+
json = ["dep:serde", "dep:serde_json"]

src/command.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use binrw::BinRead;
77
use tokio::sync::Mutex;
88

99
use crate::server_address::{RawServerAddress, ServerAddress};
10-
use tinyudp;
1110

1211
/// Get server addresses from a single master server
1312
///

src/server_address.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use binrw::BinRead;
22
use std::fmt::Display;
33

4+
#[cfg(feature = "json")]
5+
use serde::{Serialize, Serializer};
6+
47
#[derive(BinRead)]
58
#[br(big)]
69
pub struct RawServerAddress {
@@ -29,6 +32,16 @@ impl From<RawServerAddress> for ServerAddress {
2932
}
3033
}
3134

35+
#[cfg(feature = "json")]
36+
impl Serialize for ServerAddress {
37+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
38+
where
39+
S: Serializer,
40+
{
41+
serializer.serialize_str(&self.to_string())
42+
}
43+
}
44+
3245
#[cfg(test)]
3346
mod tests {
3447
use crate::server_address::{RawServerAddress, ServerAddress};
@@ -64,4 +77,18 @@ mod tests {
6477
};
6578
assert_eq!(address.to_string(), "192.168.1.1:30000");
6679
}
80+
81+
#[cfg(feature = "json")]
82+
#[test]
83+
fn test_serialize() -> Result<()> {
84+
assert_eq!(
85+
serde_json::to_string(&ServerAddress {
86+
ip: "10.10.10.10".to_string(),
87+
port: 30000,
88+
})?,
89+
r#""10.10.10.10:30000""#
90+
);
91+
92+
Ok(())
93+
}
6794
}

0 commit comments

Comments
 (0)