Skip to content

Commit

Permalink
Merge pull request #781 from rust-embedded/validate
Browse files Browse the repository at this point in the history
post-serde validation
  • Loading branch information
Emilgardis authored Nov 30, 2023
2 parents 99e68c7 + 6f8e955 commit 14dac5a
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 40 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/).

## [Unreleased]

- Add `svd::Device` validation after parsing by `serde`
- Add `skip-crate-attributes` config flag
- Better display parsing errors

## [v0.31.2] - 2023-11-29

- Add `skip-crate-attributes` config flag
- Add iterators for register/cluster/field arrays
- Use parentheses instead of square brackets in docs for field arrays
- Better display parsing errors

## [v0.31.1] - 2023-11-27

Expand Down
120 changes: 93 additions & 27 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ html-escape = "0.2"

[dependencies.svd-parser]
features = ["expand"]
version = "0.14.3"
version = "0.14.4"

[dependencies.svd-rs]
features = ["serde"]
version = "0.14.5"
version = "0.14.6"

[dependencies.syn]
version = "2.0"
Expand Down
28 changes: 19 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,24 +620,34 @@ pub fn load_from(input: &str, config: &Config) -> Result<svd::Device> {
use config::SourceType;
use svd_parser::ValidateLevel;

let validate_level = if config.strict {
ValidateLevel::Strict
} else {
ValidateLevel::Weak
};

let mut device = match config.source_type {
SourceType::Xml => {
let mut parser_config = svd_parser::Config::default();
parser_config.validate_level = if config.strict {
ValidateLevel::Strict
} else {
ValidateLevel::Weak
};
parser_config.validate_level = validate_level;

svd_parser::parse_with_config(input, &parser_config)
.with_context(|| "Error parsing SVD XML file".to_string())?
}
#[cfg(feature = "yaml")]
SourceType::Yaml => serde_yaml::from_str(input)
.with_context(|| "Error parsing SVD YAML file".to_string())?,
SourceType::Yaml => {
let device: svd::Device = serde_yaml::from_str(input)
.with_context(|| "Error parsing SVD YAML file".to_string())?;
device.validate_all(validate_level)?;
device
}
#[cfg(feature = "json")]
SourceType::Json => serde_json::from_str(input)
.with_context(|| "Error parsing SVD JSON file".to_string())?,
SourceType::Json => {
let device: svd::Device = serde_json::from_str(input)
.with_context(|| "Error parsing SVD JSON file".to_string())?;
device.validate_all(validate_level)?;
device
}
};
svd_parser::expand_properties(&mut device);
Ok(device)
Expand Down

0 comments on commit 14dac5a

Please sign in to comment.