Skip to content

Commit d5c4749

Browse files
authored
Merge pull request #234 from rust-embedded/html-enum-default
html enum isDefault
2 parents 1426fde + abfdb08 commit d5c4749

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

CHANGELOG-rust.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ This changelog tracks the Rust `svdtools` project. See
55

66
## [Unreleased]
77

8+
## [v0.3.17] 2024-07-05
9+
10+
* Support "isDefault" enum value in `svdtools html`
11+
812
## [v0.3.16] 2024-07-03
913

1014
* Add possibility to add field arrays
@@ -171,7 +175,8 @@ Other changes:
171175

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

174-
[Unreleased]: https://github.com/rust-embedded/svdtools/compare/v0.3.16...HEAD
178+
[Unreleased]: https://github.com/rust-embedded/svdtools/compare/v0.3.17...HEAD
179+
[v0.3.17]: https://github.com/rust-embedded/svdtools/compare/v0.3.16...v0.3.17
175180
[v0.3.16]: https://github.com/rust-embedded/svdtools/compare/v0.3.15...v0.3.16
176181
[v0.3.15]: https://github.com/rust-embedded/svdtools/compare/v0.3.14...v0.3.15
177182
[v0.3.14]: https://github.com/rust-embedded/svdtools/compare/v0.3.13...v0.3.14

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "svdtools"
3-
version = "0.3.16"
3+
version = "0.3.17"
44
repository = "https://github.com/rust-embedded/svdtools/"
55
description = "Tool for modifying bugs in CMSIS SVD"
66
authors = [

src/html/html_cli.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::borrow::Cow;
2+
use std::collections::BTreeMap;
23
use std::fs::File;
34
use std::io::{Read, Write};
45
#[cfg(target_os = "linux")]
@@ -23,6 +24,7 @@ use svd_parser::{
2324
expand::{derive_peripheral, Index},
2425
svd::{Access, Cluster, Register, RegisterInfo, WriteConstraint},
2526
};
27+
use svd_rs::{EnumeratedValue, EnumeratedValues};
2628

2729
fn sanitize(input: &str) -> String {
2830
use once_cell::sync::Lazy;
@@ -261,9 +263,19 @@ fn parse_register(
261263
};
262264

263265
for value in &enums.values {
266+
let val = if let Some(value) = value.value {
267+
value.to_string()
268+
} else {
269+
let val = value
270+
.is_default()
271+
.then(|| enums_to_map(&enums))
272+
.and_then(|map| minimal_hole(&map, fwidth))
273+
.ok_or_else(|| anyhow!("Value is missing from {value:?}"))?;
274+
format!("{val} (+)")
275+
};
276+
264277
doc += &format!(
265-
"<strong>{}: {}</strong>: {}<br>",
266-
value.value.unwrap(),
278+
"<strong>{val}: {}</strong>: {}<br>",
267279
value.name,
268280
sanitize(value.description.as_deref().unwrap_or(""))
269281
);
@@ -272,7 +284,7 @@ fn parse_register(
272284
} else if let Some(WriteConstraint::Range(wcrange)) = wc.as_ref() {
273285
let mn = hex(wcrange.min);
274286
let mx = hex(wcrange.max);
275-
fdoc = Some(format!("Allowed values: {mn}-{mx}"));
287+
fdoc = Some(format!("Allowed values: <strong>{mn}-{mx}</strong>"));
276288
}
277289
}
278290
fields.push(object!({
@@ -514,3 +526,17 @@ pub fn svd2html(htmldir: &Path, svdfiles: &[PathBuf]) -> anyhow::Result<()> {
514526
generate_index_page(&devices, &mut file)?;
515527
Ok(())
516528
}
529+
530+
fn enums_to_map(evs: &EnumeratedValues) -> BTreeMap<u64, &EnumeratedValue> {
531+
let mut map = BTreeMap::new();
532+
for ev in &evs.values {
533+
if let Some(v) = ev.value {
534+
map.insert(v, ev);
535+
}
536+
}
537+
map
538+
}
539+
540+
fn minimal_hole(map: &BTreeMap<u64, &EnumeratedValue>, width: u32) -> Option<u64> {
541+
(0..(1u64 << width)).find(|&v| !map.contains_key(&v))
542+
}

0 commit comments

Comments
 (0)