Skip to content

Commit

Permalink
Support top level itemless modules (djmcgill#14)
Browse files Browse the repository at this point in the history
* add support for toplevel itemless modules

* clippy
  • Loading branch information
djmcgill authored Apr 15, 2021
1 parent 10487b8 commit fcb397a
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Form
![crates.io badge](https://img.shields.io/crates/v/form.svg)
[![CircleCI](https://circleci.com/gh/djmcgill/form/tree/master.svg?style=svg)](https://circleci.com/gh/djmcgill/form/tree/master)
[![CircleCI](https://circleci.com/gh/djmcgill/form/tree/main.svg?style=svg)](https://circleci.com/gh/djmcgill/form/tree/main)

A library for splitting apart a large file with multiple modules into the idiomatic rust directory structure, intended for use with svd2rust.
Creates a lib.rs as well as a subdirectory structure in the target directory. It does NOT create the cargo project or the cargo manifest file.
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ fn run() -> Result<(), Error> {
create_directory_structure(opts.output_dir, opts.input)?;
println!("Completed successfully");
}
return Ok(());
Ok(())
}
10 changes: 5 additions & 5 deletions src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ pub struct FormOpts {

impl FormOpts {
pub fn from_args() -> Result<Option<Self>, Error> {
const SHORT_INPUT: &'static str = "i";
const SHORT_OUTDIR: &'static str = "o";
const SHORT_HELP: &'static str = "h";
const SHORT_VERSION: &'static str = "v";
const SHORT_INPUT: &str = "i";
const SHORT_OUTDIR: &str = "o";
const SHORT_HELP: &str = "h";
const SHORT_VERSION: &str = "v";

let args: Vec<String> = env::args().collect();
let program = args[0].clone();
Expand All @@ -42,7 +42,7 @@ impl FormOpts {
}
let output_dir = matches
.opt_str(SHORT_OUTDIR)
.ok_or(err_msg("Output directory missing"))?;
.ok_or_else(|| err_msg("Output directory missing"))?;
let input = read_input(matches.opt_str(SHORT_INPUT))?;

Ok(Some(FormOpts { output_dir, input }))
Expand Down
17 changes: 13 additions & 4 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub fn create_directory_structure<P: AsRef<Path>>(

let mut folder = FileIntoMods {
current_dir: &base_dir,
top_level: true,
};

// Why doesn't syn::Fold::fold handle errors again?
Expand All @@ -52,11 +53,13 @@ pub fn create_directory_structure<P: AsRef<Path>>(
#[derive(Debug)]
struct FileIntoMods<P: AsRef<Path> + Send + Sync> {
current_dir: P,
top_level: bool,
}
impl<P: AsRef<Path> + Send + Sync> FileIntoMods<P> {
fn sub_mod<Q: AsRef<Path>>(&self, path: Q) -> FileIntoMods<PathBuf> {
FileIntoMods {
current_dir: self.current_dir.as_ref().join(path),
top_level: false,
}
}
}
Expand Down Expand Up @@ -100,7 +103,7 @@ impl<P: AsRef<Path> + Send + Sync> FileIntoMods<P> {

impl<P: AsRef<Path> + Send + Sync> Fold for FileIntoMods<P> {
fn fold_item(&mut self, mut item: Item) -> Item {
for (mod_name, mod_file) in extract_mod(&mut item) {
if let Some((mod_name, mod_file)) = extract_mod(&mut item, self.top_level) {
self.fold_sub_mod(mod_name, mod_file).unwrap();
}
fold_item(self, item)
Expand All @@ -116,10 +119,16 @@ fn write_mod_file(item_mod: syn::File, file_name: &Path) -> Result<(), Error> {
write_all_tokens(&item_mod, &mut file)
}

fn extract_mod<'a>(node: &'a mut Item) -> Option<(Ident, syn::File)> {
fn extract_mod(node: &mut Item, top_level: bool) -> Option<(Ident, syn::File)> {
if let Item::Mod(mod_item) = &mut *node {
let items = mod_item.content.take().unwrap().1;
Some((mod_item.ident.clone(), make_file(items)))
if let Some(item_content) = mod_item.content.take() {
let items = item_content.1;
Some((mod_item.ident.clone(), make_file(items)))
} else if top_level {
None
} else {
panic!("Moving nested non-inline `mod` declarations not currently supported.")
}
} else {
None
}
Expand Down
2 changes: 2 additions & 0 deletions tests/resources/after/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ pub mod interrupt;
pub use cortex_m::peripheral::TPIU;
pub struct AC;
pub mod ac;

mod foo;
2 changes: 2 additions & 0 deletions tests/resources/small-lib-before.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ pub mod ac {
}
pub const AC: AC = AC;
}

mod foo;

0 comments on commit fcb397a

Please sign in to comment.