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 4, 2024
1 parent 869d34f commit 0f13bc2
Show file tree
Hide file tree
Showing 10 changed files with 393 additions and 207 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ jobs:
include:
- { rust: stable, vendor: Atmel, options: all }
- { rust: stable, vendor: Atmel, options: "" }
- { rust: stable, vendor: Freescale, options: all }
- { rust: stable, vendor: Freescale, options: "" }
- { rust: stable, vendor: Freescale, options: "--strict --atomics --ident-format register::c:" }
- { rust: stable, vendor: Freescale, options: "--ident-format register::c:" }
- { rust: stable, vendor: Fujitsu, options: "" }
- { rust: stable, vendor: Fujitsu, options: "--atomics" }
- { rust: stable, vendor: GD32, options: all }
Expand All @@ -80,7 +80,7 @@ jobs:
- { rust: stable, vendor: Spansion, options: "--atomics" }
- { rust: stable, vendor: STMicro, options: "" }
- { rust: stable, vendor: STMicro, options: "--atomics" }
- { rust: stable, vendor: STM32-patched, options: "--strict --pascal-enum-values --max-cluster-size --atomics --atomics-feature atomics --impl-debug --impl-defmt defmt" }
- { rust: stable, vendor: STM32-patched, options: "--strict --max-cluster-size --atomics --atomics-feature atomics --impl-debug --impl-defmt defmt" }
- { rust: stable, vendor: Toshiba, options: all }
- { rust: stable, vendor: Toshiba, options: "" }
# Test MSRV
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
## [Unreleased]

- Add `base-address-shift` config flag
- Use `PascalCase` for type idents, fix case changing bugs, add `--ident-format` (`-f`) option flag

## [v0.31.5] - 2024-01-04

Expand Down
95 changes: 67 additions & 28 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use anyhow::{bail, Result};
use std::path::{Path, PathBuf};
use std::{
collections::HashMap,
ops::{Deref, DerefMut},
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 +18,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 +139,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 +158,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 @@ -169,32 +174,66 @@ impl IdentFormat {

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize), serde(default))]
pub struct IdentFormats {
pub field_reader: IdentFormat,
pub field_writer: IdentFormat,
pub enum_name: IdentFormat,
pub enum_write_name: IdentFormat,
pub enum_value: IdentFormat,
pub interrupt: IdentFormat,
pub cluster: IdentFormat,
pub register: IdentFormat,
pub register_spec: IdentFormat,
pub peripheral: IdentFormat,
}
pub struct IdentFormats(HashMap<String, 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(),
}
let mut map = HashMap::new();

map.insert(
"field_reader".into(),
IdentFormat::default().pascal_case().suffix("R"),
);
map.insert(
"field_writer".into(),
IdentFormat::default().pascal_case().suffix("W"),
);
map.insert("enum_name".into(), IdentFormat::default().pascal_case());
map.insert(
"enum_write_name".into(),
IdentFormat::default().pascal_case().suffix("WO"),
);
map.insert("enum_value".into(), IdentFormat::default().pascal_case());
map.insert("interrupt".into(), IdentFormat::default());
map.insert("cluster".into(), IdentFormat::default().pascal_case());
map.insert(
"cluster_accessor".into(),
IdentFormat::default().snake_case(),
);
map.insert("cluster_mod".into(), IdentFormat::default().snake_case());
map.insert("register".into(), IdentFormat::default().pascal_case());
map.insert(
"register_spec".into(),
IdentFormat::default().pascal_case().suffix("Spec"),
);
map.insert(
"register_accessor".into(),
IdentFormat::default().snake_case(),
);
map.insert("register_mod".into(), IdentFormat::default().snake_case());
map.insert("peripheral".into(), IdentFormat::default().pascal_case());
map.insert(
"peripheral_sigleton".into(),
IdentFormat::default().snake_case(),
);
map.insert("peripheral_mod".into(), IdentFormat::default().snake_case());
map.insert(
"peripheral_feature".into(),
IdentFormat::default().snake_case(),
);

Self(map)
}
}

impl Deref for IdentFormats {
type Target = HashMap<String, IdentFormat>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for IdentFormats {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
31 changes: 19 additions & 12 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 @@ -192,6 +192,7 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
config,
)?);

let feature_format = config.ident_formats.get("peripheral_feature").unwrap();
for p in &d.peripherals {
if config.target == Target::CortexM
&& core_peripherals.contains(&p.name.to_uppercase().as_ref())
Expand Down Expand Up @@ -226,39 +227,45 @@ 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 = feature_format.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_ty = ident(&p_name, &config.ident_formats.peripheral, span);
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);
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_ty = ident(&p_name, &config.ident_formats.peripheral, span);
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);
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
9 changes: 5 additions & 4 deletions src/generate/interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::svd::Peripheral;
use proc_macro2::{Span, TokenStream};
use quote::quote;

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

Expand Down Expand Up @@ -47,14 +47,15 @@ pub fn render(
let mut pos = 0;
let mut mod_items = TokenStream::new();
let span = Span::call_site();
let feature_format = config.ident_formats.get("peripheral_feature").unwrap();
for interrupt in &interrupts {
while pos < interrupt.0.value {
elements.extend(quote!(Vector { _reserved: 0 },));
pos += 1;
}
pos += 1;

let i_ty = ident(&interrupt.0.name, &config.ident_formats.interrupt, span);
let i_ty = ident(&interrupt.0.name, &config, "interrupt", span);
let description = format!(
"{} - {}",
interrupt.0.value,
Expand All @@ -74,13 +75,13 @@ pub fn render(
let mut feature_attribute = TokenStream::new();
let mut not_feature_attribute = TokenStream::new();
if config.feature_group && interrupt.1.is_some() {
let feature_name = interrupt.1.as_ref().unwrap().to_sanitized_snake_case();
let feature_name = feature_format.apply(interrupt.1.as_ref().unwrap());
feature_attribute_flag = true;
feature_attribute.extend(quote! { #[cfg(feature = #feature_name)] });
not_feature_attribute.extend(quote! { feature = #feature_name, });
}
if config.feature_peripheral {
let feature_name = interrupt.2.to_sanitized_snake_case();
let feature_name = feature_format.apply(&interrupt.2);
feature_attribute_flag = true;
feature_attribute.extend(quote! { #[cfg(feature = #feature_name)] });
not_feature_attribute.extend(quote! { feature = #feature_name, });
Expand Down
Loading

0 comments on commit 0f13bc2

Please sign in to comment.