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 15, 2024
1 parent a23a780 commit 6a0f963
Show file tree
Hide file tree
Showing 13 changed files with 506 additions and 284 deletions.
11 changes: 6 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
include:
- { rust: stable, vendor: Atmel, options: all }
- { rust: stable, vendor: Atmel, options: "" }
- { rust: stable, vendor: Freescale, options: all }
- { rust: stable, vendor: Freescale, options: "--strict --atomics" }
- { rust: stable, vendor: Freescale, options: "" }
- { rust: stable, vendor: Fujitsu, options: "" }
- { rust: stable, vendor: Fujitsu, options: "--atomics" }
Expand All @@ -80,16 +80,17 @@ 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
- { rust: 1.70.0, vendor: Nordic, options: "" }
- { rust: 1.74.0, vendor: Nordic, options: "" }
# Use nightly for architectures which don't support stable
- { rust: nightly, vendor: MSP430, options: "--atomics" }
- { rust: nightly, vendor: MSP430, options: "" }
- { rust: nightly, vendor: Espressif, options: "--atomics" }
- { rust: nightly, vendor: Espressif, options: "" }
# Workaround for _1token0
- { rust: nightly, vendor: Espressif, options: "--atomics --ident-format register::c:" }
- { rust: nightly, vendor: Espressif, options: "--ident-format register::c:" }

steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/).

## [Unreleased]

- Bump MSRV to 1.74
- 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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ name = "svd2rust"
repository = "https://github.com/rust-embedded/svd2rust/"
version = "0.31.5"
readme = "README.md"
rust-version = "1.70"
rust-version = "1.74"

[package.metadata.deb]
section = "rust"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
![GitHub top language](https://img.shields.io/github/languages/top/rust-embedded/svd2rust)
![Minimum Supported Rust Version](https://img.shields.io/badge/rustc-1.70+-blue.svg)
![Minimum Supported Rust Version](https://img.shields.io/badge/rustc-1.74+-blue.svg)
[![crates.io](https://img.shields.io/crates/v/svd2rust.svg)](https://crates.io/crates/svd2rust)
[![crates.io](https://img.shields.io/crates/d/svd2rust.svg)](https://crates.io/crates/svd2rust)
[![Released API docs](https://docs.rs/svd2rust/badge.svg)](https://docs.rs/svd2rust)
Expand Down
6 changes: 3 additions & 3 deletions ci/svd2rust-regress/src/svd_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::{anyhow, Context, Result};
use svd2rust::{util::ToSanitizedCase, Target};
use svd2rust::{util::Case, Target};

use crate::{command::CommandExt, tests::TestCase, Opts, TestAll};
use std::io::prelude::*;
Expand Down Expand Up @@ -176,7 +176,7 @@ impl TestCase {
Ok(val) => val,
Err(_) => "rusttester".into(),
};
let chip_dir = output_dir.join(self.name().to_sanitized_snake_case().as_ref());
let chip_dir = output_dir.join(Case::Snake.sanitize(&self.name()).as_ref());
tracing::span::Span::current()
.record("chip_dir", tracing::field::display(chip_dir.display()));
if let Err(err) = fs::remove_dir_all(&chip_dir) {
Expand All @@ -195,7 +195,7 @@ impl TestCase {
.env("USER", user)
.arg("init")
.arg("--name")
.arg(self.name().to_sanitized_snake_case().as_ref())
.arg(Case::Snake.sanitize(&self.name()).as_ref())
.arg("--vcs")
.arg("none")
.arg(&chip_dir)
Expand Down
100 changes: 72 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,71 @@ 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_accessor".into(), IdentFormat::default().snake_case());
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(
"enum_value_accessor".into(),
IdentFormat::default().snake_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_singleton".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 6a0f963

Please sign in to comment.