diff --git a/CHANGELOG.md b/CHANGELOG.md index 5848c670..9db10df1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added +- New `-m` switch generates a `mod.rs` file instead of `lib.rs`, which can + be used as a module inside a crate without further modification. + - Generated crates now contain the git commit hash and date of svd2rust compilation. diff --git a/src/generate/device.rs b/src/generate/device.rs index dc112ce2..62b4c137 100644 --- a/src/generate/device.rs +++ b/src/generate/device.rs @@ -16,6 +16,7 @@ pub fn render( target: Target, nightly: bool, generic_mod: bool, + make_mod: bool, device_x: &mut String, ) -> Result { let mut out = TokenStream::new(); @@ -51,29 +52,31 @@ pub fn render( }); } - out.extend(quote! { - #![doc = #doc] - // Deny a subset of warnings - #![deny(const_err)] - #![deny(dead_code)] - #![deny(improper_ctypes)] - #![deny(missing_docs)] - #![deny(no_mangle_generic_items)] - #![deny(non_shorthand_field_patterns)] - #![deny(overflowing_literals)] - #![deny(path_statements)] - #![deny(patterns_in_fns_without_body)] - #![deny(private_in_public)] - #![deny(unconditional_recursion)] - #![deny(unused_allocation)] - #![deny(unused_comparisons)] - #![deny(unused_parens)] - #![deny(while_true)] - // Explicitly allow a few warnings that may be verbose - #![allow(non_camel_case_types)] - #![allow(non_snake_case)] - #![no_std] - }); + out.extend(quote! { #![doc = #doc] }); + if !make_mod { + out.extend(quote! { + // Deny a subset of warnings + #![deny(const_err)] + #![deny(dead_code)] + #![deny(improper_ctypes)] + #![deny(missing_docs)] + #![deny(no_mangle_generic_items)] + #![deny(non_shorthand_field_patterns)] + #![deny(overflowing_literals)] + #![deny(path_statements)] + #![deny(patterns_in_fns_without_body)] + #![deny(private_in_public)] + #![deny(unconditional_recursion)] + #![deny(unused_allocation)] + #![deny(unused_comparisons)] + #![deny(unused_parens)] + #![deny(while_true)] + // Explicitly allow a few warnings that may be verbose + #![allow(non_camel_case_types)] + #![allow(non_snake_case)] + #![no_std] + }); + } out.extend(quote! { use core::ops::Deref; @@ -145,12 +148,14 @@ pub fn render( if generic_mod { writeln!(File::create("generic.rs")?, "{}", generic_file)?; - out.extend(quote! { - #[allow(unused_imports)] - use generic::*; - #[doc="Common register and bit access and modify traits"] - pub mod generic; - }); + if !make_mod { + out.extend(quote! { + #[allow(unused_imports)] + use generic::*; + #[doc="Common register and bit access and modify traits"] + pub mod generic; + }); + } } else { let tokens = syn::parse_file(generic_file)?.into_token_stream(); diff --git a/src/lib.rs b/src/lib.rs index a1a4e8a3..e11f863d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -515,7 +515,7 @@ pub fn generate(xml: &str, target: Target, nightly: bool) -> Result let device = svd::parse(xml)?; let mut device_x = String::new(); - let items = generate::device::render(&device, target, nightly, false, &mut device_x) + let items = generate::device::render(&device, target, nightly, false, false, &mut device_x) .or(Err(SvdError::Render))?; let mut lib_rs = String::new(); diff --git a/src/main.rs b/src/main.rs index ce97959a..315d57a6 100755 --- a/src/main.rs +++ b/src/main.rs @@ -45,6 +45,12 @@ fn run() -> Result<()> { .short("g") .help("Push generic mod in separate file"), ) + .arg( + Arg::with_name("make_mod") + .long("make_mod") + .short("m") + .help("Create mod.rs instead of lib.rs, without inner attributes"), + ) .arg( Arg::with_name("log_level") .long("log") @@ -91,10 +97,19 @@ fn run() -> Result<()> { let nightly = matches.is_present("nightly_features"); let generic_mod = matches.is_present("generic_mod"); + let make_mod = matches.is_present("make_mod"); let mut device_x = String::new(); - let items = generate::device::render(&device, target, nightly, generic_mod, &mut device_x)?; - let mut file = File::create("lib.rs").expect("Couldn't create lib.rs file"); + let items = generate::device::render( + &device, + target, + nightly, + generic_mod, + make_mod, + &mut device_x, + )?; + let filename = if make_mod { "mod.rs" } else { "lib.rs" }; + let mut file = File::create(filename).expect("Couldn't create output file"); let data = items.to_string().replace("] ", "]\n"); file.write_all(data.as_ref())