Skip to content

Commit

Permalink
Merge pull request #234 from rust-embedded/html-enum-default
Browse files Browse the repository at this point in the history
html enum isDefault
  • Loading branch information
burrbull authored Jul 7, 2024
2 parents 1426fde + abfdb08 commit d5c4749
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG-rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ This changelog tracks the Rust `svdtools` project. See

## [Unreleased]

## [v0.3.17] 2024-07-05

* Support "isDefault" enum value in `svdtools html`

## [v0.3.16] 2024-07-03

* Add possibility to add field arrays
Expand Down Expand Up @@ -171,7 +175,8 @@ Other changes:

* Initial release with feature-parity with the Python project.

[Unreleased]: https://github.com/rust-embedded/svdtools/compare/v0.3.16...HEAD
[Unreleased]: https://github.com/rust-embedded/svdtools/compare/v0.3.17...HEAD
[v0.3.17]: https://github.com/rust-embedded/svdtools/compare/v0.3.16...v0.3.17
[v0.3.16]: https://github.com/rust-embedded/svdtools/compare/v0.3.15...v0.3.16
[v0.3.15]: https://github.com/rust-embedded/svdtools/compare/v0.3.14...v0.3.15
[v0.3.14]: https://github.com/rust-embedded/svdtools/compare/v0.3.13...v0.3.14
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "svdtools"
version = "0.3.16"
version = "0.3.17"
repository = "https://github.com/rust-embedded/svdtools/"
description = "Tool for modifying bugs in CMSIS SVD"
authors = [
Expand Down
32 changes: 29 additions & 3 deletions src/html/html_cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::fs::File;
use std::io::{Read, Write};
#[cfg(target_os = "linux")]
Expand All @@ -23,6 +24,7 @@ use svd_parser::{
expand::{derive_peripheral, Index},
svd::{Access, Cluster, Register, RegisterInfo, WriteConstraint},
};
use svd_rs::{EnumeratedValue, EnumeratedValues};

fn sanitize(input: &str) -> String {
use once_cell::sync::Lazy;
Expand Down Expand Up @@ -261,9 +263,19 @@ fn parse_register(
};

for value in &enums.values {
let val = if let Some(value) = value.value {
value.to_string()
} else {
let val = value
.is_default()
.then(|| enums_to_map(&enums))
.and_then(|map| minimal_hole(&map, fwidth))
.ok_or_else(|| anyhow!("Value is missing from {value:?}"))?;
format!("{val} (+)")
};

doc += &format!(
"<strong>{}: {}</strong>: {}<br>",
value.value.unwrap(),
"<strong>{val}: {}</strong>: {}<br>",
value.name,
sanitize(value.description.as_deref().unwrap_or(""))
);
Expand All @@ -272,7 +284,7 @@ fn parse_register(
} else if let Some(WriteConstraint::Range(wcrange)) = wc.as_ref() {
let mn = hex(wcrange.min);
let mx = hex(wcrange.max);
fdoc = Some(format!("Allowed values: {mn}-{mx}"));
fdoc = Some(format!("Allowed values: <strong>{mn}-{mx}</strong>"));
}
}
fields.push(object!({
Expand Down Expand Up @@ -514,3 +526,17 @@ pub fn svd2html(htmldir: &Path, svdfiles: &[PathBuf]) -> anyhow::Result<()> {
generate_index_page(&devices, &mut file)?;
Ok(())
}

fn enums_to_map(evs: &EnumeratedValues) -> BTreeMap<u64, &EnumeratedValue> {
let mut map = BTreeMap::new();
for ev in &evs.values {
if let Some(v) = ev.value {
map.insert(v, ev);
}
}
map
}

fn minimal_hole(map: &BTreeMap<u64, &EnumeratedValue>, width: u32) -> Option<u64> {
(0..(1u64 << width)).find(|&v| !map.contains_key(&v))
}

0 comments on commit d5c4749

Please sign in to comment.