Skip to content
Open
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
33 changes: 23 additions & 10 deletions src/bundler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const LIBRS_FILENAME: &str = "src/lib.rs";
lazy_static! {
static ref COMMENT_RE: Regex = source_line_regex(r" ").unwrap();
static ref WARN_RE: Regex = source_line_regex(r" #!\[warn\(.*").unwrap();
static ref USECRATE_RE: Regex = source_line_regex(r" use crate::(?P<submod>.*);$").unwrap();
static ref USECRATE_RE: Regex = source_line_regex(r" use crate::(?P<submod>.*)$").unwrap();
static ref MINIFY_RE: Regex = Regex::new(r"^\s*(?P<contents>.*)\s*$").unwrap();
}

Expand Down Expand Up @@ -219,19 +219,32 @@ impl<'a> Bundler<'a> {
writeln!(self.bundle_file, "pub mod {} {{", mod_name)?;
self.skip_use.insert(String::from(mod_import));

let mut inner_submod = String::new();
while mod_reader.read_line(&mut line)? > 0 {
line.truncate(line.trim_end().len());
if COMMENT_RE.is_match(&line) || WARN_RE.is_match(&line) {
} else if let Some(cap) = USECRATE_RE.captures(&line) {
let submodname = cap
.name("submod")
.ok_or_else(|| anyhow!("capture not found"))?
.as_str();
write!(self.bundle_file, "use ")?;
for _ in 0..lvl {
write!(self.bundle_file, "super::")?;
} else if USECRATE_RE.is_match(&line) || !inner_submod.is_empty() {
if let Some(cap) = USECRATE_RE.captures(&line) {
write!(self.bundle_file, "use ")?;
for _ in 0..lvl {
write!(self.bundle_file, "super::")?;
}
let submodname = cap
.name("submod")
.ok_or_else(|| anyhow!("capture not found"))?
.as_str();
if submodname.ends_with(';') {
writeln!(self.bundle_file, "{}", submodname)?;
} else {
inner_submod = submodname.to_string();
}
} else {
inner_submod.push_str(&line);
if inner_submod.ends_with(';') {
writeln!(self.bundle_file, "{}", inner_submod)?;
inner_submod.clear();
}
}
writeln!(self.bundle_file, "{};", submodname)?;
} else if let Some(cap) = mod_re.captures(&line) {
let submodname = cap
.name("m")
Expand Down
2 changes: 2 additions & 0 deletions tests/testdata/input/usecrate/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
pub mod submod1;
pub mod submod2;
pub mod submod3;
pub mod submod4;
2 changes: 2 additions & 0 deletions tests/testdata/input/usecrate/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
extern crate usecrate;
use usecrate::submod2::hello_world2;
use usecrate::submod4::hello_world4;

fn main() {
hello_world2();
hello_world4();
}
3 changes: 3 additions & 0 deletions tests/testdata/input/usecrate/src/submod3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn hello_world3__long_identifier_name_for_multiline() {
println!("Hello, world 3!");
}
10 changes: 10 additions & 0 deletions tests/testdata/input/usecrate/src/submod4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use crate::{
submod1,
submod3::hello_world3__long_identifier_name_for_multiline,
};

pub fn hello_world4() {
submod1::hello_world1();
hello_world3__long_identifier_name_for_multiline();
println!("Hello, world 4!");
}
15 changes: 15 additions & 0 deletions tests/testdata/output/usecrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,23 @@ pub fn hello_world2() {
println!("Hello, world 2!");
}
}
pub mod submod3 {
pub fn hello_world3__long_identifier_name_for_multiline() {
println!("Hello, world 3!");
}
}
pub mod submod4 {
use super::{ submod1, submod3::hello_world3__long_identifier_name_for_multiline,};
pub fn hello_world4() {
submod1::hello_world1();
hello_world3__long_identifier_name_for_multiline();
println!("Hello, world 4!");
}
}
}
use self::usecrate::submod2::hello_world2;
use self::usecrate::submod4::hello_world4;
fn main() {
hello_world2();
hello_world4();
}