Skip to content

Commit

Permalink
crate_path
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Nov 11, 2024
1 parent dc9db51 commit f1cc671
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
42 changes: 42 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -323,6 +329,7 @@ pub enum IdentFormatsTheme {
pub struct Settings {
/// Path to chip HTML generated by svdtools
pub html_url: Option<url::Url>,
pub crate_path: Option<CratePath>,
/// RISC-V specific settings
pub riscv_config: Option<riscv::RiscvConfig>,
}
Expand All @@ -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<D>(deserializer: D) -> std::result::Result<Self, D::Error>
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<Self, Self::Err> {
syn::parse_str(&s).map(Self)
}
}

pub mod riscv;
10 changes: 5 additions & 5 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit f1cc671

Please sign in to comment.