Commit 9d233df3 authored by Eonm's avatar Eonm
Browse files

Impl fmt::Display

parent 238870d3
use std::fmt;
use crate::attr::Nl80211Attr;
use crate::attr::Nl80211Bss;
use crate::nl80211traits::ParseNlAttr;
use crate::nl80211traits::PrettyFormat;
use crate::parse_attr::{parse_hex, parse_i32, parse_u16, parse_u32};
use neli::nlattr::AttrHandle;
......@@ -34,8 +34,8 @@ impl Bss {
}
}
impl PrettyFormat for Bss {
fn pretty_format(&self) -> String {
impl fmt::Display for Bss {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut result = Vec::new();
if let Some(bssid) = &self.bssid {
......@@ -71,7 +71,7 @@ impl PrettyFormat for Bss {
))
};
result.join("\n")
write!(f, "{}", result.join("\n"))
}
}
......@@ -135,7 +135,7 @@ mod test_bss {
signal : -53.0 dBm"#;
assert_eq!(
bss.pretty_format(),
format!("{}", bss),
expected_output.replace("\n ", "\n")
)
}
......
use std::fmt;
use crate::attr::*;
use crate::nl80211traits::ParseNlAttr;
use crate::nl80211traits::PrettyFormat;
use crate::parse_attr::parse_u32;
use crate::parse_attr::parse_u64;
use crate::socket::Socket;
......@@ -86,8 +86,8 @@ impl ParseNlAttr for Interface {
}
}
impl PrettyFormat for Interface {
fn pretty_format(&self) -> String {
impl fmt::Display for Interface {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut result = Vec::new();
if let Some(ssid) = &self.ssid {
......@@ -125,7 +125,7 @@ impl PrettyFormat for Interface {
result.push(format!("device : {}", parse_u64(device)))
};
result.join("\n")
write!(f, "{}", result.join("\n"))
}
}
......@@ -159,7 +159,7 @@ mod test_interface {
device : 1"#;
assert_eq!(
interface.pretty_format(),
format!("{}", interface),
expected_output.replace("\n ", "\n")
)
}
......
......@@ -26,7 +26,6 @@
//! extern crate neli;
//!
//! use nl80211::Socket;
//! use nl80211::PrettyFormat;
//!
//! fn main() -> Result<(), neli::err::NlError> {
//! let interfaces = Socket::connect()?.get_interfaces_info()?;
......@@ -46,7 +45,7 @@
//! // device: Some([1, 0, 0, 0, 0, 0, 0, 0])
//! // }
//!
//! println!("{}", interface.pretty_format());
//! println!("{}", interface);
//!
//! // essid : eduroam
//! // mac : FF:FF:FF:FF:FF:FF
......@@ -69,13 +68,12 @@
//! extern crate neli;
//!
//! use nl80211::Socket;
//! use nl80211::PrettyFormat;
//!
//! fn main() -> Result<(), neli::err::NlError> {
//! let interfaces = Socket::connect()?.get_interfaces_info()?;
//! for interface in interfaces {
//! let station = interface.get_station_info();
//! println!("{}", station?.pretty_format());
//! println!("{}", station?);
//!
//! // bssid : FF:FF:FF:FF:FF:FF
//! // connected time : 35.816666 minutes
......
use crate::attr::Nl80211Attr;
use neli::nlattr::AttrHandle;
/// Format and convert network information to human readable format
pub trait PrettyFormat {
fn pretty_format(&self) -> String;
}
/// Parse netlink messages attributes returned by a nl80211 command
pub trait ParseNlAttr {
fn parse(&mut self, handle: AttrHandle<Nl80211Attr>) -> Self;
......
......@@ -24,7 +24,7 @@ impl Socket {
/// # Example
///
/// ```no_run
/// # use nl80211::{Socket, Nl80211Attr, Nl80211Cmd, NL_80211_GENL_VERSION, PrettyFormat};
/// # use nl80211::{Socket, Nl80211Attr, Nl80211Cmd, NL_80211_GENL_VERSION};
/// # use neli::nlattr::Nlattr;
/// # use neli::genl::Genlmsghdr;
/// # use neli::nl::Nlmsghdr;
......@@ -92,12 +92,12 @@ impl Socket {
/// # Example
///
/// ```no_run
/// # use nl80211::{Socket, PrettyFormat};
/// # use nl80211::Socket;
///
/// # fn main() -> Result<(), neli::err::NlError>{
/// let wifi_interfaces = Socket::connect()?.get_interfaces_info();
/// for wifi_interface in wifi_interfaces? {
/// println!("{}", wifi_interface.pretty_format());
/// println!("{}", wifi_interface);
/// }
/// # Ok(())
/// # }
......@@ -142,7 +142,7 @@ impl Socket {
/// # Example
///
/// ```no_run
/// # use nl80211::{Socket, PrettyFormat};
/// # use nl80211::Socket;
///
/// # fn main() -> Result<(), neli::err::NlError>{
/// // First of all we need to get wifi interface information to get more data
......@@ -152,7 +152,7 @@ impl Socket {
///
/// // Then for each wifi interface we can fetch station information
/// let station_info = Socket::connect()?.get_station_info(&netlink_index.clone())?;
/// println!("{}", station_info.pretty_format());
/// println!("{}", station_info);
/// }
/// }
/// # Ok(())
......
use std::fmt;
use crate::attr::{Nl80211Attr, Nl80211StaInfo};
use crate::nl80211traits::*;
use crate::parse_attr::{parse_hex, parse_i8, parse_u32, parse_u8};
......@@ -119,8 +120,8 @@ impl ParseNlAttr for Station {
}
}
impl PrettyFormat for Station {
fn pretty_format(&self) -> String {
impl fmt::Display for Station {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut result = Vec::new();
if let Some(bssid) = &self.bssid {
......@@ -170,7 +171,7 @@ impl PrettyFormat for Station {
result.push(format!("tx failed : {}", parse_u32(tx_failed)))
}
result.join("\n")
write!(f, "{}", result.join("\n"))
}
}
......@@ -210,7 +211,7 @@ mod tests_station {
tx failed : 45"#;
assert_eq!(
station.pretty_format(),
format!("{}", station),
expected_output.replace("\n ", "\n")
)
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment