From 8d45765aa85428ad2db549782bda8a78933a4d16 Mon Sep 17 00:00:00 2001 From: usagi-flow <2804556+usagi-flow@users.noreply.github.com> Date: Sat, 15 Apr 2023 12:15:54 +0200 Subject: [PATCH] Consistent formatting - Format manually with `cargo +nightly fmt` --- rustfmt.toml | 23 +++++++++++++++ src/app.rs | 42 ++++++++++++++------------- src/chunk.rs | 2 +- src/main.rs | 80 ++++++++++++++++++++++++++++------------------------ 4 files changed, 89 insertions(+), 58 deletions(-) create mode 100644 rustfmt.toml diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..73335a7 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,23 @@ +# See: https://rust-lang.github.io/rustfmt + +# Check: +# cargo +nightly fmt --check + +# Install the nightly toolchain (required for the unstable features): +# rustup toolchain install nightly + +unstable_features = true # For now, unstable features are unavailable on a stable rust channel + # https://github.com/rust-lang/rustfmt/issues/3387 + +hard_tabs = true +max_width = 120 # Default is 100 +chain_width = 100 # Default is 60 +fn_call_width = 100 # Default is 60 +single_line_if_else_max_width = 80 # Default is 50 +brace_style = "AlwaysNextLine" # Unstable, default is "SameLineWhere" +control_brace_style = "ClosingNextLine" # Unstable, default is "AlwaysSameLine" +imports_granularity = "Module" # Unstable, default is "Preserve" +reorder_impl_items = true # Unstable +trailing_comma = "Never" # Unstable, default is "Vertical" +#reorder_impl_items = true # Unstable +newline_style = "Unix" # Default is "Auto" \ No newline at end of file diff --git a/src/app.rs b/src/app.rs index a8b4181..65bb6e1 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,13 +1,10 @@ use std::net::SocketAddr; -use tokio::io::AsyncReadExt; -use tokio::io::AsyncWriteExt; -use tokio::net::TcpListener; -use tokio::net::TcpStream; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; +use tokio::net::{TcpListener, TcpStream}; use tokio::sync::watch; -use tokio::sync::watch::Receiver; -use tokio::sync::watch::Sender; use tokio::sync::watch::error::SendError; +use tokio::sync::watch::{Receiver, Sender}; use tokio::time::sleep; use tokio_serial::SerialPortBuilderExt; @@ -20,9 +17,7 @@ lazy_static! { }; } -pub struct App -{ -} +pub struct App {} impl App { @@ -66,8 +61,11 @@ impl App } }); - log::info!("Listening for connections on {}:{}...", - Config::get().await.bind_address, Config::get().await.bind_port); + log::info!( + "Listening for connections on {}:{}...", + Config::get().await.bind_address, + Config::get().await.bind_port + ); loop { let (socket, peer_address) = listener.accept().await?; @@ -81,10 +79,12 @@ impl App } } - async fn serve(&self, + async fn serve( + &self, mut socket: TcpStream, peer_address: SocketAddr, - mut receiver: Receiver) -> Result<(), std::io::Error> + mut receiver: Receiver + ) -> Result<(), std::io::Error> { log::info!("{:?} connected", peer_address); @@ -110,8 +110,7 @@ impl App } else { // The sender has been dropped; disconnect gracefully from the peer. - log::warn!("Serial data sender has been dropped, disconnecting from peer: {:?}", - peer_address); + log::warn!("Serial data sender has been dropped, disconnecting from peer: {:?}", peer_address); socket.shutdown().await?; let result: Result<(), std::io::Error> = Ok(()); @@ -125,7 +124,7 @@ impl App // Quickly copy the data into a vec and release the borrowed chunk // TODO: use borrow_and_update() instead? let chunk = receiver_instance.borrow(); - return chunk.data[0 .. chunk.size].to_vec(); + return chunk.data[0..chunk.size].to_vec(); } pub async fn read_serial(&self, sender: &Sender) -> Result @@ -170,10 +169,13 @@ impl App size: bytes }; - sender.send(chunk).or_else(|_: SendError| -> Result<(), ()> { - log::warn!("Failed to transfer obtained serial data to the TCP connection(s)"); - return Ok(()); - }).unwrap(); + sender + .send(chunk) + .or_else(|_: SendError| -> Result<(), ()> { + log::warn!("Failed to transfer obtained serial data to the TCP connection(s)"); + return Ok(()); + }) + .unwrap(); } else { // No more data diff --git a/src/chunk.rs b/src/chunk.rs index 259aeb6..3a4974b 100644 --- a/src/chunk.rs +++ b/src/chunk.rs @@ -11,6 +11,6 @@ impl Default for Chunk return Chunk { data: [0u8; 1024], size: 0usize - } + }; } } diff --git a/src/main.rs b/src/main.rs index 94e8c2e..7f22a73 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ mod config; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use app::App; -use clap::{Arg, command}; +use clap::{command, Arg}; use crate::config::Config; @@ -20,7 +20,7 @@ struct CLIContext<'a> #[tokio::main] async fn main() { - simple_logger::SimpleLogger::new().with_level(log::LevelFilter::Info) .env().init().unwrap(); + simple_logger::SimpleLogger::new().with_level(log::LevelFilter::Info).env().init().unwrap(); let default_panic = std::panic::take_hook(); std::panic::set_hook(Box::new(move |info| { @@ -37,7 +37,7 @@ async fn main() Ok(()) => { let app = App::instance(); app.run().await.unwrap(); - }, + } Err(e) => { log::error!("{}\n", e); context.command.print_help().unwrap(); @@ -48,40 +48,46 @@ async fn main() async fn init_cli<'a>(context: &mut CLIContext<'a>) -> Result<(), &'a str> { context.command = command!() - .arg(Arg::new("no-polling") - .short('n') - .takes_value(false) - .display_order(0) - .help("(optional) If set, do not poll the serial device: \ - If the device is/becomes unavailable, terminate immediately.")) - .arg(Arg::new("serial-device-path") - .long("serial-device") - .short('s') - .required(true) - .value_name("path") - .display_order(1) - .help("The serial device to read from, e.g. /dev/ttyUSB0")) - .arg(Arg::new("serial-baud-rate") - .long("baud-rate") - .short('b') - .required(true) - .value_name("number") - .display_order(2) - .help("The serial baud rate to use, e.g. 115200")) - .arg(Arg::new("address") - .long("address") - .short('a') - .default_value("0.0.0.0") - .value_name("ip") - .display_order(3) - .help("The IP (v4 or v6) address to bind to")) - .arg(Arg::new("port") - .long("port") - .short('p') - .required(true) - .value_name("number") - .display_order(4) - .help("The port to listen on")); + .arg(Arg::new("no-polling").short('n').takes_value(false).display_order(0).help( + "(optional) If set, do not poll the serial device: \ + If the device is/becomes unavailable, terminate immediately." + )) + .arg( + Arg::new("serial-device-path") + .long("serial-device") + .short('s') + .required(true) + .value_name("path") + .display_order(1) + .help("The serial device to read from, e.g. /dev/ttyUSB0") + ) + .arg( + Arg::new("serial-baud-rate") + .long("baud-rate") + .short('b') + .required(true) + .value_name("number") + .display_order(2) + .help("The serial baud rate to use, e.g. 115200") + ) + .arg( + Arg::new("address") + .long("address") + .short('a') + .default_value("0.0.0.0") + .value_name("ip") + .display_order(3) + .help("The IP (v4 or v6) address to bind to") + ) + .arg( + Arg::new("port") + .long("port") + .short('p') + .required(true) + .value_name("number") + .display_order(4) + .help("The port to listen on") + ); let matches = context.command.clone().get_matches();