Skip to content

replace_suffix #425

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 1 commit into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
71 changes: 17 additions & 54 deletions src/generate/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,9 @@ pub fn render(
pub mod #name_sc #open
});

for item in mod_items {
out.extend(quote! {
#item
});
}
out.extend(mod_items);

out.extend(quote! {
#close
});
close.to_tokens(&mut out);

Ok(out)
}
Expand Down Expand Up @@ -730,12 +724,13 @@ fn cluster_block(
util::escape_brackets(util::respace(c.description.as_ref().unwrap_or(&c.name)).as_ref());

// Generate the register block.
let mod_name = match c {
Cluster::Single(info) => &info.name,
Cluster::Array(info, _ai) => &info.name,
}
.replace("[%s]", "")
.replace("%s", "");
let mod_name = util::replace_suffix(
match c {
Cluster::Single(info) => &info.name,
Cluster::Array(info, _ai) => &info.name,
},
"",
);
let name_sc = Ident::new(&mod_name.to_sanitized_snake_case(), Span::call_site());

let defaults = c.default_register_properties.derive_from(defaults);
Expand Down Expand Up @@ -794,8 +789,6 @@ fn expand_svd_register(register: &Register, name: Option<&str>) -> Vec<syn::Fiel
match register {
Register::Single(_info) => out.push(convert_svd_register(register, name)),
Register::Array(info, array_info) => {
let has_brackets = info.name.contains("[%s]");

let indices = array_info
.dim_index
.as_ref()
Expand All @@ -808,18 +801,10 @@ fn expand_svd_register(register: &Register, name: Option<&str>) -> Vec<syn::Fiel
)
});

