Skip to content

Commit 7916121

Browse files
committed
crate_path
1 parent dc9db51 commit 7916121

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
77

88
## [Unreleased]
99

10+
- Add `crate_path` setting
1011
- Inline `Settings` into `Config`, add `settings_file`
1112
- Fix MSP430 PAC inner attribute generation when used with the `-m` switch.
1213

src/config.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
use anyhow::{bail, Result};
2+
use proc_macro2::Span;
3+
use serde::Deserialize;
24
use std::{
35
collections::HashMap,
46
ops::{Deref, DerefMut},
57
path::{Path, PathBuf},
8+
str::FromStr,
69
};
10+
use syn::{punctuated::Punctuated, Ident};
11+
12+
use crate::util::path_segment;
713

814
#[cfg_attr(feature = "serde", derive(serde::Deserialize), serde(default))]
915
#[derive(Clone, PartialEq, Eq, Debug, Default)]
@@ -323,6 +329,7 @@ pub enum IdentFormatsTheme {
323329
pub struct Settings {
324330
/// Path to chip HTML generated by svdtools
325331
pub html_url: Option<url::Url>,
332+
pub crate_path: Option<CratePath>,
326333
/// RISC-V specific settings
327334
pub riscv_config: Option<riscv::RiscvConfig>,
328335
}
@@ -332,10 +339,44 @@ impl Settings {
332339
if source.html_url.is_some() {
333340
self.html_url = source.html_url;
334341
}
342+
if source.crate_path.is_some() {
343+
self.crate_path = source.crate_path;
344+
}
335345
if source.riscv_config.is_some() {
336346
self.riscv_config = source.riscv_config;
337347
}
338348
}
339349
}
340350

351+
#[derive(Clone, PartialEq, Eq, Debug)]
352+
pub struct CratePath(pub syn::Path);
353+
354+
impl Default for CratePath {
355+
fn default() -> Self {
356+
let mut segments = Punctuated::new();
357+
segments.push(path_segment(Ident::new("crate", Span::call_site())));
358+
Self(syn::Path {
359+
leading_colon: None,
360+
segments,
361+
})
362+
}
363+
}
364+
365+
impl<'de> Deserialize<'de> for CratePath {
366+
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
367+
where
368+
D: serde::Deserializer<'de>,
369+
{
370+
let s = String::deserialize(deserializer)?;
371+
Ok(Self::from_str(&s).unwrap())
372+
}
373+
}
374+
375+
impl FromStr for CratePath {
376+
type Err = syn::Error;
377+
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
378+
syn::parse_str(&s).map(Self)
379+
}
380+
}
381+
341382
pub mod riscv;

src/util.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,18 +293,18 @@ pub fn block_path_to_ty(
293293
config: &Config,
294294
span: Span,
295295
) -> TypePath {
296-
let mut segments = Punctuated::new();
297-
segments.push(path_segment(Ident::new("crate", span)));
298-
segments.push(path_segment(ident(
296+
let mut path = config.settings.crate_path.clone().unwrap_or_default().0;
297+
path.segments.push(path_segment(ident(
299298
&bpath.peripheral,
300299
config,
301300
"peripheral_mod",
302301
span,
303302
)));
304303
for ps in &bpath.path {
305-
segments.push(path_segment(ident(ps, config, "cluster_mod", span)));
304+
path.segments
305+
.push(path_segment(ident(ps, config, "cluster_mod", span)));
306306
}
307-
type_path(segments)
307+
TypePath { qself: None, path }
308308
}
309309

310310
pub fn register_path_to_ty(

0 commit comments

Comments
 (0)