Skip to content

Commit cbb1ade

Browse files
bors[bot]adamgreig
andauthored
Merge #510
510: Add -m switch to create a module instead of a lib r=burrbull a=adamgreig Currently projects like stm32-rs have to strip out all the attributes during build to create a file suitable for use as a module. This PR adds a new `-m` switch instead, which causes a `mod.rs` file to be created without the inner attributes or the `pub mod generic`/`use generic::*` lines which would live in the top-level `lib.rs`. Co-authored-by: Adam Greig <[email protected]>
2 parents 5a762c1 + b8881ec commit cbb1ade

File tree

4 files changed

+55
-32
lines changed

4 files changed

+55
-32
lines changed

Diff for: CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Added
1111

12+
- New `-m` switch generates a `mod.rs` file instead of `lib.rs`, which can
13+
be used as a module inside a crate without further modification.
14+
1215
- Generated crates now contain the git commit hash and date of svd2rust
1316
compilation.
1417

Diff for: src/generate/device.rs

+34-29
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub fn render(
1616
target: Target,
1717
nightly: bool,
1818
generic_mod: bool,
19+
make_mod: bool,
1920
device_x: &mut String,
2021
) -> Result<TokenStream> {
2122
let mut out = TokenStream::new();
@@ -51,29 +52,31 @@ pub fn render(
5152
});
5253
}
5354

54-
out.extend(quote! {
55-
#![doc = #doc]
56-
// Deny a subset of warnings
57-
#![deny(const_err)]
58-
#![deny(dead_code)]
59-
#![deny(improper_ctypes)]
60-
#![deny(missing_docs)]
61-
#![deny(no_mangle_generic_items)]
62-
#![deny(non_shorthand_field_patterns)]
63-
#![deny(overflowing_literals)]
64-
#![deny(path_statements)]
65-
#![deny(patterns_in_fns_without_body)]
66-
#![deny(private_in_public)]
67-
#![deny(unconditional_recursion)]
68-
#![deny(unused_allocation)]
69-
#![deny(unused_comparisons)]
70-
#![deny(unused_parens)]
71-
#![deny(while_true)]
72-
// Explicitly allow a few warnings that may be verbose
73-
#![allow(non_camel_case_types)]
74-
#![allow(non_snake_case)]
75-
#![no_std]
76-
});
55+
out.extend(quote! { #![doc = #doc] });
56+
if !make_mod {
57+
out.extend(quote! {
58+
// Deny a subset of warnings
59+
#![deny(const_err)]
60+
#![deny(dead_code)]
61+
#![deny(improper_ctypes)]
62+
#![deny(missing_docs)]
63+
#![deny(no_mangle_generic_items)]
64+
#![deny(non_shorthand_field_patterns)]
65+
#![deny(overflowing_literals)]
66+
#![deny(path_statements)]
67+
#![deny(patterns_in_fns_without_body)]
68+
#![deny(private_in_public)]
69+
#![deny(unconditional_recursion)]
70+
#![deny(unused_allocation)]
71+
#![deny(unused_comparisons)]
72+
#![deny(unused_parens)]
73+
#![deny(while_true)]
74+
// Explicitly allow a few warnings that may be verbose
75+
#![allow(non_camel_case_types)]
76+
#![allow(non_snake_case)]
77+
#![no_std]
78+
});
79+
}
7780

7881
out.extend(quote! {
7982
use core::ops::Deref;
@@ -145,12 +148,14 @@ pub fn render(
145148
if generic_mod {
146149
writeln!(File::create("generic.rs")?, "{}", generic_file)?;
147150

148-
out.extend(quote! {
149-
#[allow(unused_imports)]
150-
use generic::*;
151-
#[doc="Common register and bit access and modify traits"]
152-
pub mod generic;
153-
});
151+
if !make_mod {
152+
out.extend(quote! {
153+
#[allow(unused_imports)]
154+
use generic::*;
155+
#[doc="Common register and bit access and modify traits"]
156+
pub mod generic;
157+
});
158+
}
154159
} else {
155160
let tokens = syn::parse_file(generic_file)?.into_token_stream();
156161

Diff for: src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ pub fn generate(xml: &str, target: Target, nightly: bool) -> Result<Generation>
515515

516516
let device = svd::parse(xml)?;
517517
let mut device_x = String::new();
518-
let items = generate::device::render(&device, target, nightly, false, &mut device_x)
518+
let items = generate::device::render(&device, target, nightly, false, false, &mut device_x)
519519
.or(Err(SvdError::Render))?;
520520

521521
let mut lib_rs = String::new();

Diff for: src/main.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ fn run() -> Result<()> {
4545
.short("g")
4646
.help("Push generic mod in separate file"),
4747
)
48+
.arg(
49+
Arg::with_name("make_mod")
50+
.long("make_mod")
51+
.short("m")
52+
.help("Create mod.rs instead of lib.rs, without inner attributes"),
53+
)
4854
.arg(
4955
Arg::with_name("log_level")
5056
.long("log")
@@ -91,10 +97,19 @@ fn run() -> Result<()> {
9197
let nightly = matches.is_present("nightly_features");
9298

9399
let generic_mod = matches.is_present("generic_mod");
100+
let make_mod = matches.is_present("make_mod");
94101

95102
let mut device_x = String::new();
96-
let items = generate::device::render(&device, target, nightly, generic_mod, &mut device_x)?;
97-
let mut file = File::create("lib.rs").expect("Couldn't create lib.rs file");
103+
let items = generate::device::render(
104+
&device,
105+
target,
106+
nightly,
107+
generic_mod,
108+
make_mod,
109+
&mut device_x,
110+
)?;
111+
let filename = if make_mod { "mod.rs" } else { "lib.rs" };
112+
let mut file = File::create(filename).expect("Couldn't create output file");
98113

99114
let data = items.to_string().replace("] ", "]\n");
100115
file.write_all(data.as_ref())

0 commit comments

Comments
 (0)