Skip to content

Commit

Permalink
move spec to a different module path
Browse files Browse the repository at this point in the history
- reasoning: this spec file will be used in multiple modules (serialized
  in ::output but deserialize in possibly ::parse)
- remove the `Spec` suffix from the objects now, since that's clear from
  the module name

Signed-off-by: mimir-d <[email protected]>
  • Loading branch information
mimir-d committed Oct 8, 2024
1 parent b68f3b1 commit 4c539af
Show file tree
Hide file tree
Showing 10 changed files with 256 additions and 260 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
// https://opensource.org/licenses/MIT.

pub mod output;
mod spec;
45 changes: 22 additions & 23 deletions src/output/dut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

use crate::output as tv;
use tv::models;
use crate::spec;

#[derive(Default, Debug, Clone, PartialEq)]
pub struct DutInfo {
Expand All @@ -26,8 +25,8 @@ impl DutInfo {
DutInfoBuilder::new(id).build()
}

pub(crate) fn to_spec(&self) -> models::DutInfoSpec {
models::DutInfoSpec {
pub(crate) fn to_spec(&self) -> spec::DutInfo {
spec::DutInfo {
id: self.id.clone(),
name: self.name.clone(),
platform_infos: self
Expand Down Expand Up @@ -153,8 +152,8 @@ impl HardwareInfo {
HardwareInfoBuilder::new(id, name)
}

pub fn to_spec(&self) -> models::HardwareInfoSpec {
models::HardwareInfoSpec {
pub fn to_spec(&self) -> spec::HardwareInfo {
spec::HardwareInfo {
id: self.id.clone(),
name: self.name.clone(),
version: self.version.clone(),
Expand Down Expand Up @@ -269,7 +268,7 @@ impl HardwareInfoBuilder {

#[derive(Debug, Clone)]
pub struct Subcomponent {
subcomponent_type: Option<models::SubcomponentType>,
subcomponent_type: Option<spec::SubcomponentType>,
name: String,
location: Option<String>,
version: Option<String>,
Expand All @@ -280,8 +279,8 @@ impl Subcomponent {
pub fn builder(name: &str) -> SubcomponentBuilder {
SubcomponentBuilder::new(name)
}
pub fn to_spec(&self) -> models::SubcomponentSpec {
models::SubcomponentSpec {
pub fn to_spec(&self) -> spec::Subcomponent {
spec::Subcomponent {
subcomponent_type: self.subcomponent_type.clone(),
name: self.name.clone(),
location: self.location.clone(),
Expand All @@ -293,7 +292,7 @@ impl Subcomponent {

#[derive(Debug)]
pub struct SubcomponentBuilder {
subcomponent_type: Option<models::SubcomponentType>,
subcomponent_type: Option<spec::SubcomponentType>,
name: String,
location: Option<String>,
version: Option<String>,
Expand All @@ -310,7 +309,7 @@ impl SubcomponentBuilder {
revision: None,
}
}
pub fn subcomponent_type(mut self, value: models::SubcomponentType) -> SubcomponentBuilder {
pub fn subcomponent_type(mut self, value: spec::SubcomponentType) -> SubcomponentBuilder {
self.subcomponent_type = Some(value);
self
}
Expand Down Expand Up @@ -348,8 +347,8 @@ impl PlatformInfo {
PlatformInfoBuilder::new(info)
}

pub fn to_spec(&self) -> models::PlatformInfoSpec {
models::PlatformInfoSpec {
pub fn to_spec(&self) -> spec::PlatformInfo {
spec::PlatformInfo {
info: self.info.clone(),
}
}
Expand Down Expand Up @@ -378,7 +377,7 @@ pub struct SoftwareInfo {
name: String,
version: Option<String>,
revision: Option<String>,
software_type: Option<models::SoftwareType>,
software_type: Option<spec::SoftwareType>,
computer_system: Option<String>,
}

Expand All @@ -387,8 +386,8 @@ impl SoftwareInfo {
SoftwareInfoBuilder::new(id, name)
}

pub fn to_spec(&self) -> models::SoftwareInfoSpec {
models::SoftwareInfoSpec {
pub fn to_spec(&self) -> spec::SoftwareInfo {
spec::SoftwareInfo {
id: self.id.clone(),
name: self.name.clone(),
version: self.version.clone(),
Expand All @@ -405,7 +404,7 @@ pub struct SoftwareInfoBuilder {
name: String,
version: Option<String>,
revision: Option<String>,
software_type: Option<models::SoftwareType>,
software_type: Option<spec::SoftwareType>,
computer_system: Option<String>,
}

Expand All @@ -428,7 +427,7 @@ impl SoftwareInfoBuilder {
self.revision = Some(value.to_string());
self
}
pub fn software_type(mut self, value: models::SoftwareType) -> SoftwareInfoBuilder {
pub fn software_type(mut self, value: spec::SoftwareType) -> SoftwareInfoBuilder {
self.software_type = Some(value);
self
}
Expand All @@ -452,7 +451,7 @@ impl SoftwareInfoBuilder {
#[cfg(test)]
mod tests {
use super::*;
use crate::output::models;
use crate::spec;
use anyhow::{bail, Result};

#[test]
Expand Down Expand Up @@ -569,7 +568,7 @@ mod tests {
let info = SoftwareInfo::builder("software_id", "name")
.version("version")
.revision("revision")
.software_type(models::SoftwareType::Application)
.software_type(spec::SoftwareType::Application)
.computer_system("system")
.build();

Expand All @@ -581,7 +580,7 @@ mod tests {
assert_eq!(spec_swinfo.revision, Some("revision".to_owned()));
assert_eq!(
spec_swinfo.software_type,
Some(models::SoftwareType::Application)
Some(spec::SoftwareType::Application)
);
assert_eq!(spec_swinfo.computer_system, Some("system".to_owned()));

Expand All @@ -599,7 +598,7 @@ mod tests {
#[test]
fn test_subcomponent() -> Result<()> {
let sub = Subcomponent::builder("sub_name")
.subcomponent_type(models::SubcomponentType::Asic)
.subcomponent_type(spec::SubcomponentType::Asic)
.version("version")
.location("location")
.revision("revision")
Expand All @@ -613,7 +612,7 @@ mod tests {
assert_eq!(spec_subcomponent.location, Some("location".to_owned()));
assert_eq!(
spec_subcomponent.subcomponent_type,
Some(models::SubcomponentType::Asic)
Some(spec::SubcomponentType::Asic)
);

Ok(())
Expand Down
20 changes: 10 additions & 10 deletions src/output/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use tokio::fs::File;
use tokio::io::AsyncWriteExt;
use tokio::sync::Mutex;

use crate::output::models;
use crate::spec;

#[derive(Debug, thiserror::Error, derive_more::Display)]
#[non_exhaustive]
Expand Down Expand Up @@ -98,10 +98,10 @@ impl JsonEmitter {
}
}

fn serialize_artifact(&self, object: &models::RootArtifactSpec) -> serde_json::Value {
fn serialize_artifact(&self, object: &spec::RootArtifact) -> serde_json::Value {
let now = chrono::Local::now();
let now_tz = now.with_timezone(&self.timezone);
let out_artifact = models::RootSpec {
let out_artifact = spec::Root {
artifact: object.clone(),
timestamp: now_tz,
seqno: self.next_sequence_no(),
Expand All @@ -114,7 +114,7 @@ impl JsonEmitter {
self.sequence_no.load(atomic::Ordering::SeqCst)
}

pub async fn emit(&self, object: &models::RootArtifactSpec) -> Result<(), WriterError> {
pub async fn emit(&self, object: &spec::RootArtifact) -> Result<(), WriterError> {
let serialized = self.serialize_artifact(object);
match self.writer {
WriterType::File(ref file) => file.write(&serialized.to_string()).await?,
Expand All @@ -139,8 +139,8 @@ mod tests {
async fn test_emit_using_buffer_writer() -> Result<()> {
let expected = json!({
"schemaVersion": {
"major": models::SPEC_VERSION.0,
"minor": models::SPEC_VERSION.1,
"major": spec::SPEC_VERSION.0,
"minor": spec::SPEC_VERSION.1,
},
"sequenceNumber": 1
});
Expand All @@ -164,15 +164,15 @@ mod tests {
async fn test_sequence_number_increments_at_each_call() -> Result<()> {
let expected_1 = json!({
"schemaVersion": {
"major": models::SPEC_VERSION.0,
"minor": models::SPEC_VERSION.1,
"major": spec::SPEC_VERSION.0,
"minor": spec::SPEC_VERSION.1,
},
"sequenceNumber": 1
});
let expected_2 = json!({
"schemaVersion": {
"major": models::SPEC_VERSION.0,
"minor": models::SPEC_VERSION.1,
"major": spec::SPEC_VERSION.0,
"minor": spec::SPEC_VERSION.1,
},
"sequenceNumber": 2
});
Expand Down
24 changes: 13 additions & 11 deletions src/output/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@
// https://opensource.org/licenses/MIT.

use crate::output as tv;
use tv::{dut, models};
use crate::spec;
use tv::dut;

pub struct Error {
symptom: String,
message: Option<String>,
software_infos: Option<Vec<models::SoftwareInfoSpec>>,
source_location: Option<models::SourceLocationSpec>,
software_infos: Option<Vec<spec::SoftwareInfo>>,
source_location: Option<spec::SourceLocation>,
}

impl Error {
pub fn builder(symptom: &str) -> ErrorBuilder {
ErrorBuilder::new(symptom)
}

pub fn to_artifact(&self) -> models::ErrorSpec {
models::ErrorSpec {
pub fn to_artifact(&self) -> spec::Error {
spec::Error {
symptom: self.symptom.clone(),
message: self.message.clone(),
software_infos: self.software_infos.clone(),
Expand All @@ -33,8 +34,8 @@ impl Error {
pub struct ErrorBuilder {
symptom: String,
message: Option<String>,
software_infos: Option<Vec<models::SoftwareInfoSpec>>,
source_location: Option<models::SourceLocationSpec>,
software_infos: Option<Vec<spec::SoftwareInfo>>,
source_location: Option<spec::SourceLocation>,
}

impl ErrorBuilder {
Expand All @@ -51,7 +52,7 @@ impl ErrorBuilder {
self
}
pub fn source(mut self, file: &str, line: i32) -> ErrorBuilder {
self.source_location = Some(models::SourceLocationSpec {
self.source_location = Some(spec::SourceLocation {
file: file.to_string(),
line,
});
Expand Down Expand Up @@ -86,7 +87,8 @@ mod tests {

use super::*;
use crate::output as tv;
use tv::{dut, models};
use crate::spec;
use tv::dut;

#[test]
fn test_error_output_as_test_run_descendant_to_artifact() -> Result<()> {
Expand All @@ -99,7 +101,7 @@ mod tests {
let artifact = error.to_artifact();
assert_eq!(
artifact,
models::ErrorSpec {
spec::Error {
symptom: error.symptom.clone(),
message: error.message.clone(),
software_infos: error.software_infos.clone(),
Expand All @@ -121,7 +123,7 @@ mod tests {
let artifact = error.to_artifact();
assert_eq!(
artifact,
models::ErrorSpec {
spec::Error {
symptom: error.symptom.clone(),
message: error.message.clone(),
software_infos: error.software_infos.clone(),
Expand Down
Loading

0 comments on commit 4c539af

Please sign in to comment.