diff --git a/ci/svd2rust-regress/src/main.rs b/ci/svd2rust-regress/src/main.rs index 0e0eb09a..4d32f1ac 100644 --- a/ci/svd2rust-regress/src/main.rs +++ b/ci/svd2rust-regress/src/main.rs @@ -154,19 +154,16 @@ pub struct Test { impl Test { fn run(&self, opts: &Opts) -> Result<(), anyhow::Error> { match self { - Self { url: Some(url), .. } => {} + Self { url: Some(_), .. } => {} Self { - svd_file: Some(svd_file), - .. - } => {} - Self { - chip: Some(chip), .. + svd_file: Some(_), .. } => {} + Self { chip: Some(_), .. } => {} _ => unreachable!("clap should not allow this"), } let test = if let (Some(url), Some(arch)) = (&self.url, &self.arch) { tests::TestCase { - arch: svd2rust::Target::parse(&arch)?, + arch: svd2rust::Target::parse(arch)?, mfgr: tests::Manufacturer::Unknown, chip: self .chip diff --git a/ci/svd2rust-regress/src/svd_test.rs b/ci/svd2rust-regress/src/svd_test.rs index 5ac8bbd9..13bb6648 100644 --- a/ci/svd2rust-regress/src/svd_test.rs +++ b/ci/svd2rust-regress/src/svd_test.rs @@ -1,7 +1,7 @@ use anyhow::{anyhow, Context, Result}; use svd2rust::{util::Case, Target}; -use crate::{command::CommandExt, tests::TestCase, Opts, TestAll}; +use crate::{command::CommandExt, tests::TestCase, Opts}; use std::io::prelude::*; use std::path::PathBuf; use std::process::Command; diff --git a/src/generate/device.rs b/src/generate/device.rs index fc87fef2..25d6bdd2 100644 --- a/src/generate/device.rs +++ b/src/generate/device.rs @@ -236,8 +236,8 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result { let p_name = util::name_of(p, config.ignore_groups); let p_feature = feature_format.apply(&p_name); - let p_ty = ident(&p_name, &config, "peripheral", span); - let p_singleton = ident(&p_name, &config, "peripheral_singleton", span); + let p_ty = ident(&p_name, config, "peripheral", span); + let p_singleton = ident(&p_name, config, "peripheral_singleton", span); if config.feature_peripheral { feature_attribute.extend(quote! { #[cfg(feature = #p_feature)] }) }; @@ -253,8 +253,8 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result { for p_name in names(p, dim_element) { let p_feature = feature_format.apply(&p_name); - let p_ty = ident(&p_name, &config, "peripheral", span); - let p_singleton = ident(&p_name, &config, "peripheral_singleton", span); + let p_ty = ident(&p_name, config, "peripheral", span); + let p_singleton = ident(&p_name, config, "peripheral_singleton", span); if config.feature_peripheral { feature_attribute.extend(quote! { #[cfg(feature = #p_feature)] }) }; diff --git a/src/generate/interrupt.rs b/src/generate/interrupt.rs index 1c48b06e..10d9bdde 100644 --- a/src/generate/interrupt.rs +++ b/src/generate/interrupt.rs @@ -55,7 +55,7 @@ pub fn render( } pos += 1; - let i_ty = ident(&interrupt.0.name, &config, "interrupt", span); + let i_ty = ident(&interrupt.0.name, config, "interrupt", span); let description = format!( "{} - {}", interrupt.0.value, diff --git a/src/generate/peripheral.rs b/src/generate/peripheral.rs index bc4c3fc3..34f421c6 100644 --- a/src/generate/peripheral.rs +++ b/src/generate/peripheral.rs @@ -37,7 +37,7 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result let name = util::name_of(&p, config.ignore_groups); let span = Span::call_site(); - let p_ty = ident(&name, &config, "peripheral", span); + let p_ty = ident(&name, config, "peripheral", span); let name_str = p_ty.to_string(); let address = util::hex(p.base_address + config.base_address_shift); let description = util::respace(p.description.as_ref().unwrap_or(&p.name)); @@ -85,7 +85,7 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result for pi in svd::peripheral::expand(p, dim) { let name = &pi.name; let description = pi.description.as_deref().unwrap_or(&p.name); - let p_ty = ident(name, &config, "peripheral", span); + let p_ty = ident(name, config, "peripheral", span); let name_str = p_ty.to_string(); let doc_alias = (&name_str != name).then(|| quote!(#[doc(alias = #name)])); let address = util::hex(pi.base_address + config.base_address_shift); @@ -640,8 +640,8 @@ fn register_or_cluster_block( let mut doc_alias = None; let block_ty = if let Some(name) = name { - let ty = ident(name, &config, "cluster", span); - if ty.to_string() != name { + let ty = ident(name, config, "cluster", span); + if ty != name { doc_alias = Some(quote!(#[doc(alias = #name)])); } ty @@ -996,12 +996,12 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result { let doc = make_comment(cluster_size, info.address_offset, &description); - let name: Ident = ident(&info.name, &config, "cluster_accessor", span); + let name: Ident = ident(&info.name, config, "cluster_accessor", span); let syn_field = new_syn_field(name.clone(), ty.clone()); cluster_expanded.push(RegisterBlockField { syn_field, @@ -1050,7 +1050,7 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result Result Result { let doc = make_comment(register_size, info.address_offset, &description); let span = Span::call_site(); - let ty = name_to_ty(ident(&ty_str, &config, "register", span)); - let name: Ident = ident(&ty_name, &config, "register_accessor", span); + let ty = name_to_ty(ident(&ty_str, config, "register", span)); + let name: Ident = ident(&ty_name, config, "register_accessor", span); let syn_field = new_syn_field(name.clone(), ty.clone()); register_expanded.push(RegisterBlockField { syn_field, @@ -1232,18 +1232,18 @@ fn expand_register( let array_convertible = ac && sequential_addresses; let array_proxy_convertible = ac && disjoint_sequential_addresses; let span = Span::call_site(); - let ty = name_to_ty(ident(&ty_str, &config, "register", span)); + let ty = name_to_ty(ident(&ty_str, config, "register", span)); if array_convertible || array_proxy_convertible { let accessor_name = if let Some(dim_name) = array_info.dim_name.as_ref() { ident( &util::fullname(dim_name, &info.alternate_group, config.ignore_groups), - &config, + config, "register_accessor", span, ) } else { - ident(&ty_name, &config, "register_accessor", span) + ident(&ty_name, config, "register_accessor", span) }; let doc = make_comment( register_size * array_info.dim, @@ -1276,7 +1276,7 @@ fn expand_register( for (i, ri) in svd::register::expand(info, array_info).enumerate() { let idx_name = ident( &util::fullname(&ri.name, &info.alternate_group, config.ignore_groups), - &config, + config, "cluster_accessor", span, ); @@ -1321,7 +1321,7 @@ fn expand_register( info.address_offset, ri.description.as_deref().unwrap_or(&ri.name), ); - let name = ident(&ri.name, &config, "register_accessor", span); + let name = ident(&ri.name, config, "register_accessor", span); let syn_field = new_syn_field(name.clone(), ty.clone()); register_expanded.push(RegisterBlockField { @@ -1404,7 +1404,7 @@ fn cluster_block( // name_snake_case needs to take into account array type. let span = Span::call_site(); let mod_ty = ident(&mod_name, config, "cluster_mod", span); - let block_ty = ident(&mod_name, &config, "cluster", span); + let block_ty = ident(&mod_name, config, "cluster", span); if let Some(dpath) = dpath { let dparent = dpath.parent().unwrap(); @@ -1418,7 +1418,7 @@ fn cluster_block( derived .path .segments - .push(path_segment(ident(&dname, &config, "cluster", span))); + .push(path_segment(ident(&dname, config, "cluster", span))); mod_derived .path .segments diff --git a/src/generate/register.rs b/src/generate/register.rs index 9bfddf30..bf078785 100644 --- a/src/generate/register.rs +++ b/src/generate/register.rs @@ -22,7 +22,7 @@ use anyhow::{anyhow, Result}; use syn::punctuated::Punctuated; fn regspec(name: &str, config: &Config, span: Span) -> Ident { - ident(name, &config, "register_spec", span) + ident(name, config, "register_spec", span) } fn field_accessor(name: &str, config: &Config, span: Span) -> Ident { @@ -60,8 +60,8 @@ pub fn render( } } let span = Span::call_site(); - let reg_ty = ident(&name, &config, "register", span); - let doc_alias = (®_ty.to_string() != &name).then(|| quote!(#[doc(alias = #name)])); + let reg_ty = ident(&name, config, "register", span); + let doc_alias = (reg_ty.to_string().as_str() != name).then(|| quote!(#[doc(alias = #name)])); let mod_ty = ident(&name, config, "register_mod", span); let description = util::escape_special_chars( util::respace(®ister.description.clone().unwrap_or_else(|| { @@ -82,7 +82,7 @@ pub fn render( derived .path .segments - .push(path_segment(ident(&dname, &config, "register", span))); + .push(path_segment(ident(&dname, config, "register", span))); mod_derived .path .segments @@ -656,7 +656,7 @@ pub fn fields( { ident( evs.name.as_deref().unwrap_or(&name), - &config, + config, "enum_name", span, ) @@ -666,7 +666,7 @@ pub fn fields( }; // name of read proxy type - let reader_ty = ident(&name, &config, "field_reader", span); + let reader_ty = ident(&name, config, "field_reader", span); // if it's enumeratedValues and it's derived from base, don't derive the read proxy // as the base has already dealt with this; @@ -861,7 +861,7 @@ pub fn fields( evs_r = Some(evs); // generate pub use field_1 reader as field_2 reader let base_field = util::replace_suffix(&base.field.name, ""); - let base_r = ident(&base_field, &config, "field_reader", span); + let base_r = ident(&base_field, config, "field_reader", span); if !reader_derives.contains(&reader_ty) { let base_path = base_syn_path(base, &fpath, &base_r, config)?; mod_items.extend(quote! { @@ -977,14 +977,14 @@ pub fn fields( } else { "enum_name" }; - ident(evs.name.as_deref().unwrap_or(&name), &config, fmt, span) + ident(evs.name.as_deref().unwrap_or(&name), config, fmt, span) } else { // raw_field_value_write_ty fty.clone() }; // name of write proxy type - let writer_ty = ident(&name, &config, "field_writer", span); + let writer_ty = ident(&name, config, "field_writer", span); let mut proxy_items = TokenStream::new(); let mut unsafety = unsafety(f.write_constraint.as_ref(), width); @@ -1136,7 +1136,7 @@ pub fn fields( // generate pub use field_1 writer as field_2 writer let base_field = util::replace_suffix(&base.field.name, ""); - let base_w = ident(&base_field, &config, "field_writer", span); + let base_w = ident(&base_field, config, "field_writer", span); if !writer_derives.contains(&writer_ty) { let base_path = base_syn_path(base, &fpath, &base_w, config)?; mod_items.extend(quote! { @@ -1304,7 +1304,7 @@ impl Variant { .description .clone() .unwrap_or_else(|| format!("`{value:b}`")), - pc: ident(&ev.name, &config, "enum_value", span), + pc: ident(&ev.name, config, "enum_value", span), is_sc, sc, value, diff --git a/src/main.rs b/src/main.rs index b4d02e94..029aab6a 100755 --- a/src/main.rs +++ b/src/main.rs @@ -39,7 +39,7 @@ fn parse_configs(app: Command) -> Result { if let Some(ident_formats) = ident_formats.get_many::("ident_format") { for f in ident_formats { - let mut f = f.split(":"); + let mut f = f.split(':'); if let (Some(n), Some(p), Some(c), Some(s)) = (f.next(), f.next(), f.next(), f.next()) { let case = match c { "" | "unchanged" | "svd" => None, @@ -335,11 +335,11 @@ Ignore this option if you are not building your own FPGA based soft-cores."), let mut features = Vec::new(); if config.feature_group { features.extend( - util::group_names(&device, &feature_format) + util::group_names(&device, feature_format) .iter() .map(|s| format!("{s} = []\n")), ); - let add_groups: Vec<_> = util::group_names(&device, &feature_format) + let add_groups: Vec<_> = util::group_names(&device, feature_format) .iter() .map(|s| format!("\"{s}\"")) .collect(); @@ -347,11 +347,11 @@ Ignore this option if you are not building your own FPGA based soft-cores."), } if config.feature_peripheral { features.extend( - util::peripheral_names(&device, &feature_format) + util::peripheral_names(&device, feature_format) .iter() .map(|s| format!("{s} = []\n")), ); - let add_peripherals: Vec<_> = util::peripheral_names(&device, &feature_format) + let add_peripherals: Vec<_> = util::peripheral_names(&device, feature_format) .iter() .map(|s| format!("\"{s}\"")) .collect(); diff --git a/src/util.rs b/src/util.rs index 2468084b..60eebb2a 100644 --- a/src/util.rs +++ b/src/util.rs @@ -33,26 +33,22 @@ fn to_pascal_case(s: &str) -> String { if let Some(&"") = parts.peek() { string.push('_'); } - loop { - if let Some(p) = parts.next() { - if p.is_empty() { - continue; + while let Some(p) = parts.next() { + if p.is_empty() { + continue; + } + string.push_str(&p.to_pascal_case()); + match parts.peek() { + Some(nxt) + if p.ends_with(|s: char| s.is_numeric()) + && nxt.starts_with(|s: char| s.is_numeric()) => + { + string.push('_'); } - string.push_str(&p.to_pascal_case()); - match parts.peek() { - Some(nxt) - if p.ends_with(|s: char| s.is_numeric()) - && nxt.starts_with(|s: char| s.is_numeric()) => - { - string.push('_'); - } - Some(&"") => { - string.push('_'); - } - _ => {} + Some(&"") => { + string.push('_'); } - } else { - break; + _ => {} } } string @@ -310,7 +306,7 @@ pub fn block_path_to_ty( span, ))); for ps in &bpath.path { - segments.push(path_segment(ident(&ps, config, "cluster_mod", span))); + segments.push(path_segment(ident(ps, config, "cluster_mod", span))); } type_path(segments) }