Skip to content

Commit c5f9cf5

Browse files
committed
derive fields instead of enums
1 parent e171daf commit c5f9cf5

File tree

6 files changed

+151
-75
lines changed

6 files changed

+151
-75
lines changed

CHANGELOG-rust.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ This changelog tracks the Rust `svdtools` project. See
55

66
## [Unreleased]
77

8+
* Add `--enum_derive` flag
9+
810
## [v0.3.6] 2023-11-01
911

1012
* Fix #184

src/cli.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ use clap::Parser;
33
use std::path::PathBuf;
44

55
use svdtools::{
6-
convert::convert_cli, html::html_cli, html::htmlcompare_cli, interrupts::interrupts_cli,
7-
makedeps::makedeps_cli, mmap::mmap_cli, patch::patch_cli,
6+
convert::convert_cli,
7+
html::html_cli,
8+
html::htmlcompare_cli,
9+
interrupts::interrupts_cli,
10+
makedeps::makedeps_cli,
11+
mmap::mmap_cli,
12+
patch::{patch_cli, EnumAutoDerive},
813
};
914

1015
#[derive(Parser, Debug)]
@@ -27,6 +32,10 @@ enum Command {
2732
/// When a patch error happens print formatted yaml with all rules included
2833
#[clap(long)]
2934
show_patch_on_error: bool,
35+
36+
/// Derive level when several identical enumerationValues added in a field
37+
#[clap(long)]
38+
enum_derive: Option<EnumAutoDerive>,
3039
},
3140
/// Generate Make dependency file listing dependencies for a YAML file.
3241
Makedeps {
@@ -117,9 +126,13 @@ impl Command {
117126
out_path,
118127
format_config,
119128
show_patch_on_error,
129+
enum_derive,
120130
} => {
121131
let mut config = svdtools::patch::Config::default();
122132
config.show_patch_on_error = *show_patch_on_error;
133+
if let Some(enum_derive) = enum_derive.as_ref() {
134+
config.enum_derive = *enum_derive;
135+
}
123136
patch_cli::patch(
124137
yaml_file,
125138
out_path.as_deref(),

src/patch/device.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::{fs::File, io::Read, path::Path};
99
use super::iterators::{MatchIter, Matched};
1010
use super::peripheral::{PeripheralExt, RegisterBlockExt};
1111
use super::yaml_ext::{AsType, GetVal};
12-
use super::{abspath, matchname, PatchResult, VAL_LVL};
12+
use super::{abspath, matchname, Config, PatchResult, VAL_LVL};
1313
use super::{make_address_block, make_address_blocks, make_cpu, make_interrupt, make_peripheral};
1414
use super::{make_dim_element, modify_dim_element, modify_register_properties};
1515

@@ -21,7 +21,7 @@ pub trait DeviceExt {
2121
fn iter_peripherals<'a, 'b>(&'a mut self, spec: &'b str) -> PerMatchIterMut<'a, 'b>;
2222

2323
/// Work through a device, handling all peripherals
24-
fn process(&mut self, device: &Hash, update_fields: bool) -> PatchResult;
24+
fn process(&mut self, device: &Hash, config: &Config) -> PatchResult;
2525

2626
/// Delete registers matched by rspec inside ptag
2727
fn delete_peripheral(&mut self, pspec: &str) -> PatchResult;
@@ -54,7 +54,7 @@ pub trait DeviceExt {
5454
&mut self,
5555
pspec: &str,
5656
peripheral: &Hash,
57-
update_fields: bool,
57+
config: &Config,
5858
) -> PatchResult;
5959
}
6060

@@ -63,7 +63,7 @@ impl DeviceExt for Device {
6363
self.peripherals.iter_mut().matched(spec)
6464
}
6565

66-
fn process(&mut self, device: &Hash, update_fields: bool) -> PatchResult {
66+
fn process(&mut self, device: &Hash, config: &Config) -> PatchResult {
6767
// Handle any deletions
6868
for pspec in device.str_vec_iter("_delete")? {
6969
self.delete_peripheral(pspec)
@@ -154,7 +154,7 @@ impl DeviceExt for Device {
154154
let periphspec = periphspec.str()?;
155155
if !periphspec.starts_with('_') {
156156
//val["_path"] = device["_path"]; // TODO: check
157-
self.process_peripheral(periphspec, val.hash()?, update_fields)
157+
self.process_peripheral(periphspec, val.hash()?, config)
158158
.with_context(|| format!("According to `{periphspec}`"))?;
159159
}
160160
}
@@ -414,13 +414,13 @@ impl DeviceExt for Device {
414414
&mut self,
415415
pspec: &str,
416416
peripheral: &Hash,
417-
update_fields: bool,
417+
config: &Config,
418418
) -> PatchResult {
419419
// Find all peripherals that match the spec
420420
let mut pcount = 0;
421421
for ptag in self.iter_peripherals(pspec) {
422422
pcount += 1;
423-
ptag.process(peripheral, update_fields)
423+
ptag.process(peripheral, config)
424424
.with_context(|| format!("Processing peripheral `{}`", ptag.name))?;
425425
}
426426
if pcount == 0 {
@@ -444,7 +444,7 @@ mod tests {
444444
fn add_peripherals() {
445445
let (mut device, yaml) = test_utils::get_patcher(Path::new("add")).unwrap();
446446
assert_eq!(device.peripherals.len(), 1);
447-
device.process(&yaml, true).unwrap();
447+
device.process(&yaml, &Default::default()).unwrap();
448448
assert_eq!(device.peripherals.len(), 2);
449449
let periph1 = &device.peripherals[0];
450450
assert_eq!(periph1.name, "DAC1");
@@ -456,7 +456,7 @@ mod tests {
456456
fn delete_peripherals() {
457457
let (mut device, yaml) = test_utils::get_patcher(Path::new("delete")).unwrap();
458458
assert_eq!(device.peripherals.len(), 3);
459-
device.process(&yaml, true).unwrap();
459+
device.process(&yaml, &Default::default()).unwrap();
460460
assert_eq!(device.peripherals.len(), 1);
461461
let remaining_periph = &device.peripherals[0];
462462
assert_eq!(remaining_periph.name, "DAC2");
@@ -470,7 +470,7 @@ mod tests {
470470
let dac2 = device.get_peripheral("DAC2").unwrap();
471471
assert_ne!(dac1.registers, dac2.registers);
472472

473-
device.process(&yaml, true).unwrap();
473+
device.process(&yaml, &Default::default()).unwrap();
474474
assert_eq!(device.peripherals.len(), 3);
475475

476476
let dac1 = device.get_peripheral("DAC1").unwrap();
@@ -496,7 +496,7 @@ mod tests {
496496
assert_eq!(dac1.name, "DAC1");
497497
assert_eq!(dac1.description, None);
498498

499-
device.process(&yaml, true).unwrap();
499+
device.process(&yaml, &Default::default()).unwrap();
500500

501501
// check device final config
502502
assert_eq!(&device.version, "1.7");

src/patch/mod.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,35 @@ use crate::get_encoder_config;
3131
const VAL_LVL: ValidateLevel = ValidateLevel::Weak;
3232

3333
#[non_exhaustive]
34-
#[derive(Clone, Debug, Default)]
34+
#[derive(Clone, Debug)]
3535
pub struct Config {
3636
pub show_patch_on_error: bool,
37+
pub enum_derive: EnumAutoDerive,
38+
pub update_fields: bool,
39+
}
40+
41+
/// Derive level when several identical enumerationValues added in a field
42+
#[derive(clap::ValueEnum)]
43+
#[value(rename_all = "lower")]
44+
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
45+
pub enum EnumAutoDerive {
46+
#[default]
47+
/// Derive enumeratedValues
48+
Enum,
49+
/// Derive fields
50+
Field,
51+
/// Make a copy
52+
None,
53+
}
54+
55+
impl Default for Config {
56+
fn default() -> Self {
57+
Self {
58+
show_patch_on_error: false,
59+
enum_derive: Default::default(),
60+
update_fields: true,
61+
}
62+
}
3763
}
3864

3965
pub fn process_file(
@@ -76,7 +102,7 @@ pub fn process_file(
76102
yaml_includes(root)?;
77103

78104
// Process device
79-
svd.process(root, true).with_context(|| {
105+
svd.process(root, config).with_context(|| {
80106
let name = &svd.name;
81107
let mut out_str = String::new();
82108
let mut emitter = yaml_rust::YamlEmitter::new(&mut out_str);

0 commit comments

Comments
 (0)