Skip to content

Commit

Permalink
clippy::pedantic
Browse files Browse the repository at this point in the history
  • Loading branch information
RainerZ committed Oct 19, 2024
1 parent ceff3ac commit ca35f33
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 83 deletions.
35 changes: 26 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#![cfg(not(doctest))]
//#![warn(missing_docs)]

//-----------------------------------------------------------------------------
// Crate xcp
// Path: src/lib.rs
Expand All @@ -9,6 +6,31 @@
// Note that the tests can not be executed in parallel
// Use cargo test -- --test-threads=1 --features=serde --nocapture

// This crate is a library
#![crate_type = "lib"]
// The library crate is named "xcp"
#![crate_name = "xcp"]
// Disabled clippy lints
#![warn(clippy::pedantic)]
#![allow(clippy::doc_markdown)]
#![allow(clippy::missing_errors_doc)]
#![allow(clippy::missing_panics_doc)]
#![allow(clippy::must_use_candidate)]
#![allow(clippy::uninlined_format_args)]
#![allow(clippy::module_name_repetitions)]
#![allow(clippy::struct_field_names)]
#![allow(clippy::unreadable_literal)]
#![allow(clippy::if_not_else)]
#![allow(clippy::wildcard_imports)]
#![allow(clippy::cast_lossless)]
//
#![allow(clippy::ref_as_ptr)]
#![allow(clippy::ptr_as_ptr)]
#![allow(clippy::cast_possible_wrap)]
#![allow(clippy::trivially_copy_pass_by_ref)]
//
#![cfg(not(doctest))]
/*
//! A lightweight XCP on Ethernet implementation
//! The 'xcp' crate provides an XCP on ETH implementation,a wrapper type for calibration variables and
//! a registry to describe events, meaesurement and calibration objects for A2L generation.
Expand Down Expand Up @@ -73,12 +95,7 @@
//!
//!
//!

// This crate is a library
#![crate_type = "lib"]
// The library crate is named "xcp"
#![crate_name = "xcp"]

*/
//-----------------------------------------------------------------------------

