aboutsummaryrefslogtreecommitdiffstats
path: root/src/advert.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/advert.rs')
-rw-r--r--src/advert.rs65
1 files changed, 42 insertions, 23 deletions
diff --git a/src/advert.rs b/src/advert.rs
index 24533cf..c37b491 100644
--- a/src/advert.rs
+++ b/src/advert.rs
@@ -23,7 +23,6 @@ impl Advert {
let mut name_split = bytes.chunk().split(|c| *c == 0);
if let Some(name) = name_split.next() {
-
if let Ok(name) = NameString::from_utf8(name.to_vec()) {
Some(name)
} else {
@@ -39,7 +38,6 @@ impl Advert {
let mut name_split = bytes.chunk().split(|c| *c == 0);
if let Some(name) = name_split.next() {
-
if let Ok(name) = NameString::from_utf8(name) {
Some(name)
} else {
@@ -78,42 +76,56 @@ impl From<Bytes> for Advert {
let mut advert = Advert::default();
- if bytes.len() < 32 { return advert }
+ if bytes.len() < 32 {
+ return advert;
+ }
if let Ok(key) = PublicKey::try_from(bytes.split_to(32)) {
advert.public_key = key;
} else {
- return advert
+ return advert;
}
- if bytes.len() < 4 { return advert }
+ if bytes.len() < 4 {
+ return advert;
+ }
if let Some(time) = DateTime::from_timestamp(bytes.get_u32_le() as i64, 0) {
advert.timestamp = time;
}
- if bytes.len() < 64 { return advert }
+ if bytes.len() < 64 {
+ return advert;
+ }
_ = bytes.try_copy_to_slice(&mut advert.signature);
- if bytes.is_empty() { return advert }
+ if bytes.is_empty() {
+ return advert;
+ }
let flags = bytes.get_u8();
advert.node_type = NodeType::from(flags);
if (flags & 0x10) != 0 {
// The location is 8 bytes (4 each for lat and lon)
- if bytes.len() < 8 { return advert }
+ if bytes.len() < 8 {
+ return advert;
+ }
- advert.latitude = Some(bytes.get_i32_le() as f32 / 1_000_000.0);
+ advert.latitude = Some(bytes.get_i32_le() as f32 / 1_000_000.0);
advert.longitude = Some(bytes.get_i32_le() as f32 / 1_000_000.0);
}
if (flags & 0x20) != 0 {
// Feature 1 is 2 bytes when it's included
- if bytes.len() < 2 { return advert }
+ if bytes.len() < 2 {
+ return advert;
+ }
advert.feature1 = Some(bytes.get_u16_le());
}
if (flags & 0x40) != 0 {
// Feature 2 is the same
- if bytes.len() < 2 { return advert }
+ if bytes.len() < 2 {
+ return advert;
+ }
advert.feature2 = Some(bytes.get_u16_le());
}
@@ -124,7 +136,7 @@ impl From<Bytes> for Advert {
// Find only the bytes (characters) before the first null.
advert.name = Self::name_from_bytes(bytes);
-
+
advert
}
}
@@ -140,14 +152,17 @@ impl core::fmt::Display for Advert {
f.write_str("<no name>")?;
}
- f.write_fmt(format_args!("\" ({:2x?}) at: ", self.public_key.hash_prefix() >> 24))?;
+ f.write_fmt(format_args!(
+ "\" ({:2x?}) at: ",
+ self.public_key.hash_prefix() >> 24
+ ))?;
core::fmt::Display::fmt(&self.timestamp, f)?;
match (self.latitude, self.longitude) {
(Some(lat), Some(lon)) => {
f.write_fmt(format_args!(" location: {}, {}", lat, lon))?;
- },
- _ => {},
+ }
+ _ => {}
}
Ok(())
@@ -163,8 +178,8 @@ mod tests {
use hex::decode;
use tinyvec::ArrayVec;
- use crate::packet::*;
use crate::crypto::*;
+ use crate::packet::*;
use crate::packet_content::PacketContent;
use crate::string_helper::name_string_from_slice;
@@ -202,7 +217,7 @@ mod tests {
feature2: None,
name: Some(name_string_from_slice(b"HOWL")),
}),
- incomplete: false,
+ incomplete: false,
};
let rhs_packet = Packet::from_str(sample).unwrap();
@@ -234,21 +249,23 @@ mod tests {
feature2: None,
name: Some(name_string_from_slice(b"hpux735")),
}),
- incomplete: false,
+ incomplete: false,
};
let rhs_packet = Packet::from_str(sample).unwrap();
assert_eq!(lhs_packet, rhs_packet);
}
-
#[test]
fn display() {
let mut signature_slice = [0 as u8; 64];
hex::decode_to_slice("d2d976b687a506dd5325ef526bf3eb52ae687277fcbde9969a5b0087e0eb0f7c1760a50c6a88bec13cc30a2a9b681d713166515e3bbc2bc27f20c0e4d7b67e08", &mut signature_slice).unwrap();
let advert = Advert {
- public_key: PublicKey::from_str("460728508c17ef336412a223144d3a623215162682045c44fef7241af0161923").unwrap(),
+ public_key: PublicKey::from_str(
+ "460728508c17ef336412a223144d3a623215162682045c44fef7241af0161923",
+ )
+ .unwrap(),
timestamp: DateTime::from_timestamp(1762111443, 0).unwrap(),
signature: signature_slice,
node_type: NodeType::Chat,
@@ -260,7 +277,9 @@ mod tests {
};
#[cfg(feature = "std")]
- assert_eq!(format!("{}", advert), "Chat \"HOWL\" (46) at: 2025-11-02 19:24:03 UTC location: 47.98286, -122.132286");
+ assert_eq!(
+ format!("{}", advert),
+ "Chat \"HOWL\" (46) at: 2025-11-02 19:24:03 UTC location: 47.98286, -122.132286"
+ );
}
-
-} \ No newline at end of file
+}