Skip to content

Commit

Permalink
Fix groupAVP indentation for Display trait
Browse files Browse the repository at this point in the history
  • Loading branch information
lwlee2608 committed Mar 10, 2024
1 parent 771bcb7 commit fb068d1
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 109 deletions.
15 changes: 9 additions & 6 deletions src/avp/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,22 @@ impl Grouped {
.map(|avp| avp.get_length() + avp.get_padding() as u32)
.sum()
}
}

// TODO implement indent
impl std::fmt::Display for Grouped {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "\n")?;
pub fn fmt(&self, f: &mut std::fmt::Formatter<'_>, depth: usize) -> std::fmt::Result {
for avp in &self.0 {
write!(f, "{}\n", avp)?;
write!(f, "\n")?;
avp.fmt(f, depth + 1)?;
}
Ok(())
}
}

impl std::fmt::Display for Grouped {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.fmt(f, 0)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
85 changes: 67 additions & 18 deletions src/avp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,24 +140,7 @@ pub enum AvpValue {

impl fmt::Display for AvpValue {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
AvpValue::Address(avp) => avp.fmt(f),
AvpValue::AddressIPv4(avp) => avp.fmt(f),
AvpValue::AddressIPv6(avp) => avp.fmt(f),
AvpValue::Float32(avp) => avp.fmt(f),
AvpValue::Float64(avp) => avp.fmt(f),
AvpValue::Enumerated(avp) => avp.fmt(f),
AvpValue::Integer32(avp) => avp.fmt(f),
AvpValue::Integer64(avp) => avp.fmt(f),
AvpValue::Unsigned32(avp) => avp.fmt(f),
AvpValue::Unsigned64(avp) => avp.fmt(f),
AvpValue::UTF8String(avp) => avp.fmt(f),
AvpValue::OctetString(avp) => avp.fmt(f),
AvpValue::Identity(avp) => avp.fmt(f),
AvpValue::DiameterURI(avp) => avp.fmt(f),
AvpValue::Time(avp) => avp.fmt(f),
AvpValue::Grouped(avp) => avp.fmt(f),
}
self.fmt(f, 0)
}
}

Expand Down Expand Up @@ -203,6 +186,27 @@ impl AvpValue {
AvpValue::Grouped(_) => "Grouped",
}
}

fn fmt(&self, f: &mut fmt::Formatter, depth: usize) -> fmt::Result {
match self {
AvpValue::Address(avp) => write!(f, "{}", avp),
AvpValue::AddressIPv4(avp) => write!(f, "{}", avp),
AvpValue::AddressIPv6(avp) => write!(f, "{}", avp),
AvpValue::Float32(avp) => write!(f, "{}", avp),
AvpValue::Float64(avp) => write!(f, "{}", avp),
AvpValue::Enumerated(avp) => write!(f, "{}", avp),
AvpValue::Integer32(avp) => write!(f, "{}", avp),
AvpValue::Integer64(avp) => write!(f, "{}", avp),
AvpValue::Unsigned32(avp) => write!(f, "{}", avp),
AvpValue::Unsigned64(avp) => write!(f, "{}", avp),
AvpValue::UTF8String(avp) => write!(f, "{}", avp),
AvpValue::OctetString(avp) => write!(f, "{}", avp),
AvpValue::Identity(avp) => write!(f, "{}", avp),
AvpValue::DiameterURI(avp) => write!(f, "{}", avp),
AvpValue::Time(avp) => write!(f, "{}", avp),
AvpValue::Grouped(avp) => avp.fmt(f, depth),
}
}
}

impl From<Identity> for AvpValue {
Expand Down Expand Up @@ -608,6 +612,51 @@ impl Avp {
_ => None,
}
}

pub fn fmt(&self, f: &mut fmt::Formatter<'_>, depth: usize) -> fmt::Result {
let indent = " ".repeat(depth.max(0));

let dict = dictionary::DEFAULT_DICT.read().unwrap();

let avp_name = dict
.get_avp_name(self.get_code() as u32, self.get_vendor_id())
.unwrap_or("Unknown");

let avp_name = format!("{}{}", indent, avp_name);

let vendor_id = match self.get_vendor_id() {
Some(v) => v.to_string(),
None => "".to_string(),
};

write!(
f,
" {:<40} {:>8} {:>5} {} {} {} {:<16} ",
avp_name,
vendor_id,
self.get_code(),
get_bool_unicode(self.get_flags().vendor),
get_bool_unicode(self.get_flags().mandatory),
get_bool_unicode(self.get_flags().private),
self.get_value().get_type_name(),
)?;

self.get_value().fmt(f, depth)
}
}

