Skip to content

Commit

Permalink
Merge pull request imsnif#22 from ebroto/clippy-in-ci
Browse files Browse the repository at this point in the history
Run clippy and rustfmt in the CI
  • Loading branch information
ebroto authored Dec 18, 2019
2 parents a9d72f3 + 9e6f390 commit d62bf8a
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 16 deletions.
15 changes: 14 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,20 @@ language: rust
rust:
- stable
- beta
- nightly
before_script:
- rustup component add rustfmt clippy
script:
- set -e # Abort on failure, see https://github.com/travis-ci/travis-ci/issues/1066
- cargo fmt -- --check
- cargo build --verbose
- cargo test --verbose
- cargo clippy --all-targets --all-features -- -D warnings
matrix:
include:
- rust: nightly
before_script:
- rustup component add rustfmt
# Fallback to git in case clippy is not available in the current nightly
- rustup component add clippy --toolchain=nightly || cargo install --git https://github.com/rust-lang/rust-clippy/ --force clippy
allow_failures:
- rust: nightly
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
edition = "2018"
2 changes: 1 addition & 1 deletion src/display/components/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl<'a> Table<'a> {
.iter()
.map(|(process_name, data_for_process)| {
vec![
process_name.to_string(),
(*process_name).to_string(),
data_for_process.connection_count.to_string(),
display_upload_and_download(*data_for_process),
]
Expand Down
2 changes: 1 addition & 1 deletion src/display/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ where
ip_to_host: Default::default(),
}
}
pub fn output_text(&mut self, write_to_stdout: &mut Box<dyn FnMut(String) + Send>) {
pub fn output_text(&mut self, write_to_stdout: &mut (dyn FnMut(String) + Send)) {
let state = &self.state;
let ip_to_host = &self.ip_to_host;
let local_time: DateTime<Local> = Local::now();
Expand Down
8 changes: 5 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![deny(clippy::all)]

mod display;
mod network;
mod os;
Expand All @@ -9,6 +11,7 @@ use network::{
dns::{self, IpTable},
Connection, Sniffer, Utilization,
};
use os::OnSigWinch;

use ::pnet::datalink::{DataLinkReceiver, NetworkInterface};
use ::std::collections::HashMap;
Expand Down Expand Up @@ -80,12 +83,12 @@ pub struct OsInputOutput {
pub get_open_sockets: fn() -> HashMap<Connection, String>,
pub keyboard_events: Box<dyn Iterator<Item = Event> + Send>,
pub dns_client: Option<dns::Client>,
pub on_winch: Box<dyn Fn(Box<dyn Fn()>) + Send>,
pub on_winch: Box<OnSigWinch>,
pub cleanup: Box<dyn Fn() + Send>,
pub write_to_stdout: Box<dyn FnMut(String) + Send>,
}

pub fn start<'a, B>(terminal_backend: B, os_input: OsInputOutput, opts: Opt)
pub fn start<B>(terminal_backend: B, os_input: OsInputOutput, opts: Opt)
where
B: Backend + Send + 'static,
{
Expand Down Expand Up @@ -130,7 +133,6 @@ where
.spawn({
let running = running.clone();
let network_utilization = network_utilization.clone();
let ui = ui.clone();
move || {
while running.load(Ordering::Acquire) {
let render_start_time = Instant::now();
Expand Down
4 changes: 2 additions & 2 deletions src/network/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub enum Protocol {
}

impl Protocol {
pub fn from_string(string: &String) -> Option<Self> {
match string.as_str() {
pub fn from_str(string: &str) -> Option<Self> {
match string {
"TCP" => Some(Protocol::Tcp),
"UDP" => Some(Protocol::Udp),
_ => None,
Expand Down
2 changes: 1 addition & 1 deletion src/network/sniffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub enum Direction {
}

impl Direction {
pub fn new(network_interface_ips: &Vec<IpNetwork>, ip_packet: &Ipv4Packet) -> Self {
pub fn new(network_interface_ips: &[IpNetwork], ip_packet: &Ipv4Packet) -> Self {
if network_interface_ips
.iter()
.any(|ip_network| ip_network.ip() == ip_packet.get_source())
Expand Down
2 changes: 1 addition & 1 deletion src/os/lsof_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl RawConnection {
}

pub fn get_protocol(&self) -> Protocol {
return Protocol::from_string(&self.protocol).unwrap();
return Protocol::from_str(&self.protocol).unwrap();
}

pub fn get_ip_address(&self) -> IpAddr {
Expand Down
5 changes: 4 additions & 1 deletion src/os/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ use crate::os::linux::get_open_sockets;
use crate::os::macos::get_open_sockets;
use crate::{network::dns, OsInputOutput};

pub type OnSigWinch = dyn Fn(Box<dyn Fn()>) + Send;
pub type SigCleanup = dyn Fn() + Send;

pub struct KeyboardEvents;

impl Iterator for KeyboardEvents {
Expand Down Expand Up @@ -45,7 +48,7 @@ fn get_interface(interface_name: &str) -> Option<NetworkInterface> {
.find(|iface| iface.name == interface_name)
}

fn sigwinch() -> (Box<dyn Fn(Box<dyn Fn()>) + Send>, Box<dyn Fn() + Send>) {
fn sigwinch() -> (Box<OnSigWinch>, Box<SigCleanup>) {
let signals = Signals::new(&[signal_hook::SIGWINCH]).unwrap();
let on_winch = {
let signals = signals.clone();
Expand Down
2 changes: 1 addition & 1 deletion src/tests/cases/raw_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn build_tcp_packet(
pkt.packet().to_vec()
}

fn format_raw_output<'t>(output: Vec<u8>) -> String {
fn format_raw_output(output: Vec<u8>) -> String {
let stdout_utf8 = String::from_utf8(output).unwrap();
use regex::Regex;
let timestamp = Regex::new(r"<\d+>").unwrap();
Expand Down
11 changes: 7 additions & 4 deletions src/tests/fakes/fake_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ use ::std::task::{Context, Poll};
use ::std::{thread, time};
use ::termion::event::Event;

use crate::network::{
dns::{self, Lookup},
Connection, Protocol,
use crate::{
network::{
dns::{self, Lookup},
Connection, Protocol,
},
os::OnSigWinch,
};

pub struct KeyboardEvents {
Expand Down Expand Up @@ -142,7 +145,7 @@ pub fn get_interface() -> NetworkInterface {
}
}

pub fn create_fake_on_winch(should_send_winch_event: bool) -> Box<dyn Fn(Box<dyn Fn()>) + Send> {
pub fn create_fake_on_winch(should_send_winch_event: bool) -> Box<OnSigWinch> {
Box::new(move |cb| {
if should_send_winch_event {
thread::sleep(time::Duration::from_millis(900));
Expand Down

0 comments on commit d62bf8a

Please sign in to comment.