Skip to content

Commit 60d774e

Browse files
authored
Provide library interface for attest-mock (#306)
* Provide library interface for attest-mock Fixes #303
1 parent 2a8b3f0 commit 60d774e

File tree

5 files changed

+53
-20
lines changed

5 files changed

+53
-20
lines changed

Cargo.lock

Lines changed: 14 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

attest-mock/src/corim.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use miette::{IntoDiagnostic, Result, miette};
66
use rats_corim::CorimBuilder;
77

88
#[derive(knuffel::Decode, Debug)]
9-
struct Measurement {
9+
pub struct Measurement {
1010
#[knuffel(child, unwrap(argument))]
1111
pub mkey: String,
1212

@@ -18,7 +18,7 @@ struct Measurement {
1818
}
1919

2020
#[derive(knuffel::Decode, Debug)]
21-
struct Document {
21+
pub struct Document {
2222
#[knuffel(child, unwrap(argument))]
2323
pub vendor: String,
2424

@@ -32,12 +32,17 @@ struct Document {
3232
pub measurements: Vec<Measurement>,
3333
}
3434

35-
/// Parse the KDL in from string `kdl`, convert it to an `rats_corim::Corim`
36-
/// instance. NOTE: The `name` param should be the name of the file that the
35+
/// Parse the KDL in from string `kdl`
36+
///
37+
/// NOTE: The `name` param should be the name of the file that the
3738
/// `kdl` string was read from. This is used in error reporting.
38-
pub fn mock(name: &str, kdl: &str) -> Result<Vec<u8>> {
39-
let doc: Document = knuffel::parse(name, kdl)?;
39+
pub fn parse(name: &str, kdl: &str) -> Result<Document> {
40+
let doc = knuffel::parse(name, kdl)?;
41+
Ok(doc)
42+
}
4043

44+
/// Convert `doc` to an `rats_corim::Corim` instance
45+
pub fn mock(doc: Document) -> Result<Vec<u8>> {
4146
let mut corim_builder = CorimBuilder::new();
4247
corim_builder.vendor(doc.vendor);
4348
corim_builder.tag_id(doc.tag_id);

attest-mock/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
5+
pub mod corim;
6+
pub mod log;

attest-mock/src/log.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,31 @@ use hubpack::SerializedSize;
77
use miette::{IntoDiagnostic, Result, miette};
88

99
#[derive(knuffel::Decode, Debug)]
10-
struct Document {
10+
pub struct Document {
1111
#[knuffel(children)]
1212
pub measurements: Vec<Measurement>,
1313
}
1414

1515
#[derive(knuffel::Decode, Debug)]
16-
struct Measurement {
16+
pub struct Measurement {
1717
#[knuffel(child, unwrap(argument))]
1818
pub algorithm: String,
1919

2020
#[knuffel(child, unwrap(argument))]
2121
pub digest: String,
2222
}
2323

24-
/// Parse the KDL in from string `kdl`, convert it to an `attest_data::Log`
25-
/// instance. NOTE: The `name` param should be the name of the file that the
24+
/// Parse the KDL in from string `kdl`
25+
///
26+
/// NOTE: The `name` param should be the name of the file that the
2627
/// `kdl` string was read from. This is used in error reporting.
27-
pub fn mock(name: &str, kdl: &str) -> Result<Vec<u8>> {
28-
let doc: Document = knuffel::parse(name, kdl)?;
28+
pub fn parse(name: &str, kdl: &str) -> Result<Document> {
29+
let doc = knuffel::parse(name, kdl)?;
30+
Ok(doc)
31+
}
2932

33+
/// Convert `doc` to an `attest_data::Log` instance
34+
pub fn mock(doc: Document) -> Result<Vec<u8>> {
3035
let mut log = Log::default();
3136
for measurement in doc.measurements {
3237
let measurement = if measurement.algorithm == "sha3-256" {

attest-mock/src/main.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,21 @@ mod log;
3737
fn main() -> Result<()> {
3838
let args = Args::parse();
3939

40-
let name = args.kdl.to_string_lossy();
41-
let kdl = fs::read_to_string(&args.kdl)
40+
let cow_name = args.kdl.to_string_lossy();
41+
let name = cow_name.as_ref();
42+
let kdl = fs::read_to_string(name)
4243
.into_diagnostic()
4344
.map_err(|e| miette!("failed to read file {name} to string: {e}"))?;
4445

4546
let out = match args.cmd {
46-
Command::Corim => corim::mock(&name, &kdl)?,
47-
Command::Log => log::mock(&name, &kdl)?,
47+
Command::Corim => {
48+
let doc = corim::parse(name, &kdl)?;
49+
corim::mock(doc)?
50+
}
51+
Command::Log => {
52+
let doc = log::parse(name, &kdl)?;
53+
log::mock(doc)?
54+
}
4855
};
4956

5057
io::stdout()

0 commit comments

Comments
 (0)