fn get_bool_unicode(v: bool) -> &'static str {
if v {
"✓"
} else {
"✗"
}
}

impl fmt::Display for Avp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.fmt(f, 0)
}
}

#[macro_export]
Expand Down
146 changes: 61 additions & 85 deletions src/diameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
//! ```

use crate::avp::Avp;
use crate::dictionary;
use crate::error::{Error, Result};
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
Expand Down Expand Up @@ -206,6 +205,24 @@ impl DiameterMessage {

Ok(())
}

fn fmt(&self, f: &mut fmt::Formatter<'_>, depth: usize) -> fmt::Result {
let indent = " ".repeat(depth.max(0));
self.header.fmt(f, depth)?;
write!(f, "\n")?;
write!(
f,
" {}{:<40} {:>8} {:>5} {} {} {} {:<16} {}\n",
indent, "AVP", "Vendor", "Code", "V", "M", "P", "Type", "Value"
)?;

for avp in &self.avps {
avp.fmt(f, depth)?;
write!(f, "\n")?;
}

Ok(())
}
}

impl DiameterHeader {
Expand Down Expand Up @@ -247,6 +264,47 @@ impl DiameterHeader {
})
}

fn fmt(&self, f: &mut fmt::Formatter<'_>, depth: usize) -> fmt::Result {
let indent = " ".repeat(depth.max(0));
let request_flag = if self.flags & flags::REQUEST != 0 {
"Request"
} else {
"Answer"
};
let error_flag = if self.flags & flags::ERROR != 0 {
"Error"
} else {
""
};
let proxyable_flag = if self.flags & flags::PROXYABLE != 0 {
"Proxyable"
} else {
""
};
let retransmit_flag = if self.flags & flags::RETRANSMIT != 0 {
"Retransmit"
} else {
""
};

write!(
f,
"{}{} {}({}) {}({}) {}{}{}{} {}, {}",
indent,
self.version,
self.code,
self.code as u32,
self.application_id,
self.application_id as u32,
request_flag,
error_flag,
proxyable_flag,
retransmit_flag,
self.hop_by_hop_id,
self.end_to_end_id
)
}

/// Encodes the Diameter header to the given writer.
pub fn encode_to<W: Write>(&self, writer: &mut W) -> Result<()> {
// version
Expand Down Expand Up @@ -292,59 +350,13 @@ impl ApplicationId {

impl fmt::Display for DiameterMessage {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}\n", self.header)?;
write!(
f,
" {:<40} {:>8} {:>5} {} {} {} {:<16} {}\n",
"AVP", "Vendor", "Code", "V", "M", "P", "Type", "Value"
)?;

for avp in &self.avps {
write!(f, "{}\n", avp)?;
}

Ok(())
self.fmt(f, 0)
}
}

impl fmt::Display for DiameterHeader {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let request_flag = if self.flags & flags::REQUEST != 0 {
"Request"
} else {
"Answer"
};
let error_flag = if self.flags & flags::ERROR != 0 {
"Error"
} else {
""
};
let proxyable_flag = if self.flags & flags::PROXYABLE != 0 {
"Proxyable"
} else {
""
};
let retransmit_flag = if self.flags & flags::RETRANSMIT != 0 {
"Retransmit"
} else {
""
};

write!(
f,
"{} {}({}) {}({}) {}{}{}{} {}, {}",
self.version,
self.code,
self.code as u32,
self.application_id,
self.application_id as u32,
request_flag,
error_flag,
proxyable_flag,
retransmit_flag,
self.hop_by_hop_id,
self.end_to_end_id
)
self.fmt(f, 0)
}
}

Expand All @@ -360,42 +372,6 @@ impl fmt::Display for ApplicationId {
}
}

impl fmt::Display for Avp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let dict = dictionary::DEFAULT_DICT.read().unwrap();

let avp_name = dict
.get_avp_name(self.get_code() as u32, self.get_vendor_id())
.unwrap_or("Unknown");

let vendor_id = match self.get_vendor_id() {
Some(v) => v.to_string(),
None => "".to_string(),
};

write!(
f,
" {:<40} {:>8} {:>5} {} {} {} {:<16} {}",
avp_name,
vendor_id,
self.get_code(),
get_bool_unicode(self.get_flags().vendor),
get_bool_unicode(self.get_flags().mandatory),
get_bool_unicode(self.get_flags().private),
self.get_value().get_type_name(),
self.get_value().to_string()
)
}
}

fn get_bool_unicode(v: bool) -> &'static str {
if v {
"✓"
} else {
"✗"
}
}

#[cfg(test)]
mod tests {
use crate::avp;
Expand Down

0 comments on commit fb068d1

Please sign in to comment.