mirror of
https://github.com/tribufu/rust-gamedig
synced 2026-06-01 09:42:41 +00:00
fix: bson output
This commit is contained in:
parent
371e49c969
commit
de876227cc
2 changed files with 37 additions and 7 deletions
|
|
@ -17,7 +17,7 @@ default = ["packet_capture", "bson", "json", "xml", "browser"]
|
||||||
packet_capture = ["gamedig/packet_capture"]
|
packet_capture = ["gamedig/packet_capture"]
|
||||||
|
|
||||||
# Output formats
|
# Output formats
|
||||||
bson = ["dep:serde", "dep:bson", "dep:hex", "gamedig/serde"]
|
bson = ["dep:serde", "dep:bson", "dep:hex", "dep:base64", "gamedig/serde"]
|
||||||
json = ["dep:serde", "dep:serde_json", "gamedig/serde"]
|
json = ["dep:serde", "dep:serde_json", "gamedig/serde"]
|
||||||
xml = ["dep:serde", "dep:serde-xml-rs", "gamedig/serde"]
|
xml = ["dep:serde", "dep:serde-xml-rs", "gamedig/serde"]
|
||||||
|
|
||||||
|
|
@ -40,6 +40,7 @@ serde = { version = "1", optional = true, default-features = false }
|
||||||
|
|
||||||
# BSON
|
# BSON
|
||||||
bson = { version = "2.8.1", optional = true, default-features = false }
|
bson = { version = "2.8.1", optional = true, default-features = false }
|
||||||
|
base64 = { version = "0.21.7", optional = true, default-features = false, features = ["std"]}
|
||||||
hex = { version = "0.4.3", optional = true, default-features = false }
|
hex = { version = "0.4.3", optional = true, default-features = false }
|
||||||
|
|
||||||
# JSON
|
# JSON
|
||||||
|
|
@ -50,3 +51,4 @@ serde-xml-rs = { version = "0.6.0", optional = true, default-features = false }
|
||||||
|
|
||||||
# Browser
|
# Browser
|
||||||
webbrowser = { version = "0.8.12", optional = true, default-features = false }
|
webbrowser = { version = "0.8.12", optional = true, default-features = false }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -96,15 +96,23 @@ enum OutputMode {
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, ValueEnum)]
|
#[derive(Clone, Debug, PartialEq, Eq, ValueEnum)]
|
||||||
enum OutputFormat {
|
enum OutputFormat {
|
||||||
|
/// Human readable structured output
|
||||||
Debug,
|
Debug,
|
||||||
|
/// RFC 8259
|
||||||
#[cfg(feature = "json")]
|
#[cfg(feature = "json")]
|
||||||
JsonPretty,
|
JsonPretty,
|
||||||
|
/// RFC 8259
|
||||||
#[cfg(feature = "json")]
|
#[cfg(feature = "json")]
|
||||||
Json,
|
Json,
|
||||||
|
/// Parser tries to be mostly XML 1.1 (RFC 7303) compliant
|
||||||
#[cfg(feature = "xml")]
|
#[cfg(feature = "xml")]
|
||||||
Xml,
|
Xml,
|
||||||
|
/// RFC 4648 section 8
|
||||||
#[cfg(feature = "bson")]
|
#[cfg(feature = "bson")]
|
||||||
Bson,
|
BsonHex,
|
||||||
|
/// RFC 4648 section 4
|
||||||
|
#[cfg(feature = "bson")]
|
||||||
|
BsonBase64,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Attempt to find a game from the [library game definitions](GAMES) based on
|
/// Attempt to find a game from the [library game definitions](GAMES) based on
|
||||||
|
|
@ -211,9 +219,14 @@ fn output_result<T: CommonResponse + ?Sized>(output_mode: OutputMode, format: Ou
|
||||||
OutputMode::ProtocolSpecific => panic!("XML format is not supported for protocol specific output"),
|
OutputMode::ProtocolSpecific => panic!("XML format is not supported for protocol specific output"),
|
||||||
},
|
},
|
||||||
#[cfg(feature = "bson")]
|
#[cfg(feature = "bson")]
|
||||||
OutputFormat::Bson => match output_mode {
|
OutputFormat::BsonHex => match output_mode {
|
||||||
OutputMode::Generic => output_result_bson(result.as_json()),
|
OutputMode::Generic => output_result_bson_hex(result.as_json()),
|
||||||
OutputMode::ProtocolSpecific => output_result_bson(result.as_original()),
|
OutputMode::ProtocolSpecific => output_result_bson_hex(result.as_original()),
|
||||||
|
},
|
||||||
|
#[cfg(feature = "bson")]
|
||||||
|
OutputFormat::BsonBase64 => match output_mode {
|
||||||
|
OutputMode::Generic => output_result_bson_base64(result.as_json()),
|
||||||
|
OutputMode::ProtocolSpecific => output_result_bson_base64(result.as_original()),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -248,7 +261,7 @@ fn output_result_xml<T: serde::Serialize>(result: T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "bson")]
|
#[cfg(feature = "bson")]
|
||||||
fn output_result_bson<T: serde::Serialize>(result: T) {
|
fn output_result_bson_hex<T: serde::Serialize>(result: T) {
|
||||||
let bson = bson::to_bson(&result).unwrap();
|
let bson = bson::to_bson(&result).unwrap();
|
||||||
|
|
||||||
if let bson::Bson::Document(document) = bson {
|
if let bson::Bson::Document(document) = bson {
|
||||||
|
|
@ -256,7 +269,22 @@ fn output_result_bson<T: serde::Serialize>(result: T) {
|
||||||
|
|
||||||
println!("{}", hex::encode(bytes));
|
println!("{}", hex::encode(bytes));
|
||||||
} else {
|
} else {
|
||||||
panic!("Failed to convert result to BSON");
|
panic!("Failed to convert result to BSON Hex");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "bson")]
|
||||||
|
fn output_result_bson_base64<T: serde::Serialize>(result: T) {
|
||||||
|
use base64::Engine;
|
||||||
|
|
||||||
|
let bson = bson::to_bson(&result).unwrap();
|
||||||
|
|
||||||
|
if let bson::Bson::Document(document) = bson {
|
||||||
|
let bytes = bson::to_vec(&document).unwrap();
|
||||||
|
|
||||||
|
println!("{}", base64::prelude::BASE64_STANDARD.encode(&bytes));
|
||||||
|
} else {
|
||||||
|
panic!("Failed to convert result to BSON Base64");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue