Skip to content

Commit

Permalink
Make Checking Model Support Easier (#33)
Browse files Browse the repository at this point in the history
* Add a new `is_supported` function that takes a model string and
returns `true` is the model is supported
* Add a new `model::**::Instrument::model_is` function for each platform
which takes a model string and returns true if the model is that
instrument platform (e.g 2600, 3700, TTI)

**BONUS CHANGE** added tests for Stb values.
  • Loading branch information
esarver authored Dec 12, 2024
1 parent 88d0c7a commit 26f5b62
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 9 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
Security -- in case of vulnerabilities.
-->

## [0.19.0]

### Added

- Added `is_supported` function that takes in a model number
- Added `model_is` method on `model::**::Instrument` structs

## [0.18.4]

### Fixed
Expand Down Expand Up @@ -106,7 +113,8 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
- Using `read_password` instead of `prompt_password` of rpassword crate (TSP-517)

<!--Version Comparison Links-->
[Unreleased]: https://github.com/tektronix/tsp-toolkit-kic-lib/compare/v0.18.4..HEAD
[Unreleased]: https://github.com/tektronix/tsp-toolkit-kic-lib/compare/v0.19.0..HEAD
[0.19.0]: https://github.com/tektronix/tsp-toolkit-kic-lib/releases/tag/v0.19.0
[0.18.4]: https://github.com/tektronix/tsp-toolkit-kic-lib/releases/tag/v0.18.4
[0.18.3]: https://github.com/tektronix/tsp-toolkit-kic-lib/releases/tag/v0.18.3
[0.18.2]: https://github.com/tektronix/tsp-toolkit-kic-lib/releases/tag/v0.18.2
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "tsp-toolkit-kic-lib"
description = "A library specifically enabling communication to the Keithley product-line of instruments"
version = "0.18.4"
version = "0.19.0"
authors = ["Keithley Instruments, LLC"]
edition = "2021"
repository = "https://github.com/tektronix/tsp-toolkit-kic-lib"
Expand Down
11 changes: 9 additions & 2 deletions src/model/ki2600.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ pub struct Instrument {
impl Instrument {
#[must_use]
pub fn is(info: &InstrumentInfo) -> bool {
info.model
info.model.as_ref().is_some_and(Self::model_is)
}

#[must_use]
pub fn model_is(model: impl AsRef<str>) -> bool {
model
.as_ref()
.is_some_and(|model| model.split_ascii_whitespace().last().is_some_and(is_2600))
.split_ascii_whitespace()
.last()
.is_some_and(is_2600)
}

#[must_use]
Expand Down
11 changes: 9 additions & 2 deletions src/model/ki3700.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,16 @@ pub struct Instrument {
impl Instrument {
#[must_use]
pub fn is(info: &InstrumentInfo) -> bool {
info.model
info.model.as_ref().is_some_and(Self::model_is)
}

#[must_use]
pub fn model_is(model: impl AsRef<str>) -> bool {
model
.as_ref()
.is_some_and(|model| model.split_ascii_whitespace().last().is_some_and(is_3700))
.split_ascii_whitespace()
.last()
.is_some_and(is_3700)
}

#[must_use]
Expand Down
8 changes: 8 additions & 0 deletions src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ pub mod ki3700;
pub mod tti;
pub mod versatest;

#[must_use]
pub fn is_supported(model: impl AsRef<str>) -> bool {
self::ki2600::Instrument::model_is(&model)
|| self::ki3700::Instrument::model_is(&model)
|| self::tti::Instrument::model_is(&model)
|| self::versatest::Instrument::model_is(&model)
}

impl TryFrom<Protocol> for Box<dyn Instrument> {
type Error = InstrumentError;

Expand Down
11 changes: 10 additions & 1 deletion src/model/tti.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,16 @@ pub struct Instrument {
impl Instrument {
#[must_use]
pub fn is(info: &InstrumentInfo) -> bool {
info.model.as_ref().is_some_and(is_tti)
info.model.as_ref().is_some_and(Self::model_is)
}

#[must_use]
pub fn model_is(model: impl AsRef<str>) -> bool {
model
.as_ref()
.split_ascii_whitespace()
.last()
.is_some_and(is_tti)
}

#[must_use]
Expand Down
11 changes: 10 additions & 1 deletion src/model/versatest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,16 @@ pub struct Instrument {
impl Instrument {
#[must_use]
pub fn is(info: &InstrumentInfo) -> bool {
info.model.as_ref().is_some_and(is_versatest)
info.model.as_ref().is_some_and(Self::model_is)
}

#[must_use]
pub fn model_is(model: impl AsRef<str>) -> bool {
model
.as_ref()
.split_ascii_whitespace()
.last()
.is_some_and(is_versatest)
}

#[must_use]
Expand Down
62 changes: 62 additions & 0 deletions src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pub enum Protocol {
},
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Stb {
Stb(u16),
NotSupported,
Expand Down Expand Up @@ -306,3 +307,64 @@ impl From<Box<dyn Interface>> for Protocol {
Self::Raw(value)
}
}

#[cfg(test)]
mod unit {
use std::assert_matches::assert_matches;

use super::Stb;

#[test]
fn stb_test_mav() {
let input = 0x0010;

let actual = Stb::Stb(input).mav();

assert_matches!(actual, Ok(true));
}

#[test]
fn stb_test_esr() {
let input = 0x0020;

let actual = Stb::Stb(input).esr();

assert_matches!(actual, Ok(true));
}

#[test]
fn stb_test_srq() {
let input = 0x0040;

let actual = Stb::Stb(input).srq();

assert_matches!(actual, Ok(true));
}

#[test]
fn stb_test_all() {
for i in 0..=u16::MAX {
let stb = Stb::Stb(i);
//MAV
if i & 0x0010 != 0 {
assert_matches!(stb.mav(), Ok(true), "mav should be set - stb: {i:0>4x}");
} else {
assert_matches!(stb.mav(), Ok(false), "mav should be unset - stb: {i:0>4x}");
}

//ESR
if i & 0x0020 != 0 {
assert_matches!(stb.esr(), Ok(true), "esr should be set - stb: {i:0>4x}");
} else {
assert_matches!(stb.esr(), Ok(false), "esr should be unset - stb: {i:0>4x}");
}

//SRQ
if i & 0x0040 != 0 {
assert_matches!(stb.srq(), Ok(true), "srq should be set - stb: {i:0>4x}");
} else {
assert_matches!(stb.srq(), Ok(false), "srq should be unset - stb: {i:0>4x}");
}
}
}
}

0 comments on commit 26f5b62

Please sign in to comment.