From 610370f5652bd3ce96647cf35b2888f6d94b49ab Mon Sep 17 00:00:00 2001 From: Louis Thiery Date: Wed, 17 Apr 2024 08:56:34 -0700 Subject: [PATCH] handle edge-case of less than 7 digits --- src/packet.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/packet.rs b/src/packet.rs index fcb491d7..f3d4d089 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -50,16 +50,23 @@ impl From for PacketDown { impl fmt::Display for PacketUp { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let mut decimal = self.0.frequency.to_string(); - // insert period before the last 6 digits - decimal.insert(decimal.len() - 6, '.'); - // remove trailing 0's - decimal = decimal.trim_end_matches('0').to_string(); - + let mut frequency = self.0.frequency.to_string(); + if frequency.len() > 6 { + // insert period before the last 6 digits + frequency.insert(frequency.len() - 6, '.'); + // remove trailing 0's + frequency = frequency.trim_end_matches('0').to_string(); + // append MHz to the string + frequency.push_str(" MHz"); + } else { + // return in Hz + frequency.push_str(" Hz"); + } + f.write_fmt(format_args!( "@{} us, {} MHz, {:?}, snr: {}, rssi: {}, len: {}", self.0.timestamp, - decimal, + frequency, self.0.datarate(), self.0.snr, self.0.rssi,