Skip to content

Commit

Permalink
Better error message for writing, better modifiers handling
Browse files Browse the repository at this point in the history
  • Loading branch information
awoimbee committed Jan 20, 2020
1 parent 1934ce5 commit 4c099e6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/file_transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ impl FileTransformer {
let mut file_w = match open_options {
Ok(f) => f,
Err(e) => {
eprintln!("Could not open file ({})", e);
eprintln!("Could not open {} for writing ({})", file_name, e);
return false;
}
};
match file_w.write(self.contents.as_bytes()) {
Ok(_size) => true,
Err(e) => {
eprintln!("Could write to file ({})", e);
eprintln!("Could write to {} ({})", file_name, e);
false
}
}
Expand Down
34 changes: 14 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,18 @@ extern crate clap;

mod file_finder;
mod file_transformer;
mod modifiers;

use file_finder::f_find;
use file_transformer::FileTransformer;
use modifiers::{get_modifier, DynFnPtr};
use rayon::prelude::*;
use regex::Regex;
use std::error::Error;

fn to_upper(s: &str) -> String {
s.to_ascii_uppercase()
}

fn to_lower(s: &str) -> String {
s.to_ascii_lowercase()
}

struct Match {
id: usize,
transform: Option<&'static (dyn (Fn(&str) -> String) + Sync)>,
transform: Option<DynFnPtr>,
}

enum ReplacePart<'a> {
Expand All @@ -38,8 +32,9 @@ fn parse_escaped<'a>(sr: (&str, &'a str)) -> SearchReplace<'a> {
let replace = vec![ReplacePart::Str(repl)];
SearchReplace { search, replace }
}

fn parse<'a>(sr: (&str, &'a str)) -> SearchReplace<'a> {
let reg_match = Regex::new(r"\$\(([0-9]*)([A-Z])?\)").unwrap();
let reg_match = Regex::new(r"\$\(([0-9]*)([A-Z]+)?\)").unwrap();
let (raw_search, raw_replace) = sr;
let search = Regex::new(raw_search).unwrap();
let mut replace = Vec::new();
Expand All @@ -50,11 +45,12 @@ fn parse<'a>(sr: (&str, &'a str)) -> SearchReplace<'a> {
// if is not escaped
if full.start() == 0 || raw_replace.as_bytes()[full.start() - 1] != b'$' {
let id = nb.as_str().parse().unwrap();
let transform: Option<&'static (dyn (Fn(&str) -> String) + Sync)> = match m.get(2) {
Some(c) if c.as_str() == "U" => Some(&to_upper),
Some(c) if c.as_str() == "L" => Some(&to_lower),
let transform: Option<DynFnPtr> = match m.get(2) {
Some(c) => match get_modifier(c.as_str()) {
Some(m) => Some(m),
None => panic!("Unrecognised modifier: {}", c.as_str()),
},
None => None,
Some(c) => panic!("Unrecognised modifier: {}", c.as_str()),
};
replace.push(ReplacePart::Str(&raw_replace[read_ofset..full.start()]));
replace.push(ReplacePart::Match(Match { id, transform }));
Expand Down Expand Up @@ -121,12 +117,10 @@ fn sr_file(fname: &str, search_replace: &[SearchReplace]) {
for part in &sr.replace {
match part {
ReplacePart::Str(stri) => new_text.push_str(stri),
ReplacePart::Match(m) => {
match m.transform {
Some(t) => new_text.push_str(&t(&cap[m.id])),
None => new_text.push_str(&cap[m.id]),
}
}
ReplacePart::Match(m) => match m.transform {
Some(t) => new_text.push_str(&t(&cap[m.id])),
None => new_text.push_str(&cap[m.id]),
},
}
}
ft.reader_replace(start, end, new_text);
Expand Down
34 changes: 34 additions & 0 deletions src/modifiers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
pub type DynFnPtr = &'static (dyn (Fn(&str) -> String) + Sync);

struct Modifier {
s: &'static str,
fn_ptr: DynFnPtr,
}

const MODIFIERS: [Modifier; 2] = [
Modifier {
s: "U",
fn_ptr: &to_upper,
},
Modifier {
s: "L",
fn_ptr: &to_lower,
},
];

fn to_upper(s: &str) -> String {
s.to_ascii_uppercase()
}

fn to_lower(s: &str) -> String {
s.to_ascii_lowercase()
}

pub fn get_modifier(requested: &str) -> Option<DynFnPtr> {
for m in MODIFIERS.iter() {
if requested == m.s {
return Some(m.fn_ptr);
};
}
None
}

0 comments on commit 4c099e6

Please sign in to comment.