This commit is contained in:
2025-03-11 16:26:51 +01:00
parent 9d1a79d6f5
commit 1e63c31115

View File

@@ -5,15 +5,16 @@ type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)] #[derive(Debug)]
pub struct CapCode { pub struct CapCode {
capcode: &'static str, pub capcode: &'static str,
region: &'static str, pub region: &'static str,
location: &'static str, pub location: &'static str,
service: &'static str, pub service: &'static str,
description: &'static str, pub description: &'static str,
} }
include!(concat!(env!("OUT_DIR"), "/capcodes.rs")); include!(concat!(env!("OUT_DIR"), "/capcodes.rs"));
#[allow(unused)]
#[derive(Debug)] #[derive(Debug)]
struct Message { struct Message {
raw: String, raw: String,
@@ -21,11 +22,41 @@ struct Message {
timestamp: String, timestamp: String,
flags: String, flags: String,
frameid: String, frameid: String,
capcodes_raw: String,
capcodes: Vec<&'static CapCode>, capcodes: Vec<&'static CapCode>,
format: String, format: String,
message: String, message: String,
} }
impl std::fmt::Display for Message {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.capcodes.is_empty() {
write!(f, "[{}]: {}", self.capcodes_raw, self.message)?;
return Ok(());
}
let mut iterator = self.capcodes.iter().peekable();
while let Some(code) = iterator.next() {
write!(f, "[{}]", code.service)?;
if !code.region.is_empty() {
write!(f, "[{}]", code.region.trim())?;
}
if !code.location.is_empty() {
write!(f, "[{}]", code.location.trim())?;
}
if !code.description.is_empty() {
write!(f, "[{}]", code.description.trim())?;
}
write!(f, ": ")?;
write!(f, "{}", self.message)?;
if iterator.peek().is_some() {
writeln!(f)?;
}
}
Ok(())
}
}
impl FromStr for Message { impl FromStr for Message {
type Err = Error; type Err = Error;
@@ -73,6 +104,7 @@ impl FromStr for Message {
timestamp: timestamp.to_string(), timestamp: timestamp.to_string(),
flags: flags.to_string(), flags: flags.to_string(),
frameid: frameid.to_string(), frameid: frameid.to_string(),
capcodes_raw: capcodes.to_string(),
capcodes: cc, capcodes: cc,
format: format.to_string(), format: format.to_string(),
message: message.trim().to_string(), message: message.trim().to_string(),
@@ -96,7 +128,7 @@ fn main() -> Result<()> {
fn process(s: &str) -> Result<Message> { fn process(s: &str) -> Result<Message> {
let message = Message::from_str(s)?; let message = Message::from_str(s)?;
dbg!(&message); println!("{}", &message);
Ok(message) Ok(message)
} }