From f855272cdd266d95c4db0ee511cfcb72c0aee4c6 Mon Sep 17 00:00:00 2001 From: Siddharth Chandrasekaran Date: Wed, 13 Mar 2024 23:12:12 +0100 Subject: [PATCH] Fix issues reported by clippy Signed-off-by: Siddharth Chandrasekaran --- libosdp/src/commands.rs | 12 +--- libosdp/src/events.rs | 28 ++++------ libosdp/src/file.rs | 6 +- libosdp/src/pd.rs | 2 +- libosdp/src/pdcap.rs | 8 +-- libosdp/src/pdid.rs | 6 +- libosdp/tests/common/mod.rs | 1 - libosdp/tests/common/unix_channel.rs | 82 ---------------------------- osdpctl/src/config.rs | 2 +- osdpctl/src/main.rs | 2 +- 10 files changed, 27 insertions(+), 122 deletions(-) delete mode 100644 libosdp/tests/common/unix_channel.rs diff --git a/libosdp/src/commands.rs b/libosdp/src/commands.rs index 21ee1a0..54687c9 100644 --- a/libosdp/src/commands.rs +++ b/libosdp/src/commands.rs @@ -280,9 +280,7 @@ impl From for OsdpCommandText { impl From for libosdp_sys::osdp_cmd_text { fn from(value: OsdpCommandText) -> Self { let mut data = [0; libosdp_sys::OSDP_CMD_TEXT_MAX_LEN as usize]; - for i in 0..value.data.len() { - data[i] = value.data[i]; - } + data[..value.data.len()].copy_from_slice(&value.data[..]); libosdp_sys::osdp_cmd_text { reader: value.reader, control_code: value.control_code, @@ -414,9 +412,7 @@ impl From for OsdpCommandKeyset { impl From for libosdp_sys::osdp_cmd_keyset { fn from(value: OsdpCommandKeyset) -> Self { let mut data = [0; libosdp_sys::OSDP_CMD_KEYSET_KEY_MAX_LEN as usize]; - for i in 0..value.data.len() { - data[i] = value.data[i]; - } + data[..value.data.len()].copy_from_slice(&value.data[..]); libosdp_sys::osdp_cmd_keyset { type_: value.key_type, length: value.data.len() as u8, @@ -455,9 +451,7 @@ impl From for OsdpCommandMfg { impl From for libosdp_sys::osdp_cmd_mfg { fn from(value: OsdpCommandMfg) -> Self { let mut data = [0; libosdp_sys::OSDP_CMD_MFG_MAX_DATALEN as usize]; - for i in 0..value.data.len() { - data[i] = value.data[i]; - } + data[..value.data.len()].copy_from_slice(&value.data[..]); libosdp_sys::osdp_cmd_mfg { vendor_code: value.vendor_code.as_le(), command: value.command, diff --git a/libosdp/src/events.rs b/libosdp/src/events.rs index dcbd271..061ee99 100644 --- a/libosdp/src/events.rs +++ b/libosdp/src/events.rs @@ -45,9 +45,9 @@ impl From for OsdpCardFormats { } } -impl Into for OsdpCardFormats { - fn into(self) -> u32 { - match self { +impl From for u32 { + fn from(val: OsdpCardFormats) -> Self { + match val { OsdpCardFormats::Unspecified => { libosdp_sys::osdp_event_cardread_format_e_OSDP_CARD_FMT_RAW_UNSPECIFIED } @@ -118,7 +118,7 @@ impl OsdpEventCardRead { impl From for OsdpEventCardRead { fn from(value: libosdp_sys::osdp_event_cardread) -> Self { - let direction = if value.direction == 1 { true } else { false }; + let direction = value.direction == 1; let format = value.format.into(); let len = value.length as usize; let (nr_bits, nr_bytes) = match format { @@ -143,9 +143,7 @@ impl From for libosdp_sys::osdp_event_cardread { OsdpCardFormats::Weigand => value.nr_bits as i32, _ => value.data.len() as i32, }; - for i in 0..value.data.len() { - data[i] = value.data[i]; - } + data[..value.data.len()].copy_from_slice(&value.data[..]); libosdp_sys::osdp_event_cardread { reader_no: value.reader_no, format: value.format.clone().into(), @@ -192,9 +190,7 @@ impl From for OsdpEventKeyPress { impl From for libosdp_sys::osdp_event_keypress { fn from(value: OsdpEventKeyPress) -> Self { let mut data = [0; libosdp_sys::OSDP_EVENT_KEYPRESS_MAX_DATALEN as usize]; - for i in 0..value.data.len() { - data[i] = value.data[i]; - } + data[..value.data.len()].copy_from_slice(&value.data[..]); libosdp_sys::osdp_event_keypress { reader_no: value.reader_no, length: value.data.len() as i32, @@ -233,9 +229,7 @@ impl From for OsdpEventMfgReply { impl From for libosdp_sys::osdp_event_mfgrep { fn from(value: OsdpEventMfgReply) -> Self { let mut data = [0; libosdp_sys::OSDP_EVENT_MFGREP_MAX_DATALEN as usize]; - for i in 0..value.data.len() { - data[i] = value.data[i]; - } + data[..value.data.len()].copy_from_slice(&value.data[..]); libosdp_sys::osdp_event_mfgrep { vendor_code: value.vendor_code.as_le(), command: value.reply, @@ -282,16 +276,16 @@ impl From for u32 { fn from(value: OsdpStatusReportType) -> Self { match value { OsdpStatusReportType::Input => { - libosdp_sys::osdp_status_report_type_OSDP_STATUS_REPORT_INPUT as u32 + libosdp_sys::osdp_status_report_type_OSDP_STATUS_REPORT_INPUT } OsdpStatusReportType::Output => { - libosdp_sys::osdp_status_report_type_OSDP_STATUS_REPORT_OUTPUT as u32 + libosdp_sys::osdp_status_report_type_OSDP_STATUS_REPORT_OUTPUT } OsdpStatusReportType::Remote => { - libosdp_sys::osdp_status_report_type_OSDP_STATUS_REPORT_REMOTE as u32 + libosdp_sys::osdp_status_report_type_OSDP_STATUS_REPORT_REMOTE } OsdpStatusReportType::Local => { - libosdp_sys::osdp_status_report_type_OSDP_STATUS_REPORT_LOCAL as u32 + libosdp_sys::osdp_status_report_type_OSDP_STATUS_REPORT_LOCAL } } } diff --git a/libosdp/src/file.rs b/libosdp/src/file.rs index ea1c972..986a526 100644 --- a/libosdp/src/file.rs +++ b/libosdp/src/file.rs @@ -34,7 +34,7 @@ unsafe extern "C" fn raw_file_open(data: *mut c_void, file_id: i32, size: *mut i unsafe { *size = ctx.size as i32; } - return 0; + 0 } unsafe extern "C" fn raw_file_read( @@ -54,7 +54,7 @@ unsafe extern "C" fn raw_file_read( Err(_) => -1, }; std::ptr::copy_nonoverlapping(read_buf.as_mut_ptr(), buf as *mut u8, len as usize); - return len; + len } unsafe extern "C" fn raw_file_write( @@ -82,7 +82,7 @@ unsafe extern "C" fn raw_file_close(data: *mut c_void) -> i32 { return -1; } let _ = ctx.file.take().unwrap(); - return 0; + 0 } impl OsdpFile { diff --git a/libosdp/src/pd.rs b/libosdp/src/pd.rs index 73d2876..0b197fb 100644 --- a/libosdp/src/pd.rs +++ b/libosdp/src/pd.rs @@ -98,7 +98,7 @@ impl PeripheralDevice { } /// Set a vector of [`PdCapability`] for this PD. - pub fn set_capabilities(&mut self, cap: &Vec) { + pub fn set_capabilities(&mut self, cap: &[PdCapability]) { let cap: Vec = cap .iter() .map(|c| -> libosdp_sys::osdp_pd_cap { c.clone().into() }) diff --git a/libosdp/src/pdcap.rs b/libosdp/src/pdcap.rs index 365eec1..88a6d7c 100644 --- a/libosdp/src/pdcap.rs +++ b/libosdp/src/pdcap.rs @@ -61,7 +61,7 @@ impl FromStr for PdCapEntity { num_items, }) } else { - return Err(OsdpError::Parse(format!("PdCapEntry: {s}"))); + Err(OsdpError::Parse(format!("PdCapEntry: {s}"))) } } } @@ -249,9 +249,9 @@ impl From for PdCapability { } #[rustfmt::skip] -impl Into for PdCapability { - fn into(self) -> u8 { - match self { +impl From for u8 { + fn from(val: PdCapability) -> Self { + match val { PdCapability::ContactStatusMonitoring(_) => { libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_CONTACT_STATUS_MONITORING as u8 } diff --git a/libosdp/src/pdid.rs b/libosdp/src/pdid.rs index 7889d6d..847f305 100644 --- a/libosdp/src/pdid.rs +++ b/libosdp/src/pdid.rs @@ -57,11 +57,11 @@ impl ConvertEndian for [u8; 4] { ((self[0] as u32) << 24) | ((self[1] as u32) << 16) | ((self[2] as u32) << 8) - | ((self[3] as u32) << 0) + | (self[3] as u32) } fn as_le(&self) -> u32 { - ((self[0] as u32) << 0) + (self[0] as u32) | ((self[1] as u32) << 8) | ((self[2] as u32) << 16) | ((self[3] as u32) << 24) @@ -74,7 +74,7 @@ impl ConvertEndian for (u8, u8, u8) { } fn as_le(&self) -> u32 { - ((self.0 as u32) << 0) | ((self.1 as u32) << 8) | ((self.2 as u32) << 16) + (self.0 as u32) | ((self.1 as u32) << 8) | ((self.2 as u32) << 16) } } diff --git a/libosdp/tests/common/mod.rs b/libosdp/tests/common/mod.rs index 3766263..b0bfca9 100644 --- a/libosdp/tests/common/mod.rs +++ b/libosdp/tests/common/mod.rs @@ -8,7 +8,6 @@ use std::{collections::hash_map::DefaultHasher, hash::{Hash, Hasher}}; pub mod device; pub mod threadbus; pub mod memory_channel; -pub mod unix_channel; pub fn setup() { env_logger::builder() diff --git a/libosdp/tests/common/unix_channel.rs b/libosdp/tests/common/unix_channel.rs deleted file mode 100644 index 092b48b..0000000 --- a/libosdp/tests/common/unix_channel.rs +++ /dev/null @@ -1,82 +0,0 @@ -// -// Copyright (c) 2023-2024 Siddharth Chandrasekaran -// -// SPDX-License-Identifier: Apache-2.0 - -//! OSDP unix channel - -use core::time::Duration; -use std::{ - io::{Read, Write}, - os::unix::net::{UnixListener, UnixStream}, - path::{Path, PathBuf}, - str::FromStr, - thread, -}; - -use libosdp::ChannelError; - -type Result = std::result::Result; - -/// A reference OSDP channel implementation for unix domain socket. -#[derive(Debug)] -pub struct UnixChannel { - id: i32, - stream: UnixStream, -} - -impl UnixChannel { - /// Connect to a channel identified by `name`. - pub fn connect(path: &Path) -> Result { - let id = super::str_to_channel_id(path.as_os_str().try_into().unwrap()); - let stream = UnixStream::connect(&path)?; - Ok(Self { id, stream }) - } - - /// Listen on a channel identified by `name`. - pub fn new(path: &Path) -> Result { - let id = super::str_to_channel_id(path.as_os_str().try_into().unwrap()); - if path.exists() { - std::fs::remove_file(&path)?; - } - let listener = UnixListener::bind(&path)?; - println!("Waiting for connection to unix::{}", path.display()); - let (stream, _) = listener.accept()?; - Ok(Self { id, stream }) - } - - /// Create a bi-directional channel pair. Returns Result<(server, client)> - pub fn pair(name: &str) -> Result<(Self, Self)> { - let path = PathBuf::from_str(format!("/tmp/osdp-{name}.sock").as_str())?; - let path_clone = path.clone(); - let h = thread::spawn(move || { - let path = path_clone; - UnixChannel::new(&path) - }); - thread::sleep(Duration::from_secs(1)); - let client = UnixChannel::connect(&path)?; - let server = h.join().unwrap()?; - Ok((server, client)) - } -} - -impl libosdp::Channel for UnixChannel { - fn get_id(&self) -> i32 { - self.id - } - - fn read(&mut self, buf: &mut [u8]) -> std::prelude::v1::Result { - self.stream.read(buf) - .map_err(ChannelError::from) - } - - fn write(&mut self, buf: &[u8]) -> std::prelude::v1::Result { - self.stream.write(buf) - .map_err(ChannelError::from) - } - - fn flush(&mut self) -> std::prelude::v1::Result<(), libosdp::ChannelError> { - self.stream.flush() - .map_err(ChannelError::from) - } -} diff --git a/osdpctl/src/config.rs b/osdpctl/src/config.rs index d8fab5e..08fe9aa 100644 --- a/osdpctl/src/config.rs +++ b/osdpctl/src/config.rs @@ -111,7 +111,7 @@ impl CpConfig { name: config.get(§ion, "name").unwrap(), channel: config.get(§ion, "channel").unwrap(), address: config.getuint(§ion, "address").unwrap().unwrap() as i32, - key_store: KeyStore::create(runtime_dir.join("key.store"), &key)?, + key_store: KeyStore::create(runtime_dir.join("key.store"), key)?, flags: OsdpFlag::empty(), }); } diff --git a/osdpctl/src/main.rs b/osdpctl/src/main.rs index 7fa0630..441d577 100644 --- a/osdpctl/src/main.rs +++ b/osdpctl/src/main.rs @@ -130,7 +130,7 @@ fn main() -> Result<()> { let config = sub_matches .get_one::("CONFIG") .context("Device config file required")?; - let config = PathBuf::from_str(&config)?; + let config = PathBuf::from_str(config)?; let dev = DeviceConfig::new(&config, &rt_dir)?; let dest_path = cfg_dir.join(format!("{}.cfg", dev.name())); if dest_path.exists() {