Skip to content

Commit dc9db51

Browse files
committed
inline Settings into Config
1 parent 74eee32 commit dc9db51

File tree

8 files changed

+56
-40
lines changed

8 files changed

+56
-40
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
77

88
## [Unreleased]
99

10+
- Inline `Settings` into `Config`, add `settings_file`
1011
- Fix MSP430 PAC inner attribute generation when used with the `-m` switch.
1112

1213
## [v0.34.0] - 2024-11-05

src/config.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ pub struct Config {
3535
pub ident_formats_theme: Option<IdentFormatsTheme>,
3636
pub field_names_for_enums: bool,
3737
pub base_address_shift: u64,
38-
pub html_url: Option<url::Url>,
3938
/// Path to YAML file with chip-specific settings
40-
pub settings: Option<PathBuf>,
39+
pub settings_file: Option<PathBuf>,
40+
/// Chip-specific settings
41+
pub settings: Settings,
4142
}
4243

4344
#[allow(clippy::upper_case_acronyms)]
@@ -320,8 +321,21 @@ pub enum IdentFormatsTheme {
320321
#[non_exhaustive]
321322
/// Chip-specific settings
322323
pub struct Settings {
324+
/// Path to chip HTML generated by svdtools
325+
pub html_url: Option<url::Url>,
323326
/// RISC-V specific settings
324327
pub riscv_config: Option<riscv::RiscvConfig>,
325328
}
326329

330+
impl Settings {
331+
pub fn update_from(&mut self, source: Self) {
332+
if source.html_url.is_some() {
333+
self.html_url = source.html_url;
334+
}
335+
if source.riscv_config.is_some() {
336+
self.riscv_config = source.riscv_config;
337+
}
338+
}
339+
}
340+
327341
pub mod riscv;

src/generate/device.rs

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,6 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
2828
}
2929
};
3030

