Skip to content

Commit 68d69b6

Browse files
committed
html-url
1 parent 4a1847c commit 68d69b6

File tree

7 files changed

+52
-10
lines changed

7 files changed

+52
-10
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 `html-url` option to access `svdtools html` files from docs
1011
- Move `Reg` in separate file
1112
- Use `warning` class in docs
1213
- Refactor `Accessor`

Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ serde_json = { version = "1.0.85", optional = true }
5757
serde_yaml = { version = "0.9.11", optional = true }
5858
regex = "1.10.0"
5959
html-escape = "0.2"
60+
url = { version = "2.5", features = ["serde"] }
6061

6162
[dependencies.svd-parser]
6263
features = ["expand"]

src/config.rs

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub struct Config {
3535
pub ident_formats_theme: Option<IdentFormatsTheme>,
3636
pub field_names_for_enums: bool,
3737
pub base_address_shift: u64,
38+
pub html_url: Option<url::Url>,
3839
}
3940

4041
#[allow(clippy::upper_case_acronyms)]

src/generate/peripheral.rs

+6
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
8181
}
8282
};
8383

84+
let phtml = config.html_url.as_ref().map(|url| {
85+
let doc = format!("See peripheral [structure]({url}#{})", &path.peripheral);
86+
quote!(#[doc = ""] #[doc = #doc])
87+
});
88+
8489
let per_to_tokens = |out: &mut TokenStream,
8590
feature_attribute: &TokenStream,
8691
description: &str,
@@ -89,6 +94,7 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
8994
address: LitInt| {
9095
out.extend(quote! {
9196
#[doc = #description]
97+
#phtml
9298
#doc_alias
9399
#feature_attribute
94100
pub struct #p_ty { _marker: PhantomData<*const ()> }

src/generate/register.rs

+33-10
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ pub fn render(
108108
return Err(anyhow!("Incorrect access of register {}", register.name));
109109
};
110110

111+
let rpath = path.new_register(&register.name);
111112
let mut alias_doc = format!(
112113
"{name} ({accs}) register accessor: {description}{}{}",
113114
api_docs(
@@ -116,6 +117,9 @@ pub fn render(
116117
register.properties.reset_value.is_some(),
117118
&mod_ty,
118119
false,
120+
&register,
121+
&rpath,
122+
config,
119123
)?,
120124
read_action_docs(access.can_read(), register.read_action),
121125
);
@@ -128,13 +132,7 @@ pub fn render(
128132
#doc_alias
129133
pub type #reg_ty = crate::Reg<#mod_ty::#regspec_ty>;
130134
});
131-
let mod_items = render_register_mod(
132-
register,
133-
access,
134-
&path.new_register(&register.name),
135-
index,
136-
config,
137-
)?;
135+
let mod_items = render_register_mod(register, access, &rpath, index, config)?;
138136

139137
out.extend(quote! {
140138
#[doc = #description]
@@ -170,6 +168,9 @@ fn api_docs(
170168
can_reset: bool,
171169
module: &Ident,
172170
inmodule: bool,
171+
register: &Register,
172+
rpath: &RegisterPath,
173+
config: &Config,
173174
) -> Result<String, std::fmt::Error> {
174175
fn method(s: &str) -> String {
175176
format!("[`{s}`](crate::Reg::{s})")
@@ -211,13 +212,35 @@ fn api_docs(
211212

212213
doc.push_str("See [API](https://docs.rs/svd2rust/#read--modify--write-api).");
213214

215+
if let Some(url) = config.html_url.as_ref() {
216+
let first_idx = if let Register::Array(_, dim) = &register {
217+
dim.indexes().next()
218+
} else {
219+
None
220+
};
221+
let rname = if let Some(idx) = first_idx {
222+
let idx = format!("[{idx}]");
223+
rpath.name.replace("[%s]", &idx).replace("%s", &idx)
224+
} else {
225+
rpath.name.to_string()
226+
};
227+
// TODO: support html_urls for registers in cluster
228+
if rpath.block.path.is_empty() {
229+
doc.push_str(&format!(
230+
"\n\nSee register [structure]({url}#{}:{})",
231+
rpath.peripheral(),
232+
rname
233+
));
234+
}
235+
}
236+
214237
Ok(doc)
215238
}
216239

217240
pub fn render_register_mod(
218241
register: &Register,
219242
access: Access,
220-
path: &RegisterPath,
243+
rpath: &RegisterPath,
221244
index: &Index,
222245
config: &Config,
223246
) -> Result<TokenStream> {
@@ -312,7 +335,7 @@ pub fn render_register_mod(
312335
access,
313336
properties,
314337
&mut mod_items,
315-
path,
338+
rpath,
316339
index,
317340
config,
318341
)?;
@@ -361,7 +384,7 @@ pub fn render_register_mod(
361384

362385
let doc = format!(
363386
"{description}{}{}",
364-
api_docs(can_read, can_write, can_reset, &mod_ty, true)?,
387+
api_docs(can_read, can_write, can_reset, &mod_ty, true, register, rpath, config)?,
365388
read_action_docs(access.can_read(), register.read_action),
366389
);
367390

src/main.rs

+8
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,14 @@ Allowed cases are `unchanged` (''), `pascal` ('p'), `constant` ('c') and `snake`
267267
Useful for soft-cores where the peripheral address range isn't necessarily fixed.
268268
Ignore this option if you are not building your own FPGA based soft-cores."),
269269
)
270+
.arg(
271+
Arg::new("html_url")
272+
.long("html-url")
273+
.alias("html_url")
274+
.help("Path to chip HTML generated by svdtools")
275+
.action(ArgAction::Set)
276+
.value_name("URL"),
277+
)
270278
.arg(
271279
Arg::new("log_level")
272280
.long("log")

0 commit comments

Comments
 (0)