-
-
Notifications
You must be signed in to change notification settings - Fork 19
Add wrapper for serde_yaml #448
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
Closed
Closed
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,65 @@ | ||
| //! Wrapper around serde_yaml which adheres to the YAML specification | ||
| //! | ||
| //! Operators should use this module instead of serde_yaml. | ||
| use serde::ser; | ||
|
|
||
| use crate::error::OperatorResult; | ||
|
|
||
| pub use serde_yaml::from_str; | ||
| pub use serde_yaml::Error; | ||
|
|
||
| /// Serialize the given data structure as a String of YAML. | ||
| /// | ||
| /// Serialization can fail if `T`'s implementation of `Serialize` decides to | ||
| /// return an error. | ||
| pub fn to_string<T>(value: &T) -> OperatorResult<String> | ||
| where | ||
| T: ?Sized + ser::Serialize, | ||
| { | ||
| let yaml = serde_yaml::to_string(value)?; | ||
| Ok(format!("---\n{}", yaml)) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::collections::BTreeMap; | ||
|
|
||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn yaml_with_leading_dashes_can_be_deserialized() { | ||
| let yaml = "\ | ||
| ---\n\ | ||
| key: value"; | ||
|
|
||
| let actual_value: BTreeMap<_, _> = from_str(yaml).expect("deserializable value"); | ||
|
|
||
| let expected_value: BTreeMap<_, _> = [("key", "value")].into(); | ||
|
|
||
| assert_eq!(expected_value, actual_value); | ||
| } | ||
|
|
||
| #[test] | ||
| fn yaml_without_leading_dashes_can_be_deserialized() { | ||
| let yaml = "key: value"; | ||
|
|
||
| let actual_value: BTreeMap<_, _> = from_str(yaml).expect("deserializable value"); | ||
|
|
||
| let expected_value: BTreeMap<_, _> = [("key", "value")].into(); | ||
|
|
||
| assert_eq!(expected_value, actual_value); | ||
| } | ||
|
|
||
| #[test] | ||
| fn value_can_be_serialized() { | ||
| let value: BTreeMap<_, _> = [("key", "value")].into(); | ||
|
|
||
| let actual_yaml = to_string(&value).expect("serializable value"); | ||
|
|
||
| let expected_yaml = "\ | ||
| ---\n\ | ||
| key: value\n"; | ||
|
|
||
| assert_eq!(expected_yaml, actual_yaml); | ||
| } | ||
| } | ||
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.
I think strictly speaking the --- are only mandatory if the file starts with directives, to delimit these from the actual content of the file.
So arguably serde_yaml adheres to the spec, even though I have to say that I do not agree with the upstream change.
Besides that, it might be worthwhile just adding one sentence about what exactly the difference is between this and serde_yaml to make it easier to understand.
Uh oh!
There was an error while loading. Please reload this page.
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.
In YAML 1.0 and 1.1, a YAML stream can start with an "implicit document" (without dashes) or an "explicit document" (with dashes), see https://yaml.org/spec/1.0/index.html#id2561559 and https://yaml.org/spec/1.1/current.html#id898785.
It is the same in YAML 1.2 but "implicit documents" are called "bare documents", see https://yaml.org/spec/1.2.2/#913-bare-documents.
So serde_yaml is compliant with the YAML specification. I will change my pull request.
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.
Is it worth opening an issue against yamllint for this?
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.
There is already a closed issue with this topic: adrienverge/yamllint#302