Skip to content

Commit 8776231

Browse files
committed
crate_path
1 parent dc9db51 commit 8776231

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

CHANGELOG.md

+1
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

+41
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
use anyhow::{bail, Result};
2+
use proc_macro2::Span;
23
use std::{
34
collections::HashMap,
45
ops::{Deref, DerefMut},
56
path::{Path, PathBuf},
7+
str::FromStr,
68
};
9+
use syn::{punctuated::Punctuated, Ident};
10+
11+
use crate::util::path_segment;
712

813
#[cfg_attr(feature = "serde", derive(serde::Deserialize), serde(default))]
914
#[derive(Clone, PartialEq, Eq, Debug, Default)]
@@ -323,6 +328,7 @@ pub enum IdentFormatsTheme {
323328
pub struct Settings {
324329
/// Path to chip HTML generated by svdtools
325330
pub html_url: Option<url::Url>,
331+
pub crate_path: Option<CratePath>,
326332
/// RISC-V specific settings
327333
pub riscv_config: Option<riscv::RiscvConfig>,
328334
}
@@ -332,10 +338,45 @@ impl Settings {
332338
if source.html_url.is_some() {
333339
self.html_url = source.html_url;
334340
}
341+
if source.crate_path.is_some() {
342+
self.crate_path = source.crate_path;
343+
}
335344
if source.riscv_config.is_some() {
336345
self.riscv_config = source.riscv_config;
337346
}
338347
}
339348
}
340349

350+
#[derive(Clone, PartialEq, Eq, Debug)]
351+
pub struct CratePath(pub syn::Path);
352+
353+
impl Default for CratePath {
354+
fn default() -> Self {
355+
let mut segments = Punctuated::new();
356+
segments.push(path_segment(Ident::new("crate", Span::call_site())));
357+
Self(syn::Path {
358+
leading_colon: None,
359+
segments,
360+
})
361+
}
362+
}
363+
364+
#[cfg(feature = "serde")]
365+
impl<'de> serde::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

+5-5
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)