Skip to content

Commit

Permalink
change case defaults, fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Feb 3, 2024
1 parent 869d34f commit e6ec471
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 101 deletions.
41 changes: 28 additions & 13 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::path::{Path, PathBuf};

#[cfg_attr(feature = "serde", derive(serde::Deserialize), serde(default))]
#[derive(Clone, PartialEq, Eq, Debug, Default)]
#[non_exhaustive]
pub struct Config {
pub target: Target,
pub atomics: bool,
Expand All @@ -13,7 +14,6 @@ pub struct Config {
pub ignore_groups: bool,
pub keep_list: bool,
pub strict: bool,
pub pascal_enum_values: bool,
pub feature_group: bool,
pub feature_peripheral: bool,
pub max_cluster_size: bool,
Expand Down Expand Up @@ -135,6 +135,7 @@ pub enum Case {
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize), serde(default))]
pub struct IdentFormat {
// Ident case. `None` means don't change
pub case: Option<Case>,
pub prefix: String,
pub suffix: String,
Expand All @@ -153,8 +154,8 @@ impl IdentFormat {
self.case = Some(Case::Pascal);
self
}
pub fn scake_case(mut self) -> Self {
self.case = Some(Case::Pascal);
pub fn snake_case(mut self) -> Self {
self.case = Some(Case::Snake);
self
}
pub fn prefix(mut self, prefix: &str) -> Self {
Expand All @@ -177,24 +178,38 @@ pub struct IdentFormats {
pub enum_value: IdentFormat,
pub interrupt: IdentFormat,
pub cluster: IdentFormat,
pub cluster_accessor: IdentFormat,
//pub cluster_mod: IdentFormat,
pub register: IdentFormat,
pub register_spec: IdentFormat,
pub register_accessor: IdentFormat,
//pub register_mod: IdentFormat,
pub peripheral: IdentFormat,
pub peripheral_sigleton: IdentFormat,
//pub peripheral_mod: IdentFormat,
pub peripheral_feature: IdentFormat,
}

impl Default for IdentFormats {
fn default() -> Self {
Self {
field_reader: IdentFormat::default().constant_case().suffix("_R"),
field_writer: IdentFormat::default().constant_case().suffix("_W"),
enum_name: IdentFormat::default().constant_case().suffix("_A"),
enum_write_name: IdentFormat::default().constant_case().suffix("_AW"),
enum_value: IdentFormat::default().constant_case(),
interrupt: IdentFormat::default().constant_case(),
cluster: IdentFormat::default().constant_case(),
register: IdentFormat::default().constant_case(),
register_spec: IdentFormat::default().constant_case().suffix("_SPEC"),
peripheral: IdentFormat::default().constant_case(),
field_reader: IdentFormat::default().pascal_case().suffix("R"),
field_writer: IdentFormat::default().pascal_case().suffix("W"),
enum_name: IdentFormat::default().pascal_case().suffix("A"),
enum_write_name: IdentFormat::default().pascal_case().suffix("AW"),
enum_value: IdentFormat::default().pascal_case(),
interrupt: IdentFormat::default(),
cluster: IdentFormat::default().pascal_case(),
cluster_accessor: IdentFormat::default().snake_case(),
//cluster_mod: IdentFormat::default().snake_case(),
register: IdentFormat::default().pascal_case(),
register_spec: IdentFormat::default().pascal_case().suffix("Spec"),
register_accessor: IdentFormat::default().snake_case(),
//register_mod: IdentFormat::default().snake_case(),
peripheral: IdentFormat::default().pascal_case(),
peripheral_sigleton: IdentFormat::default().snake_case(),
//peripheral_mod: IdentFormat::default().snake_case(),
peripheral_feature: IdentFormat::default().snake_case(),
}
}
}
30 changes: 20 additions & 10 deletions src/generate/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::io::Write;
use std::path::Path;

use crate::config::{Config, Target};
use crate::util::{self, ident, ToSanitizedCase};
use crate::util::{self, ident};
use anyhow::{Context, Result};

use crate::generate::{interrupt, peripheral};
Expand Down Expand Up @@ -226,39 +226,49 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
}
let mut feature_attribute = TokenStream::new();
if config.feature_group && p.group_name.is_some() {
let feature_name = p.group_name.as_ref().unwrap().to_sanitized_snake_case();
let feature_name = config
.ident_formats
.peripheral_feature
.apply(p.group_name.as_deref().unwrap());
feature_attribute.extend(quote! { #[cfg(feature = #feature_name)] })
};

let span = Span::call_site();
match p {
Peripheral::Single(_p) => {
let p_name = util::name_of(p, config.ignore_groups);
let p_snake = p_name.to_sanitized_snake_case();
let p_feature = config.ident_formats.peripheral_feature.apply(&p_name);
let p_ty = ident(&p_name, &config.ident_formats.peripheral, span);
let p_singleton = ident(&p_name, &config.ident_formats.peripheral_sigleton, span);
if config.feature_peripheral {
feature_attribute.extend(quote! { #[cfg(feature = #p_snake)] })
feature_attribute.extend(quote! { #[cfg(feature = #p_feature)] })
};
fields.extend(quote! {
#[doc = #p_name]
#feature_attribute
pub #p_ty: #p_ty,
pub #p_singleton: #p_ty,
});
exprs.extend(quote!(#feature_attribute #p_ty: #p_ty { _marker: PhantomData },));
exprs.extend(
quote!(#feature_attribute #p_singleton: #p_ty { _marker: PhantomData },),
);
}
Peripheral::Array(p, dim_element) => {
for p_name in names(p, dim_element) {
let p_snake = p_name.to_sanitized_snake_case();
let p_feature = config.ident_formats.peripheral_feature.apply(&p_name);
let p_ty = ident(&p_name, &config.ident_formats.peripheral, span);
let p_singleton =
ident(&p_name, &config.ident_formats.peripheral_sigleton, span);
if config.feature_peripheral {
feature_attribute.extend(quote! { #[cfg(feature = #p_snake)] })
feature_attribute.extend(quote! { #[cfg(feature = #p_feature)] })
};
fields.extend(quote! {
#[doc = #p_name]
#feature_attribute
pub #p_ty: #p_ty,
pub #p_singleton: #p_ty,
});
exprs.extend(quote!(#feature_attribute #p_ty: #p_ty { _marker: PhantomData },));
exprs.extend(
quote!(#feature_attribute #p_singleton: #p_ty { _marker: PhantomData },),
);
}
}
}
Expand Down
74 changes: 41 additions & 33 deletions src/generate/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,18 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result

match &p {
Peripheral::Array(p, dim) => {
let mut snake_names = Vec::with_capacity(dim.dim as _);
let mut feature_names = Vec::with_capacity(dim.dim as _);
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.ident_formats.peripheral, span);
let name_str = p_ty.to_string();
let address = util::hex(pi.base_address + config.base_address_shift);
let p_snake = name.to_sanitized_snake_case();
snake_names.push(p_snake.to_string());
let feature_name = config.ident_formats.peripheral_feature.apply(name);
feature_names.push(feature_name.to_string());
let mut feature_attribute_n = feature_attribute.clone();
if config.feature_peripheral {
feature_attribute_n.extend(quote! { #[cfg(feature = #p_snake)] })
feature_attribute_n.extend(quote! { #[cfg(feature = #feature_name)] })
};
// Insert the peripherals structure
out.extend(quote! {
Expand Down Expand Up @@ -132,7 +132,7 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
});
}

let feature_any_attribute = quote! {#[cfg(any(#(feature = #snake_names),*))]};
let feature_any_attribute = quote! {#[cfg(any(#(feature = #feature_names),*))]};

// Derived peripherals may not require re-implementation, and will instead
// use a single definition of the non-derived version.
Expand All @@ -147,9 +147,9 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
}
}
Peripheral::Single(_) => {
let p_snake = name.to_sanitized_snake_case();
let feature_name = config.ident_formats.peripheral_feature.apply(&name);
if config.feature_peripheral {
feature_attribute.extend(quote! { #[cfg(feature = #p_snake)] })
feature_attribute.extend(quote! { #[cfg(feature = #feature_name)] })
};
// Insert the peripheral structure
out.extend(quote! {
Expand Down Expand Up @@ -981,12 +981,13 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
} else {
util::replace_suffix(&cluster.name, "")
};
let ty = name_to_ty(&ty_name);
let span = Span::call_site();
let ty = name_to_ty(ident(&ty_name, &config.ident_formats.cluster, span));

match cluster {
Cluster::Single(info) => {
let doc = make_comment(cluster_size, info.address_offset, &description);
let name: Ident = info.name.to_snake_case_ident(Span::call_site());
let name: Ident = ident(&info.name, &config.ident_formats.cluster_accessor, span);
let syn_field = new_syn_field(name.clone(), ty.clone());
cluster_expanded.push(RegisterBlockField {
syn_field,
Expand Down Expand Up @@ -1029,12 +1030,15 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
let array_convertible = sequential_addresses && convert_list;

if convert_list {
let span = Span::call_site();
let nb_name_sc = if let Some(dim_name) = array_info.dim_name.as_ref() {
dim_name.to_snake_case_ident(span)
} else {
ty_name.to_snake_case_ident(span)
};
let accessor_name = ident(
if let Some(dim_name) = array_info.dim_name.as_ref() {
dim_name
} else {
&ty_name
},
&config.ident_formats.cluster_accessor,
span,
);
let doc = make_comment(
cluster_size * array_info.dim,
info.address_offset,
Expand All @@ -1044,7 +1048,7 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
accessors.push(if array_convertible {
ArrayAccessor {
doc,
name: nb_name_sc.clone(),
name: accessor_name.clone(),
ty: ty.clone(),
offset: unsuffixed(info.address_offset),
dim: unsuffixed(array_info.dim),
Expand All @@ -1054,7 +1058,7 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
} else {
RawArrayAccessor {
doc,
name: nb_name_sc.clone(),
name: accessor_name.clone(),
ty: ty.clone(),
offset: unsuffixed(info.address_offset),
dim: unsuffixed(array_info.dim),
Expand All @@ -1076,7 +1080,7 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
doc,
name: idx_name,
ty: ty.clone(),
basename: nb_name_sc.clone(),
basename: accessor_name.clone(),
i,
}
.into(),
Expand All @@ -1088,7 +1092,7 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
} else {
zst_type()
};
let syn_field = new_syn_field(nb_name_sc, array_ty);
let syn_field = new_syn_field(accessor_name, array_ty);
cluster_expanded.push(RegisterBlockField {
syn_field,
offset: info.address_offset,
Expand All @@ -1106,7 +1110,7 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
ci.address_offset,
ci.description.as_deref().unwrap_or(&ci.name),
);
let name = ci.name.to_snake_case_ident(Span::call_site());
let name = ident(&ci.name, &config.ident_formats.cluster_accessor, span);
let syn_field = new_syn_field(name.clone(), ty.clone());

cluster_expanded.push(RegisterBlockField {
Expand Down Expand Up @@ -1155,8 +1159,9 @@ fn expand_register(
match register {
Register::Single(info) => {
let doc = make_comment(register_size, info.address_offset, &description);
let ty = name_to_ty(&ty_name);
let name = ty_name.to_snake_case_ident(Span::call_site());
let span = Span::call_site();
let ty = name_to_ty(ident(&ty_name, &config.ident_formats.register, span));
let name: Ident = ident(&ty_name, &config.ident_formats.register_accessor, span);
let syn_field = new_syn_field(name.clone(), ty.clone());
register_expanded.push(RegisterBlockField {
syn_field,
Expand Down Expand Up @@ -1210,15 +1215,18 @@ fn expand_register(
};
let array_convertible = ac && sequential_addresses;
let array_proxy_convertible = ac && disjoint_sequential_addresses;
let ty = name_to_ty(&ty_name);
let span = Span::call_site();
let ty = name_to_ty(ident(&ty_name, &config.ident_formats.register, span));

if array_convertible || array_proxy_convertible {
let span = Span::call_site();
let nb_name_sc = if let Some(dim_name) = array_info.dim_name.as_ref() {
util::fullname(dim_name, &info.alternate_group, config.ignore_groups)
.to_snake_case_ident(span)
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.ident_formats.register,
span,
)
} else {
ty_name.to_snake_case_ident(span)
ident(&ty_name, &config.ident_formats.register, span)
};
let doc = make_comment(
register_size * array_info.dim,
Expand All @@ -1229,7 +1237,7 @@ fn expand_register(
accessors.push(if array_convertible {
ArrayAccessor {
doc,
name: nb_name_sc.clone(),
name: accessor_name.clone(),
ty: ty.clone(),
offset: unsuffixed(info.address_offset),
dim: unsuffixed(array_info.dim),
Expand All @@ -1239,7 +1247,7 @@ fn expand_register(
} else {
RawArrayAccessor {
doc,
name: nb_name_sc.clone(),
name: accessor_name.clone(),
ty: ty.clone(),
offset: unsuffixed(info.address_offset),
dim: unsuffixed(array_info.dim),
Expand All @@ -1263,7 +1271,7 @@ fn expand_register(
doc,
name: idx_name,
ty: ty.clone(),
basename: nb_name_sc.clone(),
basename: accessor_name.clone(),
i,
}
.into(),
Expand All @@ -1275,7 +1283,7 @@ fn expand_register(
} else {
zst_type()
};
let syn_field = new_syn_field(nb_name_sc, array_ty);
let syn_field = new_syn_field(accessor_name, array_ty);
register_expanded.push(RegisterBlockField {
syn_field,
offset: info.address_offset,
Expand All @@ -1293,7 +1301,7 @@ fn expand_register(
info.address_offset,
ri.description.as_deref().unwrap_or(&ri.name),
);
let name = ri.name.to_snake_case_ident(Span::call_site());
let name = ident(&ri.name, &config.ident_formats.register, span);
let syn_field = new_syn_field(name.clone(), ty.clone());

register_expanded.push(RegisterBlockField {
Expand Down
Loading

0 comments on commit e6ec471

Please sign in to comment.