Skip to content

Commit

Permalink
refactoring, wip
Browse files Browse the repository at this point in the history
- lazy_static all regex's
  • Loading branch information
lpenz committed May 24, 2022
1 parent ee1f298 commit b50437d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 31 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ repository = "https://github.com/lpenz/rust-sourcebundler.git"
edition = "2018"

[dependencies]
lazy_static = "1.4.0"
regex = "1.5.6"
74 changes: 43 additions & 31 deletions src/bundler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,37 @@ use std::io::BufReader;
use std::io::Write;
use std::path::Path;

use lazy_static::lazy_static;
use regex::Regex;

const LIBRS_FILENAME: &str = "src/lib.rs";
lazy_static! {
static ref COMMENT_RE: Regex = source_line_regex(r" ");
static ref WARN_RE: Regex = source_line_regex(r" #!\[warn\(.*");
static ref MINIFY_RE: Regex = Regex::new(r"^\s*(?P<contents>.*)\s*$").unwrap();
}

#[derive(Debug, Clone)]
pub struct Bundler<'a> {
binrs_filename: &'a Path,
bundle_filename: &'a Path,
librs_filename: &'a Path,
comment_re: Regex,
warn_re: Regex,
_crate_name: &'a str,
crate_name: &'a str,
skip_use: HashSet<String>,
minify_re: Option<Regex>,
minify: bool,
}

impl<'a> Default for Bundler<'a> {
fn default() -> Self {
Bundler {
binrs_filename: Path::new(""),
bundle_filename: Path::new(""),
librs_filename: Path::new(LIBRS_FILENAME),
crate_name: "",
skip_use: HashSet::new(),
minify: false,
}
}
}

/// Defines a regex to match a line of rust source.
Expand All @@ -43,35 +60,30 @@ fn source_line_regex<S: AsRef<str>>(source_regex: S) -> Regex {
}

impl<'a> Bundler<'a> {
pub fn new(binrs_filename: &'a Path, bundle_filename: &'a Path) -> Bundler<'a> {
Bundler {
binrs_filename,
bundle_filename,
librs_filename: Path::new(LIBRS_FILENAME),
comment_re: source_line_regex(r" "),
warn_re: source_line_regex(r" #!\[warn\(.*"),
_crate_name: "",
skip_use: HashSet::new(),
minify_re: None,
}
pub fn new() -> Bundler<'a> {
Bundler::default()
}

pub fn minify_set(&mut self, enable: bool) {
self.minify_re = if enable {
Some(Regex::new(r"^\s*(?P<contents>.*)\s*$").unwrap())
} else {
None
};
pub fn binrs(&mut self, filename: &'a Path) {
self.binrs_filename = filename;
}

pub fn output(&mut self, filename: &'a Path) {
self.bundle_filename = filename;
}

pub fn crate_name(&mut self, name: &'a str) {
self._crate_name = name;
self.crate_name = name;
}

pub fn minify_set(&mut self, enable: bool) {
self.minify = enable;
}

pub fn run(&mut self) {
let mut o = File::create(&self.bundle_filename)
.unwrap_or_else(|_| panic!("error creating {}", &self.bundle_filename.display()));
self.binrs(&mut o).unwrap_or_else(|_| {
self.binrs_process(&mut o).unwrap_or_else(|_| {
panic!(
"error creating bundle {} for {}",
self.bundle_filename.display(),
Expand All @@ -84,22 +96,22 @@ impl<'a> Bundler<'a> {
/// From the file that has the main() function, expand "extern
/// crate <_crate_name>" into lib.rs contents, and smartly skips
/// "use <_crate_name>::" lines.
fn binrs(&mut self, mut o: &mut File) -> Result<(), io::Error> {
fn binrs_process(&mut self, mut o: &mut File) -> Result<(), io::Error> {
let bin_fd = File::open(self.binrs_filename)?;
let mut bin_reader = BufReader::new(&bin_fd);

let extcrate_re = source_line_regex(format!(
r" extern crate {} ; ",
String::from(self._crate_name)
String::from(self.crate_name)
));
let usecrate_re = source_line_regex(
format!(r" use {} :: (.*) ; ", String::from(self._crate_name)).as_str(),
format!(r" use {} :: (.*) ; ", String::from(self.crate_name)).as_str(),
);

let mut line = String::new();
while bin_reader.read_line(&mut line).unwrap() > 0 {
line.truncate(line.trim_end().len());
if self.comment_re.is_match(&line) || self.warn_re.is_match(&line) {
if COMMENT_RE.is_match(&line) || WARN_RE.is_match(&line) {
} else if extcrate_re.is_match(&line) {
self.librs(o)?;
} else if let Some(cap) = usecrate_re.captures(&line) {
Expand All @@ -125,7 +137,7 @@ impl<'a> Bundler<'a> {
let mut line = String::new();
while lib_reader.read_line(&mut line).unwrap() > 0 {
line.pop();
if self.comment_re.is_match(&line) || self.warn_re.is_match(&line) {
if COMMENT_RE.is_match(&line) || WARN_RE.is_match(&line) {
} else if let Some(cap) = mod_re.captures(&line) {
let modname = cap.name("m").unwrap().as_str();
if modname != "tests" {
Expand Down Expand Up @@ -172,7 +184,7 @@ impl<'a> Bundler<'a> {

while mod_reader.read_line(&mut line).unwrap() > 0 {
line.truncate(line.trim_end().len());
if self.comment_re.is_match(&line) || self.warn_re.is_match(&line) {
if COMMENT_RE.is_match(&line) || WARN_RE.is_match(&line) {
} else if let Some(cap) = mod_re.captures(&line) {
let submodname = cap.name("m").unwrap().as_str();
if submodname != "tests" {
Expand All @@ -192,8 +204,8 @@ impl<'a> Bundler<'a> {
}

fn write_line(&self, mut o: &mut File, line: &str) -> Result<(), io::Error> {
if let Some(ref minify_re) = self.minify_re {
writeln!(&mut o, "{}", minify_re.replace_all(line, "$contents"))
if self.minify {
writeln!(&mut o, "{}", MINIFY_RE.replace_all(line, "$contents"))
} else {
writeln!(&mut o, "{}", line)
}
Expand Down

0 comments on commit b50437d

Please sign in to comment.