// Submodule xcp
Expand Down
76 changes: 31 additions & 45 deletions src/reg/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,40 +60,35 @@ pub enum RegistryDataType {
impl RegistryDataType {
/// Get minimum value for data type
/// Used by the register macros
pub fn get_min(&self) -> f64 {
pub fn get_min(self) -> f64 {
match self {
RegistryDataType::Sbyte => -128.0,
RegistryDataType::Sword => -32768.0,
RegistryDataType::Slong => -2147483648.0,
RegistryDataType::AInt64 => -1e12,
RegistryDataType::Float32Ieee => -1e12,
RegistryDataType::Float64Ieee => -1e12,
RegistryDataType::Sbyte => i8::MIN as f64,
RegistryDataType::Sword => i16::MIN as f64,
RegistryDataType::Slong => i32::MIN as f64,
RegistryDataType::AInt64 | RegistryDataType::Float32Ieee | RegistryDataType::Float64Ieee => f32::MIN as f64,
_ => 0.0,
}
}

/// Get maximum value for data type
/// Used by the register macros
pub fn get_max(&self) -> f64 {
pub fn get_max(self) -> f64 {
match self {
RegistryDataType::Ubyte => 255.0,
RegistryDataType::Uword => 65535.0,
RegistryDataType::Ulong => 4294967295.0,
RegistryDataType::AUint64 => 1e12,
RegistryDataType::Sbyte => 127.0,
RegistryDataType::Sword => 32767.0,
RegistryDataType::Slong => 2147483647.0,
RegistryDataType::AInt64 => 1e12,
RegistryDataType::Float32Ieee => 1e12,
RegistryDataType::Float64Ieee => 1e12,
RegistryDataType::Ubyte => u8::MAX as f64,
RegistryDataType::Sbyte => i8::MAX as f64,
RegistryDataType::Uword => u16::MAX as f64,
RegistryDataType::Sword => i16::MAX as f64,
RegistryDataType::Ulong => u32::MAX as f64,
RegistryDataType::Slong => i32::MAX as f64,
RegistryDataType::AUint64 | RegistryDataType::AInt64 | RegistryDataType::Float32Ieee | RegistryDataType::Float64Ieee => f32::MAX as f64,
RegistryDataType::Blob => 0.0,
_ => panic!("get_max: Unsupported data type"),
RegistryDataType::Unknown => panic!("get_max: Unsupported data type"),
}
}

// Get data type as str
// Used by A2L writer
fn get_type_str(&self) -> &'static str {
fn get_type_str(self) -> &'static str {
match self {
RegistryDataType::Ubyte => "UBYTE",
RegistryDataType::Uword => "UWORD",
Expand All @@ -106,13 +101,13 @@ impl RegistryDataType {
RegistryDataType::Float32Ieee => "FLOAT32_IEEE",
RegistryDataType::Float64Ieee => "FLOAT64_IEEE",
RegistryDataType::Blob => "BLOB",
_ => panic!("get_type_str: Unsupported data type"),
RegistryDataType::Unknown => panic!("get_type_str: Unsupported data type"),
}
}

// Get data type as str for A2L deposit
// Used by A2L writer
fn get_deposit_str(&self) -> &'static str {
fn get_deposit_str(self) -> &'static str {
match self {
RegistryDataType::Ubyte => "U8",
RegistryDataType::Uword => "U16",
Expand All @@ -125,44 +120,35 @@ impl RegistryDataType {
RegistryDataType::Float32Ieee => "F32",
RegistryDataType::Float64Ieee => "F64",
RegistryDataType::Blob => "BLOB",
_ => panic!("get_deposit_str: Unsupported data type"),
RegistryDataType::Unknown => panic!("get_deposit_str: Unsupported data type"),
}
}

/// Get data type size
/// Used by the register macros
pub fn get_size(&self) -> usize {
pub fn get_size(self) -> usize {
match self {
RegistryDataType::Ubyte => 1,
RegistryDataType::Uword => 2,
RegistryDataType::Ulong => 4,
RegistryDataType::AUint64 => 8,
RegistryDataType::Sbyte => 1,
RegistryDataType::Sword => 2,
RegistryDataType::Slong => 4,
RegistryDataType::AInt64 => 8,
RegistryDataType::Float32Ieee => 4,
RegistryDataType::Float64Ieee => 8,
RegistryDataType::Ubyte | RegistryDataType::Sbyte => 1,
RegistryDataType::Uword | RegistryDataType::Sword => 2,
RegistryDataType::Ulong | RegistryDataType::Slong | RegistryDataType::Float32Ieee => 4,
RegistryDataType::AUint64 | RegistryDataType::AInt64 | RegistryDataType::Float64Ieee => 8,
RegistryDataType::Blob => 0,
_ => panic!("get_size: Unsupported data type"),
RegistryDataType::Unknown => panic!("get_size: Unsupported data type"),
}
}

/// Convert from Rust basic type as str
/// Used by the register macros
pub fn from_rust_basic_type(s: &str) -> RegistryDataType {
match s {
"bool" => RegistryDataType::Ubyte,
"u8" => RegistryDataType::Ubyte,
"u16" => RegistryDataType::Uword,
"u32" => RegistryDataType::Ulong,
"u64" => RegistryDataType::AUint64,
"usize" => RegistryDataType::AUint64, // @@@@ Check if usize is correct
"bool" | "u8" => RegistryDataType::Ubyte,
"i8" => RegistryDataType::Sbyte,
"u16" => RegistryDataType::Uword,
"i16" => RegistryDataType::Sword,
"u32" => RegistryDataType::Ulong,
"i32" => RegistryDataType::Slong,
"i64" => RegistryDataType::AInt64,
"isize" => RegistryDataType::AInt64, // @@@@ Check if isize is correct
"u64" | "usize" => RegistryDataType::AUint64, // @@@@ Check if usize is correct
"i64" | "isize" => RegistryDataType::AInt64, // @@@@ Check if isize is correct
"f32" => RegistryDataType::Float32Ieee,
"f64" => RegistryDataType::Float64Ieee,
_ => RegistryDataType::Unknown,
Expand Down Expand Up @@ -334,7 +320,7 @@ impl RegistryEventList {
self.0.iter()
}
fn get_name(&self, xcp_event: XcpEvent) -> Option<&'static str> {
for event in self.0.iter() {
for event in &self.0 {
if event.xcp_event == xcp_event {
return Some(event.name);
}
Expand Down Expand Up @@ -727,7 +713,7 @@ impl Registry {

// Append event index to name in case of a multi instance event (index>0)
if m.xcp_event.get_index() > 0 {
m.name = format!("{}_{}", m.name, m.xcp_event.get_index())
m.name = format!("{}_{}", m.name, m.xcp_event.get_index());
}

// Panic if symbol_name with same name already exists
Expand Down
10 changes: 5 additions & 5 deletions src/reg/registry/a2l_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl GenerateA2l for RegistryMeasurement {
"#
)?;
} else {
if self.factor != 1.0 || self.offset != 0.0 || !self.unit.is_empty() {
if (self.factor - 1.0).abs() <= f64::EPSILON || self.offset != 0.0 || !self.unit.is_empty() {
writeln!(writer, r#"/begin COMPU_METHOD {name}.Conv "" LINEAR "%6.3" "{unit}" COEFFS_LINEAR {factor} {offset} /end COMPU_METHOD"#)?;
write!(
writer,
Expand Down Expand Up @@ -193,7 +193,7 @@ impl GenerateA2l for RegistryMeasurement {
if self.datatype == RegistryDataType::Blob {
writeln!(writer, r#" /end CHARACTERISTIC"#)?;
} else {
writeln!(writer, r#" /end MEASUREMENT"#)?
writeln!(writer, r#" /end MEASUREMENT"#)?;
};

Ok(())
Expand Down Expand Up @@ -419,7 +419,7 @@ impl<'a> A2lWriter<'a> {

// Eventlist
for e in self.registry.event_list.iter() {
e.write_a2l(self)?
e.write_a2l(self)?;
}

write!(self, "\n\t\t\t/end DAQ\n")?;
Expand Down Expand Up @@ -501,10 +501,10 @@ impl<'a> A2lWriter<'a> {

fn write_a2l_tail(&mut self) -> std::io::Result<()> {
self.write_all(
r#"
"
/end MODULE
/end PROJECT
"#
"
.as_bytes(),
)
}
Expand Down
26 changes: 20 additions & 6 deletions src/xcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl From<u8> for XcpLogLevel {
match item {
0 => XcpLogLevel::Off,
1 => XcpLogLevel::Error,
2 => XcpLogLevel::Warn,
//2 => XcpLogLevel::Warn,
3 => XcpLogLevel::Info,
4 => XcpLogLevel::Debug,
5 => XcpLogLevel::Trace,
Expand All @@ -85,7 +85,7 @@ impl From<u8> for XcpLogLevel {
use log::{debug, error, info, trace, warn};

impl XcpLogLevel {
/// Convert XcpLogLevel to log::LevelFilter
/// Convert XcpLogLevel to 'log::LevelFilter'
pub fn to_log_level_filter(self) -> log::LevelFilter {
match self {
XcpLogLevel::Off => log::LevelFilter::Off,
Expand Down Expand Up @@ -174,6 +174,7 @@ impl XcpEvent {
/// Used by A2L writer
pub fn get_dyn_ext_addr(self, offset: i16) -> (u8, u32) {
let a2l_ext = Xcp::XCP_ADDR_EXT_DYN;
#[allow(clippy::cast_sign_loss)]
let a2l_addr: u32 = (self.get_channel() as u32) << 16 | (offset as u16 as u32);
(a2l_ext, a2l_addr)
}
Expand Down Expand Up @@ -368,7 +369,7 @@ pub enum XcpTransportLayer {

impl XcpTransportLayer {
/// Get the protocol name of the transport layer
pub fn protocol_name(&self) -> &'static str {
pub fn protocol_name(self) -> &'static str {
match self {
XcpTransportLayer::Tcp => "TCP",
XcpTransportLayer::Udp => "UDP",
Expand Down Expand Up @@ -398,12 +399,14 @@ impl XcpBuilder {
}

/// Set log level
#[must_use]
pub fn set_log_level(mut self, log_level: XcpLogLevel) -> Self {
self.log_level = log_level;
self
}

/// Set the EPK to enable the XCP tool to check the A2L file fits the code
#[must_use]
pub fn set_epk(mut self, epk: &'static str) -> Self {
self.epk = epk;
self
Expand Down Expand Up @@ -465,7 +468,7 @@ impl XcpBuilder {
unsafe {
// Initialize the XCP Server and ETH transport layer
let a: [u8; 4] = ipv4_addr.octets();
if 0 == xcplib::XcpEthServerInit(&a as *const u8, port, if tl == XcpTransportLayer::Tcp { 1 } else { 0 }) {
if 0 == xcplib::XcpEthServerInit(&a as *const u8, port, (tl == XcpTransportLayer::Tcp) as u8) {
return Err(XcpError::XcpLib("Error: XcpEthServerInit() failed"));
}
}
Expand Down Expand Up @@ -547,13 +550,15 @@ impl Xcp {
}

/// Get the Xcp singleton instance
#[inline(always)]
#[inline]
#[must_use]
pub fn get() -> &'static Xcp {
// XCP_SINGLETON will be initialized by lazy_static
&XCP_SINGLETON
}

/// Get XCP session status flags
#[allow(clippy::unused_self)]
pub fn get_session_status(&self) -> XcpSessionStatus {
// @@@@ Unsafe - C library call
let session_status: u16 = unsafe { xcplib::XcpGetSessionStatus() } & 0xE040;
Expand All @@ -571,6 +576,7 @@ impl Xcp {
}

/// Set the log level for XCP protocol layer
#[allow(clippy::unused_self)]
pub fn set_log_level(&self, level: XcpLogLevel) {
// @@@@ Unsafe - C library call
unsafe {
Expand All @@ -579,6 +585,7 @@ impl Xcp {
}

/// Print a formated text message to the XCP client tool console
#[allow(clippy::unused_self)]
pub fn print(&self, msg: &str) {
let msg = std::ffi::CString::new(msg).unwrap();
// @@@@ Unsafe - C library call
Expand All @@ -592,6 +599,7 @@ impl Xcp {

/// Execute a XCP command
/// In transport layer mode
#[allow(clippy::unused_self)]
pub fn tl_command(&self, buf: &[u8]) {
// @@@@ Unsafe - C library call
unsafe {
Expand All @@ -601,6 +609,7 @@ impl Xcp {

/// Get the next message in the transmit queue, do not advance the read pointer
/// Data is ready to be sent over TCP or UDP socket
#[allow(clippy::unused_self)]
pub fn tl_transmit_queue_peek(&self) -> Option<&'static [u8]> {
// @@@@ Unsafe - C library call
unsafe {
Expand All @@ -615,12 +624,14 @@ impl Xcp {
}

/// Check if the transmit queue has a message ready to be sent
#[allow(clippy::unused_self)]
pub fn tl_transmit_queue_has_msg(&self) -> bool {
// @@@@ Unsafe - C library call
unsafe { xcplib::XcpTlTransmitQueueHasMsg() != 0 }
}

/// Advance the transmit queue read pointer
#[allow(clippy::unused_self)]
pub fn tl_transmit_queue_next(&self) {
// @@@@ Unsafe - C library call
unsafe {
Expand All @@ -629,6 +640,7 @@ impl Xcp {
}

/// Shut down the XCP transport layer
#[allow(clippy::unused_self)]
pub fn tl_shutdown(&self) {
// @@@@ Unsafe - C library call
unsafe {
Expand All @@ -641,6 +653,7 @@ impl Xcp {

/// Check if the XCP server is ok and running
#[cfg(feature = "xcp_server")]
#[allow(clippy::unused_self)]
pub fn check_server(&self) -> bool {
// @@@@ Unsafe - C library call
unsafe {
Expand All @@ -651,6 +664,7 @@ impl Xcp {

/// Stop the XCP server
#[cfg(feature = "xcp_server")]
#[allow(clippy::unused_self)]
pub fn stop_server(&self) {
// @@@@ Unsafe - C library call
unsafe {
Expand Down Expand Up @@ -799,7 +813,7 @@ impl Xcp {
}

/// Get the active calibration page for the ECU access
#[inline(always)]
#[inline]
fn get_ecu_cal_page(&self) -> XcpCalPage {
if self.ecu_cal_page.load(Ordering::Relaxed) == XcpCalPage::Ram as u8 {
XcpCalPage::Ram
Expand Down
Loading

0 comments on commit ca35f33

Please sign in to comment.