diff --git a/CHANGELOG.md b/CHANGELOG.md index 2da11759..ceff291e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/). ## [Unreleased] +- Add `crate_path` setting - Inline `Settings` into `Config`, add `settings_file` - Fix MSP430 PAC inner attribute generation when used with the `-m` switch. diff --git a/src/config.rs b/src/config.rs index b206d075..5e57ed70 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,9 +1,15 @@ use anyhow::{bail, Result}; +use proc_macro2::Span; +use serde::Deserialize; use std::{ collections::HashMap, ops::{Deref, DerefMut}, path::{Path, PathBuf}, + str::FromStr, }; +use syn::{punctuated::Punctuated, Ident}; + +use crate::util::path_segment; #[cfg_attr(feature = "serde", derive(serde::Deserialize), serde(default))] #[derive(Clone, PartialEq, Eq, Debug, Default)] @@ -323,6 +329,7 @@ pub enum IdentFormatsTheme { pub struct Settings { /// Path to chip HTML generated by svdtools pub html_url: Option, + pub crate_path: Option, /// RISC-V specific settings pub riscv_config: Option, } @@ -332,10 +339,45 @@ impl Settings { if source.html_url.is_some() { self.html_url = source.html_url; } + if source.crate_path.is_some() { + self.crate_path = source.crate_path; + } if source.riscv_config.is_some() { self.riscv_config = source.riscv_config; } } } +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct CratePath(pub syn::Path); + +impl Default for CratePath { + fn default() -> Self { + let mut segments = Punctuated::new(); + segments.push(path_segment(Ident::new("crate", Span::call_site()))); + Self(syn::Path { + leading_colon: None, + segments, + }) + } +} + +#[cfg(feature = "serde")] +impl<'de> Deserialize<'de> for CratePath { + fn deserialize(deserializer: D) -> std::result::Result + where + D: serde::Deserializer<'de>, + { + let s = String::deserialize(deserializer)?; + Ok(Self::from_str(&s).unwrap()) + } +} + +impl FromStr for CratePath { + type Err = syn::Error; + fn from_str(s: &str) -> std::result::Result { + syn::parse_str(&s).map(Self) + } +} + pub mod riscv; diff --git a/src/util.rs b/src/util.rs index 3520bcf3..b1d7ee45 100644 --- a/src/util.rs +++ b/src/util.rs @@ -293,18 +293,18 @@ pub fn block_path_to_ty( config: &Config, span: Span, ) -> TypePath { - let mut segments = Punctuated::new(); - segments.push(path_segment(Ident::new("crate", span))); - segments.push(path_segment(ident( + let mut path = config.settings.crate_path.clone().unwrap_or_default().0; + path.segments.push(path_segment(ident( &bpath.peripheral, config, "peripheral_mod", span, ))); for ps in &bpath.path { - segments.push(path_segment(ident(ps, config, "cluster_mod", span))); + path.segments + .push(path_segment(ident(ps, config, "cluster_mod", span))); } - type_path(segments) + TypePath { qself: None, path } } pub fn register_path_to_ty(