From 2e8d262d9087b13d86b7082156151541cea7c22b Mon Sep 17 00:00:00 2001 From: Sijmen Schoon Date: Sun, 29 Jul 2018 20:54:12 +0200 Subject: [PATCH] ty rustfmt --- src/main.rs | 51 ++++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/src/main.rs b/src/main.rs index e09f338..693133e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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) { for fd in glob("/proc/*/fd/*").unwrap() { let path = match fd { @@ -106,13 +105,15 @@ fn get_inodes(inodes: &mut HashMap) { 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) { for inode in inodes.values_mut() { for process in &mut inode.processes { @@ -128,7 +129,6 @@ fn get_command_lines(inodes: &mut HashMap) { } } - fn get_addresses(inodes: &mut HashMap) { for protocol in &[Protocol::Tcp, Protocol::Udp] { for version in &["", "6"] { @@ -147,7 +147,10 @@ fn get_addresses(inodes: &mut HashMap) { } 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) { } } - fn print_all(inodes: &HashMap) { let columns = match terminal_size() { Some((Width(value), _)) => usize::from(value), @@ -175,10 +177,14 @@ fn print_all(inodes: &HashMap) { 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) { } } - fn main() { let mut inodes: HashMap = HashMap::new();