Skip to content

little cleanups #845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 18 additions & 22 deletions src/generate/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,9 @@ pub fn render(
register.read_action,
)?
);
if mod_ty != "cfg" {
alias_doc +=
format!("\n\nFor information about available fields see [`mod@{mod_ty}`] module")
.as_str();
}
alias_doc +=
format!("\n\nFor information about available fields see [`mod@{mod_ty}`] module")
.as_str();
let mut out = TokenStream::new();
out.extend(quote! {
#[doc = #alias_doc]
Expand Down Expand Up @@ -158,21 +156,21 @@ fn api_docs(
read_action: Option<ReadAction>,
) -> Result<String, std::fmt::Error> {
fn method(s: &str) -> String {
format!("[`{s}`](crate::generic::Reg::{s})")
format!("[`{s}`](crate::Reg::{s})")
}

let mut doc = String::new();

if can_read {
write!(
doc,
"You can {} this register and get [`{module}::R`]{}. ",
"You can {} this register and get [`{module}::R`]{}.",
method("read"),
if inmodule { "(R)" } else { "" },
)?;

if let Some(action) = read_action {
doc.push_str("WARN: ");
doc.push_str(" WARN: ");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could make this a little better with rust-lang/rust#106561 now :D

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's interesting. I'll test. The question is should we up MSRV or not?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since it's just css it doesn't break when the class doesn't exist. No msrv bump needed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like done.
Do you have any other ideas on improving documentation?

doc.push_str(match action {
ReadAction::Clear => "The register is **cleared** (set to zero) following a read operation.",
ReadAction::Set => "The register is **set** (set to ones) following a read operation.",
Expand Down Expand Up @@ -203,7 +201,7 @@ fn api_docs(
}

if can_read && can_write {
write!(doc, "You can also {} this register. ", method("modify"),)?;
write!(doc, "You can also {} this register. ", method("modify"))?;
}

doc.push_str("See [API](https://docs.rs/svd2rust/#read--modify--write-api).");
Expand All @@ -220,12 +218,13 @@ pub fn render_register_mod(
) -> Result<TokenStream> {
let properties = &register.properties;
let name = util::name_of(register, config.ignore_groups);
let rname = &register.name;
let span = Span::call_site();
let regspec_ty = regspec(&name, config, span);
let mod_ty = ident(&name, config, "register_mod", span);
let rsize = properties
.size
.ok_or_else(|| anyhow!("Register {} has no `size` field", register.name))?;
.ok_or_else(|| anyhow!("Register {rname} has no `size` field"))?;
let rsize = if rsize < 8 {
8
} else if rsize.is_power_of_two() {
Expand All @@ -236,7 +235,7 @@ pub fn render_register_mod(
let rty = rsize.to_ty()?;
let description = util::escape_special_chars(
util::respace(&register.description.clone().unwrap_or_else(|| {
warn!("Missing description for register {}", register.name);
warn!("Missing description for register {rname}");
Default::default()
}))
.as_ref(),
Expand All @@ -249,15 +248,15 @@ pub fn render_register_mod(
let can_reset = properties.reset_value.is_some();

if can_read {
let desc = format!("Register `{}` reader", register.name);
let desc = format!("Register `{rname}` reader");
mod_items.extend(quote! {
#[doc = #desc]
pub type R = crate::R<#regspec_ty>;
});
}

if can_write {
let desc = format!("Register `{}` writer", register.name);
let desc = format!("Register `{rname}` writer");
mod_items.extend(quote! {
#[doc = #desc]
pub type W = crate::W<#regspec_ty>;
Expand Down Expand Up @@ -663,15 +662,11 @@ pub fn fields(
let rv = properties.reset_value.map(|rv| (rv >> offset) & mask);
let fty = width.to_ty()?;

let use_mask;
let use_cast;
if let Some(size) = properties.size {
let (use_cast, use_mask) = if let Some(size) = properties.size {
let size = size.to_ty_width()?;
use_cast = size != width.to_ty_width()?;
use_mask = size != width;
(size != width.to_ty_width()?, size != width)
} else {
use_cast = true;
use_mask = true;
(true, true)
};

let mut lookup_results = Vec::new();
Expand Down Expand Up @@ -724,7 +719,8 @@ pub fn fields(

let brief_suffix = if let Field::Array(_, de) = &f {
if let Some(range) = de.indexes_as_range() {
format!("({}-{})", *range.start(), *range.end())
let (start, end) = range.into_inner();
format!("({start}-{end})")
} else {
let suffixes: Vec<_> = de.indexes().collect();
format!("({})", suffixes.join(","))
Expand Down Expand Up @@ -1420,7 +1416,7 @@ impl Variant {
.ok_or_else(|| anyhow!("EnumeratedValue {} has no `<value>` entry", ev.name))?;
Self::from_value(value, ev, config)
})
.collect::<Result<Vec<_>>>()
.collect()
}
fn from_value(value: u64, ev: &EnumeratedValue, config: &Config) -> Result<Self> {
let span = Span::call_site();
Expand Down
20 changes: 5 additions & 15 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,7 @@ impl U32Ext for u32 {
16 => "u16",
32 => "u32",
64 => "u64",
_ => {
return Err(anyhow!(
"can't convert {} bits into register size type",
*self
))
}
_ => return Err(anyhow!("can't convert {self} bits into register size type")),
})
}
fn to_ty(&self) -> Result<Ident> {
Expand All @@ -374,8 +369,7 @@ impl U32Ext for u32 {
33..=64 => "u64",
_ => {
return Err(anyhow!(
"can't convert {} bits into a Rust integral type",
*self
"can't convert {self} bits into a Rust integral type"
))
}
},
Expand All @@ -392,8 +386,7 @@ impl U32Ext for u32 {
33..=64 => 64,
_ => {
return Err(anyhow!(
"can't convert {} bits into a Rust integral type width",
*self
"can't convert {self} bits into a Rust integral type width"
))
}
})
Expand Down Expand Up @@ -437,11 +430,8 @@ pub trait DimSuffix {
impl DimSuffix for str {
fn expand_dim(&self, suffix: &str) -> Cow<str> {
if self.contains("%s") {
if self.contains("[%s]") {
self.replace("[%s]", suffix).into()
} else {
self.replace("%s", suffix).into()
}
self.replace(if self.contains("[%s]") { "[%s]" } else { "%s" }, suffix)
.into()
} else {
self.into()
}
Expand Down
Loading