Skip to content

Commit

Permalink
clean up clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
esarver committed May 2, 2024
1 parent a1e1ad2 commit a8f27b2
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 74 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,15 @@ tracing = { version = "0.1.40", features = ["async-await"] }
tracing-subscriber = "0.3.18"
tsp-toolkit-kic-lib = { git = "https://github.com/tektronix/tsp-toolkit-kic-lib.git", tag = "v0.15.1" }

[workspace.lints.rust]
warnings = "deny"

[workspace.lints.clippy]
pedantic = { level = "deny", priority = -1 }
nursery = { level = "deny", priority = -1 }
undocumented_unsafe_blocks = "deny"
arithmetic_side_effects = "deny"

[workspace.lints.rustdoc]
all = "warn"
missing_doc_code_examples = "warn"
12 changes: 0 additions & 12 deletions instrument-repl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,3 @@ tsp-toolkit-kic-lib = { workspace = true }
chrono = "0.4.34"
regex = "1.10.3"

[lints.rust]
warnings = "deny"

[lints.clippy]
pedantic = { level = "deny", priority = -1 }
nursery = { level = "deny", priority = -1 }
undocumented_unsafe_blocks = "deny"
arithmetic_side_effects = "deny"

[lints.rustdoc]
all = "warn"
missing_doc_code_examples = "warn"
12 changes: 0 additions & 12 deletions kic-discover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,3 @@ tracing = { workspace = true }
tracing-subscriber = { workspace = true }
tsp-toolkit-kic-lib = { workspace = true }

[lints.rust]
warnings = "deny"

[lints.clippy]
pedantic = { level = "deny", priority = -1 }
nursery = { level = "deny", priority = -1 }
undocumented_unsafe_blocks = "deny"
arithmetic_side_effects = "deny"

[lints.rustdoc]
all = "warn"
missing_doc_code_examples = "warn"
16 changes: 13 additions & 3 deletions kic-discover/src/ethernet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub const SERVICE_NAMES: [&str; 3] = [
//"_scpi-telnet._tcp.local",
];

