From b0eee39f13d76e580a95a6016e75ca0ec1d7e279 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Tue, 11 Aug 2020 12:34:14 +0700 Subject: [PATCH] Add clippy and fmt checks and conform to clippy style --- .github/workflows/style_checks.yaml | 33 +++++++++++++++++++++++++++++ clippy.toml | 1 + src/bc/de.rs | 4 ++-- src/bc/model.rs | 6 ++++++ src/bc_protocol.rs | 3 +-- src/bc_protocol/connection.rs | 4 ++-- src/gst.rs | 10 +++++++-- src/main.rs | 6 +++--- 8 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 .github/workflows/style_checks.yaml create mode 100644 clippy.toml diff --git a/.github/workflows/style_checks.yaml b/.github/workflows/style_checks.yaml new file mode 100644 index 0000000..b1f6a41 --- /dev/null +++ b/.github/workflows/style_checks.yaml @@ -0,0 +1,33 @@ +name: Style + +on: [push, pull_request] + +jobs: + check_clippy: + name: Clippy + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - uses: actions-rs/toolchain@v1 + with: + toolchain: nightly + components: clippy + override: true + - uses: actions-rs/clippy-check@v1 + with: + token: ${{ secrets.GITHUB_TOKEN }} + args: --all-features + + check_fmt: + name: Rust-fmt + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - uses: actions-rs/toolchain@v1 + with: + toolchain: nightly + components: rustfmt + override: true + - name: rustfmt + run: | + cargo +nightly fmt -- --check diff --git a/clippy.toml b/clippy.toml new file mode 100644 index 0000000..3217898 --- /dev/null +++ b/clippy.toml @@ -0,0 +1 @@ +enum-variant-size-threshold = 700 diff --git a/src/bc/de.rs b/src/bc/de.rs index d377157..2a7dfec 100644 --- a/src/bc/de.rs +++ b/src/bc/de.rs @@ -99,14 +99,14 @@ fn hex32<'a>() -> impl Fn(&'a [u8]) -> IResult<&'a [u8], String> { }) } -fn bc_legacy_login_msg<'a, 'b>(buf: &'b [u8]) -> IResult<&'b [u8], LegacyMsg> { +fn bc_legacy_login_msg<'a>(buf: &'a [u8]) -> IResult<&'a [u8], LegacyMsg> { let (buf, username) = hex32()(buf)?; let (buf, password) = hex32()(buf)?; Ok((buf, LegacyMsg::LoginMsg { username, password })) } -fn bc_modern_msg<'a, 'b, 'c>( +fn bc_modern_msg<'a, 'b>( context: &mut BcContext, header: &'a BcHeader, buf: &'b [u8], diff --git a/src/bc/model.rs b/src/bc/model.rs index a94da36..9ddc4e8 100644 --- a/src/bc/model.rs +++ b/src/bc/model.rs @@ -83,6 +83,12 @@ impl Bc { } } +impl Default for BcContext { + fn default() -> BcContext { + Self::new() + } +} + impl BcContext { pub fn new() -> BcContext { BcContext { diff --git a/src/bc_protocol.rs b/src/bc_protocol.rs index 86bed5b..92c8e8b 100644 --- a/src/bc_protocol.rs +++ b/src/bc_protocol.rs @@ -4,7 +4,6 @@ use crate::bc; use crate::bc::{model::*, xml::*}; use err_derive::Error; use log::*; -use md5; use std::io::Write; use std::net::{SocketAddr, ToSocketAddrs}; use std::time::Duration; @@ -111,7 +110,7 @@ impl BcCamera { let md5_username = md5_string(username, ZeroLast); let md5_password = password .map(|p| md5_string(p, ZeroLast)) - .unwrap_or(EMPTY_LEGACY_PASSWORD.to_owned()); + .unwrap_or_else(|| EMPTY_LEGACY_PASSWORD.to_owned()); let legacy_login = Bc { meta: BcMeta { diff --git a/src/bc_protocol/connection.rs b/src/bc_protocol/connection.rs index 16a19a1..7003b6a 100644 --- a/src/bc_protocol/connection.rs +++ b/src/bc_protocol/connection.rs @@ -55,7 +55,7 @@ impl BcConnection { let rx_thread = std::thread::spawn(move || { let mut context = BcContext::new(); - while let Ok(_) = BcConnection::poll(&mut context, &conn, &mut subs) {} + while BcConnection::poll(&mut context, &conn, &mut subs).is_ok() {} }); Ok(BcConnection { @@ -96,7 +96,7 @@ impl BcConnection { let mut locked_subs = subscribers.lock().unwrap(); match locked_subs.entry(msg_id) { Entry::Occupied(mut occ) => { - if let Err(_) = occ.get_mut().send(response) { + if occ.get_mut().send(response).is_err() { // Exceedingly unlikely, unless you mishandle the subscription object warn!("Subscriber to ID {} dropped their channel", msg_id); occ.remove(); diff --git a/src/gst.rs b/src/gst.rs index 47d9521..6cec61e 100644 --- a/src/gst.rs +++ b/src/gst.rs @@ -33,6 +33,12 @@ pub enum StreamFormat { Custom(String), } +impl Default for RtspServer { + fn default() -> RtspServer { + Self::new() + } +} + impl RtspServer { pub fn new() -> RtspServer { gstreamer::init().expect("Gstreamer should not explode"); @@ -144,7 +150,7 @@ impl RtspServer { } pub fn set_credentials(&self, credentials: &[(&str, &str)]) -> Result<()> { - let auth = self.server.get_auth().unwrap_or_else(|| RTSPAuth::new()); + let auth = self.server.get_auth().unwrap_or_else(RTSPAuth::new); auth.set_supported_methods(RTSPAuthMethod::Basic); let mut un_authtoken = RTSPToken::new(&[(*RTSP_TOKEN_MEDIA_FACTORY_ROLE, &"anonymous")]); @@ -164,7 +170,7 @@ impl RtspServer { pub fn set_tls(&self, cert_file: &str, client_auth: TlsAuthenticationMode) -> Result<()> { debug!("Setting up TLS using {}", cert_file); - let auth = self.server.get_auth().unwrap_or_else(|| RTSPAuth::new()); + let auth = self.server.get_auth().unwrap_or_else(RTSPAuth::new); // We seperate reading the file and changing to a PEM so that we get different error messages. let cert_contents = fs::read_to_string(cert_file).expect("TLS file not found"); diff --git a/src/main.rs b/src/main.rs index 0726786..0660aa3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,7 +62,7 @@ fn main() -> Result<(), Error> { let stream_format = match &*camera.format { "h264" | "H264" => StreamFormat::H264, "h265" | "H265" => StreamFormat::H265, - custom_format @ _ => StreamFormat::Custom(custom_format.to_string()), + custom_format => StreamFormat::Custom(custom_format.to_string()), }; // Let subthreads share the camera object; in principle I think they could share @@ -73,7 +73,7 @@ fn main() -> Result<(), Error> { // The substream always seems to be H264, even on B800 cameras let substream_format = match &*arc_cam.format { "h264" | "H264" | "h265" | "H265" => StreamFormat::H264, - custom_format @ _ => StreamFormat::Custom(custom_format.to_string()), + custom_format => StreamFormat::Custom(custom_format.to_string()), }; let permitted_users = get_permitted_users(config.users.as_slice(), &arc_cam.permitted_users); @@ -205,7 +205,7 @@ fn camera_main( let mut connected = false; (|| { let mut camera = BcCamera::new_with_addr(camera_config.camera_addr)?; - if let Some(_) = camera_config.timeout { + if camera_config.timeout.is_some() { warn!("The undocumented `timeout` config option has been removed and is no longer needed."); warn!("Please update your config file."); }