Browse Source

query: output ping only when necessary

ipv6
Denis Drakhnia 1 year ago
parent
commit
382062bf59
  1. 27
      query/src/main.rs

27
query/src/main.rs

@ -30,32 +30,29 @@ enum Error {
#[derive(Clone, Debug, Serialize)] #[derive(Clone, Debug, Serialize)]
#[serde(tag = "status")] #[serde(tag = "status")]
#[serde(rename_all = "lowercase")]
enum ServerResultKind { enum ServerResultKind {
#[serde(rename = "ok")]
Ok { Ok {
#[serde(flatten)] #[serde(flatten)]
info: ServerInfo, info: ServerInfo,
}, },
#[serde(rename = "error")]
Error { message: String }, Error { message: String },
#[serde(rename = "invalid")]
Invalid { message: String, response: String }, Invalid { message: String, response: String },
#[serde(rename = "timeout")]
Timeout, Timeout,
#[serde(rename = "protocol")]
Protocol, Protocol,
} }
#[derive(Clone, Debug, Serialize)] #[derive(Clone, Debug, Serialize)]
struct ServerResult { struct ServerResult {
address: SocketAddrV4, address: SocketAddrV4,
ping: f32, #[serde(skip_serializing_if = "Option::is_none")]
ping: Option<f32>,
#[serde(flatten)] #[serde(flatten)]
kind: ServerResultKind, kind: ServerResultKind,
} }
impl ServerResult { impl ServerResult {
fn new(address: SocketAddrV4, ping: f32, kind: ServerResultKind) -> Self { fn new(address: SocketAddrV4, ping: Option<f32>, kind: ServerResultKind) -> Self {
Self { Self {
address, address,
ping, ping,
@ -64,15 +61,15 @@ impl ServerResult {
} }
fn ok(address: SocketAddrV4, ping: f32, info: ServerInfo) -> Self { fn ok(address: SocketAddrV4, ping: f32, info: ServerInfo) -> Self {
Self::new(address, ping, ServerResultKind::Ok { info }) Self::new(address, Some(ping), ServerResultKind::Ok { info })
} }
fn timeout(address: SocketAddrV4) -> Self { fn timeout(address: SocketAddrV4) -> Self {
Self::new(address, 0.0, ServerResultKind::Timeout) Self::new(address, None, ServerResultKind::Timeout)
} }
fn protocol(address: SocketAddrV4, ping: f32) -> Self { fn protocol(address: SocketAddrV4, ping: f32) -> Self {
Self::new(address, ping, ServerResultKind::Protocol) Self::new(address, Some(ping), ServerResultKind::Protocol)
} }
fn error<T>(address: SocketAddrV4, message: T) -> Self fn error<T>(address: SocketAddrV4, message: T) -> Self
@ -81,7 +78,7 @@ impl ServerResult {
{ {
Self::new( Self::new(
address, address,
0.0, None,
ServerResultKind::Error { ServerResultKind::Error {
message: message.to_string(), message: message.to_string(),
}, },
@ -94,7 +91,7 @@ impl ServerResult {
{ {
Self::new( Self::new(
address, address,
ping, Some(ping),
ServerResultKind::Invalid { ServerResultKind::Invalid {
message: message.to_string(), message: message.to_string(),
response: Str(response).to_string(), response: Str(response).to_string(),
@ -458,7 +455,11 @@ fn query_server_info(cli: &Cli, servers: &[String]) -> Result<(), Error> {
} }
} else { } else {
for i in servers { for i in servers {
println!("server: {} [{:.3} ms]", i.address, i.ping); print!("server: {}", i.address);
if let Some(ping) = i.ping {
print!(" [{:.3} ms]", ping);
}
println!();
macro_rules! p { macro_rules! p {
($($key:ident: $value:expr),+ $(,)?) => { ($($key:ident: $value:expr),+ $(,)?) => {

Loading…
Cancel
Save