31-
let settings = match config.settings.as_ref() {
32-
#[cfg(feature = "yaml")]
33-
Some(settings) => {
34-
let file = std::fs::read_to_string(settings).context("could not read settings file")?;
35-
Some(serde_yaml::from_str(&file).context("could not parse settings file")?)
36-
}
37-
#[cfg(not(feature = "yaml"))]
38-
Some(_) => {
39-
return Err(anyhow::anyhow!("Support for yaml config files is not available because svd2rust was compiled without the yaml feature"));
40-
}
41-
None => None,
42-
};
43-
4431
// make_mod option explicitly disables inner attributes.
4532
if config.target == Target::Msp430 && !config.make_mod {
4633
out.extend(quote! {
@@ -203,7 +190,7 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
203190

204191
match config.target {
205192
Target::RISCV => {
206-
if settings.is_none() {
193+
if config.settings.riscv_config.is_none() {
207194
warn!("No settings file provided for RISC-V target. Using legacy interrupts rendering");
208195
warn!("Please, consider migrating your PAC to riscv 0.12.0 or later");
209196
out.extend(interrupt::render(
@@ -214,12 +201,7 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
214201
)?);
215202
} else {
216203
debug!("Rendering RISC-V specific code");
217-
out.extend(riscv::render(
218-
&d.peripherals,
219-
device_x,
220-
settings.as_ref().unwrap(),
221-
config,
222-
)?);
204+
out.extend(riscv::render(&d.peripherals, device_x, config)?);
223205
}
224206
}
225207
_ => {
@@ -241,10 +223,7 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
241223
// Core peripherals are handled above
242224
continue;
243225
}
244-
if config.target == Target::RISCV
245-
&& settings.is_some()
246-
&& riscv::is_riscv_peripheral(p, settings.as_ref().unwrap())
247-
{
226+
if config.target == Target::RISCV && riscv::is_riscv_peripheral(p, &config.settings) {
248227
// RISC-V specific peripherals are handled above
249228
continue;
250229
}

src/generate/peripheral.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
8181
}
8282
};
8383

84-
let phtml = config.html_url.as_ref().map(|url| {
84+
let phtml = config.settings.html_url.as_ref().map(|url| {
8585
let doc = format!("See peripheral [structure]({url}#{})", &path.peripheral);
8686
quote!(#[doc = ""] #[doc = #doc])
8787
});

src/generate/register.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ fn api_docs(
210210

211211
doc.push_str("See [API](https://docs.rs/svd2rust/#read--modify--write-api).");
212212

213-
if let Some(url) = config.html_url.as_ref() {
213+
if let Some(url) = config.settings.html_url.as_ref() {
214214
let first_idx = if let Register::Array(_, dim) = &register {
215215
dim.indexes().next()
216216
} else {

src/generate/riscv.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ pub fn is_riscv_peripheral(p: &Peripheral, s: &Settings) -> bool {
2020
pub fn render(
2121
peripherals: &[Peripheral],
2222
device_x: &mut String,
23-
settings: &Settings,
2423
config: &Config,
2524
) -> Result<TokenStream> {
2625
let mut mod_items = TokenStream::new();
@@ -30,7 +29,7 @@ pub fn render(
3029
.as_ref()
3130
.map(|feature| quote!(#[cfg_attr(feature = #feature, derive(defmt::Format))]));
3231

33-
if let Some(c) = settings.riscv_config.as_ref() {
32+
if let Some(c) = config.settings.riscv_config.as_ref() {
3433
if !c.core_interrupts.is_empty() {
3534
debug!("Rendering target-specific core interrupts");
3635
writeln!(device_x, "/* Core interrupt sources and trap handlers */")?;
@@ -216,7 +215,7 @@ pub fn render(
216215
}
217216

218217
let mut riscv_peripherals = TokenStream::new();
219-
if let Some(c) = &settings.riscv_config {
218+
if let Some(c) = config.settings.riscv_config.as_ref() {
220219
let harts = match c.harts.is_empty() {
221220
true => vec![],
222221
false => c

src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,22 @@ pub fn generate(input: &str, config: &Config) -> Result<Generation> {
628628
use std::fmt::Write;
629629

630630
let mut config = config.clone();
631+
632+
match config.settings_file.as_ref() {
633+
#[cfg(feature = "yaml")]
634+
Some(settings) => {
635+
let file = std::fs::read_to_string(settings).context("could not read settings file")?;
636+
config
637+
.settings
638+
.update_from(serde_yaml::from_str(&file).context("could not parse settings file")?)
639+
}
640+
#[cfg(not(feature = "yaml"))]
641+
Some(_) => {
642+
return Err(anyhow::anyhow!("Support for yaml config files is not available because svd2rust was compiled without the yaml feature"));
643+
}
644+
None => {}
645+
};
646+
631647
let mut ident_formats = match config.ident_formats_theme {
632648
Some(IdentFormatsTheme::Legacy) => IdentFormats::legacy_theme(),
633649
_ => IdentFormats::default_theme(),

src/main.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ fn run() -> Result<()> {
101101
.value_name("TOML_FILE"),
102102
)
103103
.arg(
104-
Arg::new("settings")
104+
Arg::new("settings_file")
105105
.long("settings")
106106
.help("Target-specific settings YAML file")
107107
.action(ArgAction::Set)
@@ -276,14 +276,6 @@ Allowed cases are `unchanged` (''), `pascal` ('p'), `constant` ('c') and `snake`
276276
Useful for soft-cores where the peripheral address range isn't necessarily fixed.
277277
Ignore this option if you are not building your own FPGA based soft-cores."),
278278
)
279-
.arg(
280-
Arg::new("html_url")
281-
.long("html-url")
282-
.alias("html_url")
283-
.help("Path to chip HTML generated by svdtools")
284-
.action(ArgAction::Set)
285-
.value_name("URL"),
286-
)
287279
.arg(
288280
Arg::new("log_level")
289281
.long("log")
@@ -330,6 +322,21 @@ Ignore this option if you are not building your own FPGA based soft-cores."),
330322
}
331323
}
332324

325+
match config.settings_file.as_ref() {
326+
#[cfg(feature = "yaml")]
327+
Some(settings) => {
328+
let file = std::fs::read_to_string(settings).context("could not read settings file")?;
329+
config
330+
.settings
331+
.update_from(serde_yaml::from_str(&file).context("could not parse settings file")?)
332+
}
333+
#[cfg(not(feature = "yaml"))]
334+
Some(_) => {
335+
return Err(anyhow::anyhow!("Support for yaml config files is not available because svd2rust was compiled without the yaml feature"));
336+
}
337+
None => {}
338+
};
339+
333340
if let Some(file) = config.input.as_ref() {
334341
config.source_type = SourceType::from_path(file)
335342
}

0 commit comments

Comments
 (0)