#[allow(clippy::unsafe_derive_deserialize)]
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Hash, Serialize, Deserialize)]
pub struct LxiDeviceInfo {
io_type: IoType,
Expand Down Expand Up @@ -85,9 +86,18 @@ impl LxiDeviceInfo {
const DEVICE_NS: &str = "http://www.lxistandard.org/InstrumentIdentification/1.0";
if let Ok(root) = xml_data.parse::<Element>() {
if root.is("LXIDevice", DEVICE_NS) {
let manufacturer = root.get_child("Manufacturer", DEVICE_NS).unwrap_or(&minidom::Element::bare("FirmwareRevision", DEVICE_NS)).text();
let model = root.get_child("Model", DEVICE_NS).unwrap_or(&minidom::Element::bare("FirmwareRevision", DEVICE_NS)).text();
let serial_number = root.get_child("SerialNumber", DEVICE_NS).unwrap_or(&minidom::Element::bare("FirmwareRevision", DEVICE_NS)).text();
let manufacturer = root
.get_child("Manufacturer", DEVICE_NS)
.unwrap_or(&minidom::Element::bare("FirmwareRevision", DEVICE_NS))
.text();
let model = root
.get_child("Model", DEVICE_NS)
.unwrap_or(&minidom::Element::bare("FirmwareRevision", DEVICE_NS))
.text();
let serial_number = root
.get_child("SerialNumber", DEVICE_NS)
.unwrap_or(&minidom::Element::bare("FirmwareRevision", DEVICE_NS))
.text();
let firmware_revision = root
.get_child("FirmwareRevision", DEVICE_NS)
.unwrap_or(&minidom::Element::bare("FirmwareRevision", DEVICE_NS))
Expand Down
12 changes: 10 additions & 2 deletions kic-discover/src/instrument_discovery/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pub struct InstrumentDiscovery {

impl InstrumentDiscovery {
#[must_use]
pub fn new(timeout: Duration) -> InstrumentDiscovery {
InstrumentDiscovery {
pub const fn new(timeout: Duration) -> Self {
Self {
timeout: Some(timeout),
}
}
Expand All @@ -38,6 +38,10 @@ impl InstrumentDiscovery {
// Ok(discovery_results)
// }

/// Discover instruments on the network.
///
/// # Errors
/// If [`LxiDeviceInfo::discover`] fails, an error will be returned
pub async fn lan_discover(&self) -> anyhow::Result<HashSet<InstrumentInfo>> {
let mut discovery_results: HashSet<InstrumentInfo> = HashSet::new();

Expand All @@ -55,6 +59,10 @@ impl InstrumentDiscovery {
Ok(discovery_results)
}

/// Discover instruments over USB
///
/// # Errors
/// If [`Usbtmc::usb_discover`] fails, and error will be returned.
pub async fn usb_discover(&self) -> anyhow::Result<HashSet<InstrumentInfo>> {
let mut discovery_results: HashSet<InstrumentInfo> = HashSet::new();

Expand Down
22 changes: 14 additions & 8 deletions kic-discover/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,21 @@ pub fn is_nimitz(in_str: &str) -> bool {
false
}

/// Insert a discovered device into our map of instruments
///
/// # Errors
/// If we fail to lock the `DISC_INSTRUMENTS` variable, a [`std::io::Error`]
/// with [`std::io::ErrorKind::PermissionDenied`] will be returned.
pub fn insert_disc_device(device: &str) -> Result<(), Error> {
let mut db = DISC_INSTRUMENTS.lock().map_err(|_| {
std::io::Error::new(
std::io::ErrorKind::PermissionDenied,
"failed to acquire".to_string(),
)
})?;

db.insert(device.to_string());
DISC_INSTRUMENTS
.lock()
.map_err(|_| {
std::io::Error::new(
std::io::ErrorKind::PermissionDenied,
"failed to acquire".to_string(),
)
})?
.insert(device.to_string());
Ok(())
}

Expand Down
30 changes: 15 additions & 15 deletions kic-discover/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,28 @@ async fn main() -> anyhow::Result<()> {
let lan_instruments = discover_lan(args).await?;
println!("Discovered {} Lan instruments", lan_instruments.len());
for instrument in lan_instruments {
println!("{}", instrument);
println!("{instrument}");
}
}
SubCli::Usb(_) => {
#[allow(clippy::mutable_key_type)]
let usb_instruments = discover_usb().await?;
for instrument in usb_instruments {
println!("{}", instrument);
println!("{instrument}");
}
}
SubCli::All(_args) => {
SubCli::All(args) => {
#[allow(clippy::mutable_key_type)]
let usb_instruments = discover_usb().await?;
for instrument in usb_instruments {
println!("{}", instrument);
println!("{instrument}");
}

#[allow(clippy::mutable_key_type)]
let lan_instruments = discover_lan(_args).await?;
let lan_instruments = discover_lan(args).await?;
println!("Discovered {} Lan instruments", lan_instruments.len());
for instrument in lan_instruments {
println!("{}", instrument);
println!("{instrument}");
}
}
}
Expand All @@ -117,9 +117,9 @@ async fn main() -> anyhow::Result<()> {
Ok(())
}

fn require_exit_timer(sub: &SubCli) -> bool {
if let SubCli::All(_args) = sub {
if _args.exit {
const fn require_exit_timer(sub: &SubCli) -> bool {
if let SubCli::All(args) = sub {
if args.exit {
return true;
}
}
Expand All @@ -130,8 +130,8 @@ async fn init_rpc() -> anyhow::Result<ServerHandle> {
let server = Server::builder().build("127.0.0.1:3030").await?;

let mut module = RpcModule::new(());
module.register_method("get_instr_list", |_, _| {
let mut new_out_str = "".to_owned();
module.register_method("get_instr_list", |_, ()| {
let mut new_out_str = String::new();

if let Ok(db) = DISC_INSTRUMENTS.lock() {
db.iter()
Expand All @@ -152,15 +152,15 @@ async fn init_rpc() -> anyhow::Result<ServerHandle> {
}

async fn discover_lan(args: DiscoverCmd) -> anyhow::Result<HashSet<InstrumentInfo>> {
let mut instr_str = "".to_owned();
let mut instr_str = String::new();
let dur = Duration::from_secs(args.timeout_secs.unwrap_or(20) as u64);
let discover_instance = InstrumentDiscovery::new(dur);
let instruments = discover_instance.lan_discover().await;

match &instruments {
Ok(instrs_set) => {
for instr in instrs_set {
instr_str = format!("{}{}\n", instr_str, instr);
instr_str = format!("{instr_str}{instr}\n");
}
}

Expand All @@ -177,7 +177,7 @@ async fn discover_lan(args: DiscoverCmd) -> anyhow::Result<HashSet<InstrumentInf
}

async fn discover_usb() -> anyhow::Result<HashSet<InstrumentInfo>> {
let mut instr_str = "".to_owned();
let mut instr_str = String::new();

let dur = Duration::from_secs(5); //Not used in USB
let discover_instance = InstrumentDiscovery::new(dur);
Expand All @@ -186,7 +186,7 @@ async fn discover_usb() -> anyhow::Result<HashSet<InstrumentInfo>> {
match &instruments {
Ok(instrs_set) => {
for instr in instrs_set {
instr_str = format!("{}{}\n", instr_str, instr);
instr_str = format!("{instr_str}{instr}\n");
}
}

Expand Down
27 changes: 21 additions & 6 deletions kic-discover/src/usbtmc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ pub struct Usbtmc {
}

impl Usbtmc {
/// Construct a new [`Usbtmc`] device that holds the details of a USB
/// device.
///
/// # Errors
/// This will return a [`TMCResult`] error if the details of the device
/// cannot be fetched from the USB subsystem. This can occur for platform
/// dependent reasons such as the device being assigned an unsupported
/// USB driver in Windows or the device not having proper udev rules created
/// in Linux.
pub fn new(device: rusb::Device<rusb::Context>) -> TMCResult<Self> {
let vendor = device.device_descriptor()?.vendor_id();
let product = device.device_descriptor()?.product_id();
Expand All @@ -27,6 +36,13 @@ impl Usbtmc {
})
}

/// Discover instruments connected via USB.
///
/// # Errors
/// Errors can occur if
/// - [`rusb::Context`] has an error during construction
/// - [`list_instruments`] has an error
#[allow(clippy::unused_async)] // to keep API consistent
pub async fn usb_discover(
_timeout: Option<Duration>,
) -> anyhow::Result<HashSet<InstrumentInfo>> {
Expand Down Expand Up @@ -62,17 +78,16 @@ impl Usbtmc {
let manufacturer = instrument
.read_manufacturer_string()?
.unwrap_or_else(|| String::from("NA"));
let firmware_revision = match instrument.read_device_version()? {
Some(version) => version.to_string(),
None => String::from("NA"),
};
let firmware_revision = instrument
.read_device_version()?
.map_or_else(|| String::from("NA"), |version| version.to_string());
let model = String::from(model_lut(instrument.device_desc.product_id()));
let serial_number = instrument
.read_serial_number()?
.unwrap_or_else(|| String::from("NA"))
.clone();

let tmc_instr: Result<Usbtmc, TMCError> = instrument.try_into();
let tmc_instr: Result<Self, TMCError> = instrument.try_into();

//ToDo: test versatest when it's discoverable
let res = model_check(model.as_str());
Expand Down Expand Up @@ -114,7 +129,7 @@ impl TryFrom<Instrument<rusb::Context>> for Usbtmc {
type Error = TMCError;

fn try_from(value: Instrument<rusb::Context>) -> Result<Self, Self::Error> {
Usbtmc::new(value.device)
Self::new(value.device)
}
}

Expand Down
13 changes: 0 additions & 13 deletions kic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,3 @@ windows-sys = { version = "0.52.0", features = [
] }
regex = "1.10.3"

[lints.rust]
warnings = "deny"

[lints.clippy]
pedantic = { level = "deny", priority = -1 }
nursery = { level = "deny", priority = -1 }
undocumented_unsafe_blocks = "deny"
arithmetic_side_effects = "deny"

[lints.rustdoc]
all = "warn"
missing_doc_code_examples = "warn"

0 comments on commit a8f27b2

Please sign in to comment.