-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add configuration objects and JSON serialization #231
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
words: | ||
- crossterm | ||
- hasher | ||
- schemars |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
use schemars::JsonSchema; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::Algorithm; | ||
|
||
/// Definition for a file resource configuration, including the path, hash, and content. | ||
/// | ||
/// * `path` - The path to the file. | ||
/// * `hash` - The hash of the file to either compare or compute. | ||
/// * `content` - The content to use when asserting or setting the desired state. | ||
/// * `exist` - The well-known flag indicating whether or not the file exists or should exist. | ||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)] | ||
#[schemars(title = "DSC.FileConfiguration", description = "File resource configuration.")] | ||
pub struct File { | ||
pub path: String, | ||
#[serde(rename = "hash", skip_serializing_if = "Option::is_none")] | ||
pub hash: Option<Hash>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub content: Option<String>, | ||
#[serde(rename = "_exist", skip_serializing_if = "Option::is_none")] | ||
pub exist: Option<bool>, | ||
} | ||
|
||
impl File { | ||
/// Serialize the file configuration to a JSON string. | ||
/// | ||
/// # Return value | ||
/// | ||
/// The file configuration instance as a JSON string. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// # use file_lib::configuration::*; | ||
/// # use file_lib::checksum::Algorithm; | ||
/// # let EXPECTED_PATH = "path/to/file"; | ||
/// # let EXPECTED_ALGORITHM = Algorithm::Sha512; | ||
/// # let EXPECTED_CHECKSUM = "checksum-of-file"; | ||
/// # let EXPECTED_CONTENT = "content-of-file"; | ||
/// # let file = File { | ||
/// # path: EXPECTED_PATH.to_string(), | ||
/// # hash: Some(Hash { | ||
/// # algorithm: EXPECTED_ALGORITHM, | ||
/// # checksum: Some(EXPECTED_CHECKSUM.to_string()), | ||
/// # }), | ||
/// # content: Some(EXPECTED_CONTENT.to_string()), | ||
/// # exist: None, | ||
/// # }; | ||
/// let json = file.to_json(); | ||
/// assert!(json.contains(EXPECTED_PATH)); | ||
/// assert!(json.contains(EXPECTED_ALGORITHM.to_string().as_str())); | ||
/// assert!(json.contains(EXPECTED_CHECKSUM)); | ||
/// assert!(json.contains(EXPECTED_CONTENT)); | ||
/// ``` | ||
#[must_use] | ||
pub fn to_json(&self) -> String { | ||
match serde_json::to_string(self) { | ||
Ok(json) => json, | ||
Err(e) => { | ||
eprintln!("Failed to serialize to JSON: {e}"); | ||
String::new() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it make sense to return an empty string if it fails to serialize? Seems better to return a |
||
} | ||
} | ||
} | ||
|
||
/// Deserialize a file configuration from a JSON string. | ||
/// | ||
/// * `json` - The JSON string to deserialize. | ||
/// | ||
/// # Return value | ||
/// On success, the deserialized file configuration; otherwise, `None`. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// # use file_lib::configuration::File; | ||
/// # use file_lib::checksum::Algorithm; | ||
/// # let EXPECTED_PATH = "path/to/file"; | ||
/// # let EXPECTED_ALGORITHM = Algorithm::Sha512; | ||
/// # let EXPECTED_CHECKSUM = "checksum-of-file"; | ||
/// # let EXPECTED_CONTENT = "content-of-file"; | ||
/// # let JSON = format!( | ||
/// # r#"{{"path":"{path}","hash":{{"algorithm":"{algorithm}","checksum":"{checksum}"}},"content":"{content}"}}"#, | ||
/// # path = EXPECTED_PATH, | ||
/// # algorithm = EXPECTED_ALGORITHM, | ||
/// # checksum = EXPECTED_CHECKSUM, | ||
/// # content = EXPECTED_CONTENT); | ||
/// let file = File::from_json(&JSON).unwrap(); | ||
/// assert_eq!(file.path, EXPECTED_PATH); | ||
/// assert_eq!(&file.content.unwrap(), EXPECTED_CONTENT); | ||
/// | ||
/// let hash = file.hash.unwrap(); | ||
/// assert_eq!(hash.algorithm, EXPECTED_ALGORITHM); | ||
/// assert_eq!(hash.checksum.unwrap(), EXPECTED_CHECKSUM); | ||
/// ``` | ||
#[must_use] | ||
pub fn from_json(json: &str) -> Option<File> { | ||
match serde_json::from_str(json) { | ||
Ok(file) => Some(file), | ||
Err(e) => { | ||
eprintln!("Failed to deserialize from JSON: {e}"); | ||
None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, return a |
||
} | ||
} | ||
} | ||
|
||
/// Get the JSON schema for the file resource configuration. | ||
/// | ||
/// * `pretty` - Flag indicating whether or not to pretty print the schema. | ||
/// | ||
/// # Return value | ||
/// | ||
/// The JSON schema as a string. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// # use file_lib::configuration::File; | ||
/// let schema = File::get_schema(false); | ||
/// assert!(schema.unwrap().contains(r#""$schema":"http://json-schema.org/draft-07/schema#""#)); | ||
/// ``` | ||
/// | ||
/// # Errors | ||
/// | ||
/// Serialization fails if the schema cannot be generated. | ||
pub fn get_schema(pretty: bool) -> Result<String, serde_json::Error> { | ||
let schema = schemars::schema_for!(File); | ||
if pretty { | ||
serde_json::to_string_pretty(&schema) | ||
} else { | ||
serde_json::to_string(&schema) | ||
} | ||
} | ||
} | ||
|
||
impl Default for File { | ||
/// Create an empty file configuration. | ||
fn default() -> Self { | ||
Self { | ||
path: String::new(), | ||
hash: None, | ||
content: None, | ||
exist: None, | ||
} | ||
} | ||
} | ||
|
||
/// Definition for a hash using a given algorithm, and an optional checksum. | ||
/// | ||
/// * `algorithm` - The algorithm to use when comparing or computing the checksum. | ||
/// * `checksum` - The checksum to compare against or the computed result. | ||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)] | ||
#[schemars(title = "DSC.HashConfiguration", description = "Hash configuration.")] | ||
pub struct Hash { | ||
pub algorithm: Algorithm, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub checksum: Option<String>, | ||
} | ||
|
||
impl Default for Hash { | ||
/// Create a default hash configuration with no checksum. | ||
fn default() -> Self { | ||
Self { | ||
algorithm: Algorithm::Sha512, | ||
checksum: None, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like
rename
is not needed here