Skip to content

Commit

Permalink
remove the boilerplate needed for tv::Value
Browse files Browse the repository at this point in the history
- this commit adds template args such that the users dont need to
  manually covert their values into `tv::Value`
- applies for measurements, metadata and run start params

Signed-off-by: mimir-d <[email protected]>
  • Loading branch information
mimir-d committed Oct 12, 2024
1 parent b909e7a commit dbe672e
Show file tree
Hide file tree
Showing 14 changed files with 141 additions and 147 deletions.
4 changes: 1 addition & 3 deletions examples/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ async fn main() -> Result<()> {
tv::TestRun::builder("simple measurement", "1.0")
.build()
.scope(dut, |r| async move {
r.add_step("step0")
.scope(run_file_step)
.await?;
r.add_step("step0").scope(run_file_step).await?;

Ok(tv::TestRunOutcome {
status: TestStatus::Complete,
Expand Down
50 changes: 21 additions & 29 deletions examples/measurement_series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ async fn step0_measurements(step: tv::ScopedTestStep) -> Result<TestStatus, tv::
.start()
.await?;

fan_speed.add_measurement(1000.into()).await?;
fan_speed.add_measurement(1200.into()).await?;
fan_speed.add_measurement(1500.into()).await?;
fan_speed.add_measurement(1000).await?;
fan_speed.add_measurement(1200).await?;
fan_speed.add_measurement(1500).await?;

fan_speed.end().await?;
Ok(TestStatus::Complete)
Expand All @@ -35,20 +35,18 @@ async fn step1_measurements(step: tv::ScopedTestStep) -> Result<TestStatus, tv::
.unit("C")
.build(),
)
.scope(|s| {
async move {
let two_seconds_ago =
chrono::Local::now().with_timezone(&chrono_tz::UTC) - Duration::seconds(2);
s.add_measurement_detail(
tv::MeasurementElementDetail::builder(42.into())
.timestamp(two_seconds_ago)
.build(),
)
.await?;

s.add_measurement(43.into()).await?;
Ok(())
}
.scope(|s| async move {
let two_seconds_ago =
chrono::Local::now().with_timezone(&chrono_tz::UTC) - Duration::seconds(2);
s.add_measurement_detail(
tv::MeasurementElementDetail::builder(42)
.timestamp(two_seconds_ago)
.build(),
)
.await?;

s.add_measurement(43).await?;
Ok(())
})
.await?;

Expand All @@ -74,9 +72,9 @@ async fn step2_measurements(step: tv::ScopedTestStep) -> Result<TestStatus, tv::
.start()
.await?;

freq0.add_measurement(1.0.into()).await?;
freq1.add_measurement(2.0.into()).await?;
freq0.add_measurement(1.2.into()).await?;
freq0.add_measurement(1.0).await?;
freq1.add_measurement(2.0).await?;
freq0.add_measurement(1.2).await?;

freq0.end().await?;
freq1.end().await?;
Expand All @@ -95,17 +93,11 @@ async fn main() -> Result<()> {
tv::TestRun::builder("simple measurement", "1.0")
.build()
.scope(dut, |r| async move {
r.add_step("step0")
.scope(step0_measurements)
.await?;
r.add_step("step0").scope(step0_measurements).await?;

r.add_step("step1")
.scope(step1_measurements)
.await?;
r.add_step("step1").scope(step1_measurements).await?;

r.add_step("step2")
.scope(step2_measurements)
.await?;
r.add_step("step2").scope(step2_measurements).await?;

Ok(tv::TestRunOutcome {
status: TestStatus::Complete,
Expand Down
8 changes: 3 additions & 5 deletions examples/measurement_single.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use ocptv::output as tv;
use tv::{TestResult, TestStatus};

async fn run_measure_step(step: tv::ScopedTestStep) -> Result<TestStatus, tv::OcptvError> {
step.add_measurement("temperature", 42.5.into()).await?;
step.add_measurement("temperature", 42.5).await?;
step.add_measurement_detail(
tv::Measurement::builder("fan_speed", 1200.into())
tv::Measurement::builder("fan_speed", 1200)
.unit("rpm")
.build(),
)
Expand All @@ -29,9 +29,7 @@ async fn main() -> Result<()> {
tv::TestRun::builder("simple measurement", "1.0")
.build()
.scope(dut, |r| async move {
r.add_step("step0")
.scope(run_measure_step)
.await?;
r.add_step("step0").scope(run_measure_step).await?;

Ok(tv::TestRunOutcome {
status: TestStatus::Complete,
Expand Down
4 changes: 2 additions & 2 deletions examples/measurement_subcomponent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async fn run_measure_step(
ram0: tv::DutHardwareInfo,
) -> Result<TestStatus, tv::OcptvError> {
step.add_measurement_detail(
tv::Measurement::builder("temp0", 100.5.into())
tv::Measurement::builder("temp0", 100.5)
.unit("F")
.hardware_info(&ram0)
.subcomponent(tv::Subcomponent::builder("chip0").build())
Expand All @@ -40,7 +40,7 @@ async fn run_measure_step(

chip1_temp
.scope(|s| async move {
s.add_measurement(79.into()).await?;
s.add_measurement(79).await?;

Ok(())
})
Expand Down
12 changes: 5 additions & 7 deletions examples/measurement_validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use tv::{TestResult, TestStatus, ValidatorType};

async fn run_measure_step(step: tv::ScopedTestStep) -> Result<TestStatus, tv::OcptvError> {
step.add_measurement_detail(
tv::Measurement::builder("temp", 40.into())
tv::Measurement::builder("temp", 40)
.add_validator(
tv::Validator::builder(ValidatorType::GreaterThan, 30.into())
tv::Validator::builder(ValidatorType::GreaterThan, 30)
.name("gt_30")
.build(),
)
Expand All @@ -25,20 +25,18 @@ async fn run_measure_step(step: tv::ScopedTestStep) -> Result<TestStatus, tv::Oc
step.add_measurement_series_detail(
tv::MeasurementSeriesDetail::builder("fan_speed")
.unit("rpm")
.add_validator(
tv::Validator::builder(ValidatorType::LessThanOrEqual, 3000.into()).build(),
)
.add_validator(tv::Validator::builder(ValidatorType::LessThanOrEqual, 3000).build())
.build(),
)
.scope(|s| async move {
s.add_measurement(1000.into()).await?;
s.add_measurement(1000).await?;

Ok(())
})
.await?;

step.add_measurement_detail(
tv::Measurement::builder("fan_speed", 1200.into())
tv::Measurement::builder("fan_speed", 1200)
.unit("rpm")
.build(),
)
Expand Down
8 changes: 4 additions & 4 deletions src/output/dut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ impl DutInfoBuilder {
self
}

pub fn add_metadata(mut self, key: &str, value: tv::Value) -> Self {
self.metadata.insert(key.to_string(), value);
pub fn add_metadata<V: Into<tv::Value>>(mut self, key: &str, value: V) -> Self {
self.metadata.insert(key.to_string(), value.into());
self
}

Expand Down Expand Up @@ -518,8 +518,8 @@ mod tests {
fn test_dut_builder() -> Result<()> {
let mut dut = DutInfo::builder("1234")
.name("dut")
.add_metadata("key", "value".into())
.add_metadata("key2", "value2".into())
.add_metadata("key", "value")
.add_metadata("key2", "value2")
.add_platform_info(PlatformInfo::builder("platform_info").build())
.build();

Expand Down
12 changes: 6 additions & 6 deletions src/output/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::spec;
/// .is_snapshot(true)
/// .description("description")
/// .content_type(mime::TEXT_PLAIN)
/// .add_metadata("key", "value".into())
/// .add_metadata("key", "value")
/// .build();
/// ```
pub struct File {
Expand Down Expand Up @@ -79,7 +79,7 @@ impl File {
/// let file = File::builder("name", uri)
/// .description("description")
/// .content_type(mime::TEXT_PLAIN)
/// .add_metadata("key", "value".into())
/// .add_metadata("key", "value")
/// .build();
/// ```
pub fn builder(name: &str, uri: tv::Uri) -> FileBuilder {
Expand Down Expand Up @@ -119,7 +119,7 @@ impl File {
/// let builder = File::builder("name", uri)
/// .description("description")
/// .content_type(mime::TEXT_PLAIN)
/// .add_metadata("key", "value".into());
/// .add_metadata("key", "value");
/// let file = builder.build();
/// ```
pub struct FileBuilder {
Expand Down Expand Up @@ -199,10 +199,10 @@ impl FileBuilder {
///
/// let uri = Uri::parse("file:///tmp/foo").unwrap();
/// let builder = File::builder("name", uri)
/// .add_metadata("key", "value".into());
/// .add_metadata("key", "value");
/// ```
pub fn add_metadata(mut self, key: &str, value: tv::Value) -> FileBuilder {
self.metadata.insert(key.to_string(), value);
pub fn add_metadata<V: Into<tv::Value>>(mut self, key: &str, value: V) -> FileBuilder {
self.metadata.insert(key.to_string(), value.into());
self
}

Expand Down
2 changes: 2 additions & 0 deletions src/output/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ impl LogBuilder {
source_location: None,
}
}

pub fn severity(mut self, value: spec::LogSeverity) -> Self {
self.severity = value;
self
}

pub fn source(mut self, file: &str, line: i32) -> Self {
self.source_location = Some(spec::SourceLocation {
file: file.to_string(),
Expand Down
Loading

0 comments on commit dbe672e

Please sign in to comment.