display
This commit is contained in:
44
src/main.rs
44
src/main.rs
@@ -5,15 +5,16 @@ type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CapCode {
|
||||
capcode: &'static str,
|
||||
region: &'static str,
|
||||
location: &'static str,
|
||||
service: &'static str,
|
||||
description: &'static str,
|
||||
pub capcode: &'static str,
|
||||
pub region: &'static str,
|
||||
pub location: &'static str,
|
||||
pub service: &'static str,
|
||||
pub description: &'static str,
|
||||
}
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/capcodes.rs"));
|
||||
|
||||
#[allow(unused)]
|
||||
#[derive(Debug)]
|
||||
struct Message {
|
||||
raw: String,
|
||||
@@ -21,11 +22,41 @@ struct Message {
|
||||
timestamp: String,
|
||||
flags: String,
|
||||
frameid: String,
|
||||
capcodes_raw: String,
|
||||
capcodes: Vec<&'static CapCode>,
|
||||
format: 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 {
|
||||
type Err = Error;
|
||||
|
||||
@@ -73,6 +104,7 @@ impl FromStr for Message {
|
||||
timestamp: timestamp.to_string(),
|
||||
flags: flags.to_string(),
|
||||
frameid: frameid.to_string(),
|
||||
capcodes_raw: capcodes.to_string(),
|
||||
capcodes: cc,
|
||||
format: format.to_string(),
|
||||
message: message.trim().to_string(),
|
||||
@@ -96,7 +128,7 @@ fn main() -> Result<()> {
|
||||
|
||||
fn process(s: &str) -> Result<Message> {
|
||||
let message = Message::from_str(s)?;
|
||||
dbg!(&message);
|
||||
println!("{}", &message);
|
||||
|
||||
Ok(message)
|
||||
}
|
||||
|
Reference in New Issue
Block a user