ty rustfmt

This commit is contained in:
Sijmen 2018-07-29 20:54:12 +02:00
parent 65f87d678f
commit 2e8d262d90

View file

@ -1,18 +1,17 @@
extern crate byteorder;
extern crate glob;
extern crate hex;
extern crate byteorder;
extern crate terminal_size;
use std::fs::{File, read_link};
use std::io::{BufRead, BufReader};
use std::collections::HashMap;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::fmt;
use std::fs::{read_link, File};
use std::io::{BufRead, BufReader};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use byteorder::{ByteOrder, LittleEndian};
use glob::glob;
use terminal_size::{Width, terminal_size};
use terminal_size::{terminal_size, Width};
fn parse_ipv4(ip: &str) -> IpAddr {
let bytes = hex::decode(ip).unwrap();
@ -20,19 +19,18 @@ fn parse_ipv4(ip: &str) -> IpAddr {
IpAddr::V4(ipv4)
}
fn parse_ipv6(ip: &str) -> IpAddr {
let bytes = hex::decode(ip).unwrap();
let mut words = [0; 8];
LittleEndian::read_u16_into(&bytes, &mut words);
let ipv6 = Ipv6Addr::new(words[7], words[6], words[5], words[4],
words[3], words[2], words[1], words[0]);
let ipv6 = Ipv6Addr::new(
words[7], words[6], words[5], words[4], words[3], words[2], words[1], words[0],
);
IpAddr::V6(ipv6)
}
enum Protocol {
Tcp,
Udp,
@ -40,10 +38,12 @@ enum Protocol {
impl fmt::Display for Protocol {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", match self {
let protocol = match self {
Protocol::Tcp => "tcp",
Protocol::Udp => "udp",
})
};
write!(f, "{}", protocol)
}
}
@ -62,7 +62,7 @@ impl<'a> fmt::Display for Address<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.ip {
IpAddr::V4(ip) => write!(f, "{} {}:{}", self.protocol, ip, self.port),
IpAddr::V6(ip) => write!(f, "{} [{}]:{}", self.protocol, ip, self.port)
IpAddr::V6(ip) => write!(f, "{} [{}]:{}", self.protocol, ip, self.port),
}
}
}
@ -81,7 +81,6 @@ impl<'a> INode<'a> {
}
}
fn get_inodes(inodes: &mut HashMap<usize, INode>) {
for fd in glob("/proc/*/fd/*").unwrap() {
let path = match fd {
@ -106,13 +105,15 @@ fn get_inodes(inodes: &mut HashMap<usize, INode>) {
let inode = inode_str[1..(inode_str.len() - 1)].parse().unwrap();
let mut inode = inodes.entry(inode).or_insert_with(INode::new);
let process = Process { pid: pid.unwrap(), command_line: None };
let process = Process {
pid: pid.unwrap(),
command_line: None,
};
inode.processes.push(process);
}
}
}
fn get_command_lines(inodes: &mut HashMap<usize, INode>) {
for inode in inodes.values_mut() {
for process in &mut inode.processes {
@ -128,7 +129,6 @@ fn get_command_lines(inodes: &mut HashMap<usize, INode>) {
}
}
fn get_addresses(inodes: &mut HashMap<usize, INode>) {
for protocol in &[Protocol::Tcp, Protocol::Udp] {
for version in &["", "6"] {
@ -147,7 +147,10 @@ fn get_addresses(inodes: &mut HashMap<usize, INode>) {
}
let address: Vec<_> = fields[1].split(':').collect();
let ip = match *version { "6" => parse_ipv6, _ => parse_ipv4 }(address[0]);
let ip = match *version {
"6" => parse_ipv6,
_ => parse_ipv4,
}(address[0]);
let port = u16::from_str_radix(address[1], 16).unwrap();
let inode_id = fields[9].parse().unwrap();
@ -161,7 +164,6 @@ fn get_addresses(inodes: &mut HashMap<usize, INode>) {
}
}
fn print_all(inodes: &HashMap<usize, INode>) {
let columns = match terminal_size() {
Some((Width(value), _)) => usize::from(value),
@ -175,10 +177,14 @@ fn print_all(inodes: &HashMap<usize, INode>) {
for address in &inode.addresses {
for (i, process) in inode.processes.iter().enumerate() {
let command_line = process.command_line.clone().unwrap_or_default();
let address_str = if i == 0 { format!("{}", address) } else { String::new() };
let address_str = if i == 0 {
format!("{}", address)
} else {
String::new()
};
let mut output = format!(
"{: <45} {: >6} {}", address_str, process.pid, command_line);
let mut output =
format!("{: <45} {: >6} {}", address_str, process.pid, command_line);
output.truncate(columns);
println!("{}", output);
@ -187,7 +193,6 @@ fn print_all(inodes: &HashMap<usize, INode>) {
}
}
fn main() {
let mut inodes: HashMap<usize, INode> = HashMap::new();