for (idx, _i) in indices.iter().zip(0..) {
let nb_name = if has_brackets {
info.name.replace("[%s]", idx)
} else {
info.name.replace("%s", idx)
};
let ty_name = util::replace_suffix(&info.name, "");

let ty_name = if has_brackets {
info.name.replace("[%s]", "")
} else {
info.name.replace("%s", "")
};
for (idx, _i) in indices.iter().zip(0..) {
let nb_name = util::replace_suffix(&info.name, idx);

let ty = name_to_ty(&ty_name, name);

Expand Down Expand Up @@ -849,13 +834,7 @@ fn convert_svd_register(register: &Register, name: Option<&str>) -> syn::Field {
syn::Type::Path(parse_str::<syn::TypePath>(&name_to_ty(&info.name, name)).unwrap()),
),
Register::Array(info, array_info) => {
let has_brackets = info.name.contains("[%s]");

let nb_name = if has_brackets {
info.name.replace("[%s]", "")
} else {
info.name.replace("%s", "")
};
let nb_name = util::replace_suffix(&info.name, "");

let ty = syn::Type::Array(
parse_str::<syn::TypeArray>(&format!(
Expand Down Expand Up @@ -883,8 +862,6 @@ fn expand_svd_cluster(cluster: &Cluster) -> Vec<syn::Field> {
match &cluster {
Cluster::Single(_info) => out.push(convert_svd_cluster(cluster)),
Cluster::Array(info, array_info) => {
let has_brackets = info.name.contains("[%s]");

let indices = array_info
.dim_index
.as_ref()
Expand All @@ -897,18 +874,10 @@ fn expand_svd_cluster(cluster: &Cluster) -> Vec<syn::Field> {
)
});

for (idx, _i) in indices.iter().zip(0..) {
let name = if has_brackets {
info.name.replace("[%s]", idx)
} else {
info.name.replace("%s", idx)
};
let ty_name = util::replace_suffix(&info.name, "");

let ty_name = if has_brackets {
info.name.replace("[%s]", "")
} else {
info.name.replace("%s", "")
};
for (idx, _i) in indices.iter().zip(0..) {
let name = util::replace_suffix(&info.name, idx);

let ty = name_to_ty(&ty_name);

Expand All @@ -929,13 +898,7 @@ fn convert_svd_cluster(cluster: &Cluster) -> syn::Field {
),
),
Cluster::Array(info, array_info) => {
let has_brackets = info.name.contains("[%s]");

let name = if has_brackets {
info.name.replace("[%s]", "")
} else {
info.name.replace("%s", "")
};
let name = util::replace_suffix(&info.name, "");

let ty = syn::Type::Array(
parse_str::<syn::TypeArray>(&format!(
Expand Down
9 changes: 3 additions & 6 deletions src/generate/register.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::quote::ToTokens;
use crate::svd::{
Access, BitRange, EnumeratedValues, Field, Peripheral, Register, RegisterCluster,
RegisterProperties, Usage, WriteConstraint,
Expand Down Expand Up @@ -122,9 +123,7 @@ pub fn render(

mod_items.extend(r_impl_items);

mod_items.extend(quote! {
#close
});
close.to_tokens(&mut mod_items);
}

if can_write {
Expand All @@ -134,9 +133,7 @@ pub fn render(

mod_items.extend(w_impl_items);

mod_items.extend(quote! {
#close
});
close.to_tokens(&mut mod_items);
}

let mut out = TokenStream::new();
Expand Down
57 changes: 27 additions & 30 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::borrow::Cow;

use crate::quote::{ToTokens, TokenStreamExt};
use crate::quote::ToTokens;
use crate::svd::{Access, Cluster, Register, RegisterCluster};
use inflections::Inflect;
use proc_macro2::{Ident, Literal, Span, TokenStream};
Expand Down Expand Up @@ -185,13 +185,15 @@ pub fn escape_brackets(s: &str) -> String {
pub fn name_of(register: &Register) -> Cow<str> {
match register {
Register::Single(info) => Cow::from(&info.name),
Register::Array(info, _) => {
if info.name.contains("[%s]") {
info.name.replace("[%s]", "").into()
} else {
info.name.replace("%s", "").into()
}
}
Register::Array(info, _) => replace_suffix(&info.name, "").into(),
}
}

pub fn replace_suffix(name: &str, suffix: &str) -> String {
if name.contains("[%s]") {
name.replace("[%s]", suffix)
} else {
name.replace("%s", suffix)
}
}

Expand Down Expand Up @@ -250,19 +252,12 @@ pub fn hex(n: u64) -> TokenStream {

/// Turns `n` into an unsuffixed token
pub fn unsuffixed(n: u64) -> TokenStream {
let mut t = TokenStream::new();
t.append(Literal::u64_unsuffixed(n));
t
Literal::u64_unsuffixed(n).into_token_stream()
}

pub fn unsuffixed_or_bool(n: u64, width: u32) -> TokenStream {
if width == 1 {
let mut t = TokenStream::new();
t.append(Ident::new(
if n == 0 { "false" } else { "true" },
Span::call_site(),
));
t
Ident::new(if n == 0 { "false" } else { "true" }, Span::call_site()).into_token_stream()
} else {
unsuffixed(n)
}
Expand All @@ -275,19 +270,21 @@ pub trait U32Ext {

impl U32Ext for u32 {
fn to_ty(&self) -> Result<Ident> {
let span = Span::call_site();
Ok(match *self {
1 => Ident::new("bool", span),
2..=8 => Ident::new("u8", span),
9..=16 => Ident::new("u16", span),
17..=32 => Ident::new("u32", span),
33..=64 => Ident::new("u64", span),
_ => {
return Err(
format!("can't convert {} bits into a Rust integral type", *self).into(),
)
}
})
Ok(Ident::new(
match *self {
1 => "bool",
2..=8 => "u8",
9..=16 => "u16",
17..=32 => "u32",
33..=64 => "u64",
_ => {
return Err(
format!("can't convert {} bits into a Rust integral type", *self).into(),
)
}
},
Span::call_site(),
))
}

fn to_ty_width(&self) -> Result<u32> {
Expand Down