Skip to content

Commit

Permalink
fix clippy errors and remove generics from pass creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Fogapod committed Feb 22, 2024
1 parent 9d28890 commit 4aad5e3
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 74 deletions.
4 changes: 2 additions & 2 deletions benches/accents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::fs;

pub fn read_accent(filename: &str) -> Accent {
let content = fs::read_to_string(filename).expect("reading accent definition");
ron::from_str::<Accent>(&content).expect(&format!("parsing accent {filename}"))
ron::from_str::<Accent>(&content).unwrap_or_else(|_| panic!("parsing accent {filename}"))
}

pub fn read_sample_file() -> String {
Expand Down Expand Up @@ -32,7 +32,7 @@ fn accents(c: &mut Criterion) {
g.bench_function(name, |b| {
b.iter(|| {
for line in &lines {
accent.say_it(&line, 0);
accent.say_it(line, 0);
}
})
});
Expand Down
2 changes: 1 addition & 1 deletion benches/literal_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn read_sample_words() -> Vec<String> {
// there are a lot of " :" lines for some reason. delete them
.filter(|&w| w != ":")
// remove direct speech colon: "Adam:" -> "Adam"
.map(|w| w.strip_suffix(":").unwrap_or(&w))
.map(|w| w.strip_suffix(':').unwrap_or(w))
.map(|w| w.to_owned())
.collect()
}
Expand Down
12 changes: 5 additions & 7 deletions src/accent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ impl fmt::Display for CreationError {
CreationError::FirstIntensityNotZero => {
write!(f, "first intensity must have level 0")
}
CreationError::UnsortedOrDuplicatedIntensities(level) => write!(
f,
"{}",
format!("duplicated or out of order intensity level {level}")
),
CreationError::UnsortedOrDuplicatedIntensities(level) => {
write!(f, "duplicated or out of order intensity level {level}")
}
}
}
}
Expand Down Expand Up @@ -102,8 +100,8 @@ mod tests {
vec![Pass::new(
"",
vec![
("(?-i)[a-z]".to_owned(), Literal::new_boxed("e")),
("[A-Z]".to_owned(), Literal::new_boxed("E")),
("(?-i)[a-z]".to_string(), Literal::new_boxed("e")),
("[A-Z]".to_string(), Literal::new_boxed("E")),
],
)
.unwrap()],
Expand Down
62 changes: 34 additions & 28 deletions src/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,9 @@ mod tests {
Intensity::new(
0,
vec![
Pass::new("words", vec![(r"\ba\b", Original::new_boxed())]).unwrap(),
Pass::new("patterns", vec![("1", Original::new_boxed())]).unwrap(),
Pass::new("words", vec![(r"\ba\b".to_string(), Original::new_boxed())])
.unwrap(),
Pass::new("patterns", vec![("1".to_string(), Original::new_boxed())]).unwrap(),
],
),
Intensity::new(
Expand All @@ -302,14 +303,17 @@ mod tests {
Pass::new(
"words",
vec![
(r"\ba\b", Original::new_boxed()),
(r"\bb\b", Original::new_boxed()),
(r"\ba\b".to_string(), Original::new_boxed()),
(r"\bb\b".to_string(), Original::new_boxed()),
],
)
.unwrap(),
Pass::new(
"patterns",
vec![("1", Original::new_boxed()), ("2", Original::new_boxed())],
vec![
("1".to_string(), Original::new_boxed()),
("2".to_string(), Original::new_boxed()),
],
)
.unwrap(),
],
Expand Down Expand Up @@ -356,15 +360,17 @@ mod tests {
Intensity::new(
0,
vec![
Pass::new("words", vec![(r"\ba\b", Original::new_boxed())]).unwrap(),
Pass::new("patterns", vec![("1", Original::new_boxed())]).unwrap(),
Pass::new("words", vec![(r"\ba\b".to_string(), Original::new_boxed())])
.unwrap(),
Pass::new("patterns", vec![("1".to_string(), Original::new_boxed())]).unwrap(),
],
),
Intensity::new(
1,
vec![
Pass::new("words", vec![(r"\bb\b", Original::new_boxed())]).unwrap(),
Pass::new("patterns", vec![("2", Original::new_boxed())]).unwrap(),
Pass::new("words", vec![(r"\bb\b".to_string(), Original::new_boxed())])
.unwrap(),
Pass::new("patterns", vec![("2".to_string(), Original::new_boxed())]).unwrap(),
],
),
];
Expand Down Expand Up @@ -493,20 +499,20 @@ mod tests {
"words",
vec![
(
r"\btest\b",
r"\btest\b".to_string(),
Literal::new_boxed("Testing in progress; Please ignore ..."),
),
(r"\bbadword\b", Literal::new_boxed("")),
(r"\bdupe\b", Literal::new_boxed("0")),
(r"\bbadword\b".to_string(), Literal::new_boxed("")),
(r"\bdupe\b".to_string(), Literal::new_boxed("0")),
],
)
.unwrap(),
Pass::new(
"patterns",
vec![
(r"[a-z]", Literal::new_boxed("e")),
(r"[a-z]".to_string(), Literal::new_boxed("e")),
(
r"[A-Z]",
r"[A-Z]".to_string(),
Weights::new_boxed(vec![
(5, Literal::new_boxed("E")),
(1, Literal::new_boxed("Ē")),
Expand All @@ -518,7 +524,7 @@ mod tests {
.unwrap(),
),
(
r"[0-9]",
r"[0-9]".to_string(),
Any::new_boxed(vec![Weights::new_boxed(vec![(
1,
Any::new_boxed(vec![
Expand All @@ -542,17 +548,17 @@ mod tests {
Pass::new(
"words",
vec![
(r"\breplaced\b", Literal::new_boxed("words")),
(r"\bdupe\b", Literal::new_boxed("1")),
(r"\bWindows\b", Literal::new_boxed("Linux")),
(r"\breplaced\b".to_string(), Literal::new_boxed("words")),
(r"\bdupe\b".to_string(), Literal::new_boxed("1")),
(r"\bWindows\b".to_string(), Literal::new_boxed("Linux")),
],
)
.unwrap(),
Pass::new(
"patterns",
vec![
(r"a+", Literal::new_boxed("multiple A's")),
(r"^", Literal::new_boxed("start")),
(r"a+".to_string(), Literal::new_boxed("multiple A's")),
(r"^".to_string(), Literal::new_boxed("start")),
],
)
.unwrap(),
Expand All @@ -564,20 +570,20 @@ mod tests {
Pass::new(
"words",
vec![
(r"\breplaced\b", Literal::new_boxed("words")),
(r"\bdupe\b", Literal::new_boxed("2")),
(r"\bWindows\b", Literal::new_boxed("Linux")),
(r"\badded\b", Literal::new_boxed("words")),
(r"\breplaced\b".to_string(), Literal::new_boxed("words")),
(r"\bdupe\b".to_string(), Literal::new_boxed("2")),
(r"\bWindows\b".to_string(), Literal::new_boxed("Linux")),
(r"\badded\b".to_string(), Literal::new_boxed("words")),
],
)
.unwrap(),
Pass::new(
"patterns",
vec![
(r"a+", Literal::new_boxed("multiple A's")),
(r"^", Literal::new_boxed("start")),
(r"b+", Literal::new_boxed("multiple B's")),
(r"$", Literal::new_boxed("end")),
(r"a+".to_string(), Literal::new_boxed("multiple A's")),
(r"^".to_string(), Literal::new_boxed("start")),
(r"b+".to_string(), Literal::new_boxed("multiple B's")),
(r"$".to_string(), Literal::new_boxed("end")),
],
)
.unwrap(),
Expand Down
32 changes: 19 additions & 13 deletions src/intensity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ impl Intensity {
}

/// Produces new instance by extending inner passes
#[allow(clippy::result_large_err)]
pub fn extend(&self, level: u64, passes: Vec<Pass>) -> Result<Self, pass::CreationError> {
let mut existing_passes = self.passes.clone();
let mut appended_passes = Vec::new();
Expand Down Expand Up @@ -59,22 +60,22 @@ mod tests {
let old_pass = Pass::new(
"",
vec![
("old", Literal::new_boxed("old")),
("old2", Literal::new_boxed("old2")),
("old".to_string(), Literal::new_boxed("old")),
("old2".to_string(), Literal::new_boxed("old2")),
],
)
.unwrap();

let new_pass = Pass::new("", vec![("old", Literal::new_boxed("new"))]).unwrap();
let new_pass = Pass::new("", vec![("old".to_string(), Literal::new_boxed("new"))]).unwrap();

let old = Intensity::new(0, vec![old_pass]);

let extended = old.extend(1, vec![new_pass]).unwrap();
let expected = Pass::new(
"",
vec![
("old", Literal::new_boxed("new")),
("old2", Literal::new_boxed("old2")),
("old".to_string(), Literal::new_boxed("new")),
("old2".to_string(), Literal::new_boxed("old2")),
],
)
.unwrap();
Expand All @@ -85,17 +86,22 @@ mod tests {

#[test]
fn rules_appended() {
let old_pass = Pass::new("", vec![("existing", Literal::new_boxed("old"))]).unwrap();
let new_pass = Pass::new("", vec![("added", Literal::new_boxed("new"))]).unwrap();
let old_pass = Pass::new(
"",
vec![("existing".to_string(), Literal::new_boxed("old"))],
)
.unwrap();
let new_pass =
Pass::new("", vec![("added".to_string(), Literal::new_boxed("new"))]).unwrap();

let old = Intensity::new(0, vec![old_pass]);

let extended = old.extend(1, vec![new_pass]).unwrap();
let expected = Pass::new(
"",
vec![
("existing", Literal::new_boxed("old")),
("added", Literal::new_boxed("new")),
("existing".to_string(), Literal::new_boxed("old")),
("added".to_string(), Literal::new_boxed("new")),
],
)
.unwrap();
Expand All @@ -105,15 +111,15 @@ mod tests {

#[test]
fn passes_appended() {
let old_pass = Pass::new("old", Vec::<(&str, _)>::new()).unwrap();
let new_pass = Pass::new("new", Vec::<(&str, _)>::new()).unwrap();
let old_pass = Pass::new("old", Vec::new()).unwrap();
let new_pass = Pass::new("new", Vec::new()).unwrap();

let old = Intensity::new(0, vec![old_pass]);

let extended = old.extend(1, vec![new_pass]).unwrap();
let expected = vec![
Pass::new("old", Vec::<(&str, _)>::new()).unwrap(),
Pass::new("new", Vec::<(&str, _)>::new()).unwrap(),
Pass::new("old", Vec::new()).unwrap(),
Pass::new("new", Vec::new()).unwrap(),
];

assert_eq!(extended.passes, expected);
Expand Down
38 changes: 19 additions & 19 deletions src/pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,11 @@ impl fmt::Display for CreationError {
impl Error for CreationError {}

impl Pass {
pub fn new<T: AsRef<str>>(
name: &str,
rules: Vec<(T, Box<dyn Tag>)>,
) -> Result<Self, CreationError> {
#[allow(clippy::result_large_err)]
pub fn new(name: &str, rules: Vec<(String, Box<dyn Tag>)>) -> Result<Self, CreationError> {
let (patterns, tags): (Vec<_>, Vec<_>) = rules.into_iter().unzip();

let patterns: Vec<_> = patterns
.into_iter()
.map(|s| s.as_ref().to_owned())
.collect();
let patterns: Vec<_> = patterns.into_iter().map(|s| s.to_string()).collect();

let multi_regex = Regex::builder()
.syntax(
Expand All @@ -65,7 +60,7 @@ impl Pass {
.case_insensitive(true),
)
.build_many(&patterns)
.map_err(|err| CreationError::BadRegex(err))?;
.map_err(CreationError::BadRegex)?;

Ok(Self {
name: name.to_owned(),
Expand All @@ -75,6 +70,7 @@ impl Pass {
})
}

#[allow(clippy::result_large_err)]
pub fn extend(&self, other: Pass) -> Result<Self, CreationError> {
let mut existing_rules: Vec<_> = self
.regexes
Expand All @@ -89,7 +85,7 @@ impl Pass {
let mut replaced = false;

for (existing_regex, existing_tag) in &mut existing_rules {
if &new_regex == existing_regex {
if new_regex == **existing_regex {
// FIXME: remove clone
*existing_tag = new_tag.clone();
replaced = true;
Expand Down Expand Up @@ -153,20 +149,20 @@ mod tests {
let old = Pass::new(
"",
vec![
("old", Literal::new_boxed("old")),
("old2", Literal::new_boxed("old2")),
("old".to_string(), Literal::new_boxed("old")),
("old2".to_string(), Literal::new_boxed("old2")),
],
)
.unwrap();

let new = Pass::new("", vec![("old", Literal::new_boxed("new"))]).unwrap();
let new = Pass::new("", vec![("old".to_string(), Literal::new_boxed("new"))]).unwrap();

let extended = old.extend(new).unwrap();
let expected = Pass::new(
"",
vec![
("old", Literal::new_boxed("new")),
("old2", Literal::new_boxed("old2")),
("old".to_string(), Literal::new_boxed("new")),
("old2".to_string(), Literal::new_boxed("old2")),
],
)
.unwrap();
Expand All @@ -176,15 +172,19 @@ mod tests {

#[test]
fn rules_appended() {
let old = Pass::new("", vec![("existing", Literal::new_boxed("old"))]).unwrap();
let new = Pass::new("", vec![("added", Literal::new_boxed("new"))]).unwrap();
let old = Pass::new(
"",
vec![("existing".to_string(), Literal::new_boxed("old"))],
)
.unwrap();
let new = Pass::new("", vec![("added".to_string(), Literal::new_boxed("new"))]).unwrap();

let extended = old.extend(new).unwrap();
let expected = Pass::new(
"",
vec![
("existing", Literal::new_boxed("old")),
("added", Literal::new_boxed("new")),
("existing".to_string(), Literal::new_boxed("old")),
("added".to_string(), Literal::new_boxed("new")),
],
)
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions tests/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use utils::read_sample_file_lines;
pub fn read_accent(filename: PathBuf) -> Accent {
let content = fs::read_to_string(&filename).unwrap();
serde_json::from_str::<Accent>(&content)
.expect(&format!("parsing accent {}", filename.display()))
.unwrap_or_else(|_| panic!("parsing accent {}", filename.display()))
}

// flatten breaks treating string map keys as u64 (ints are not allowed as json map keys)
Expand Down Expand Up @@ -62,7 +62,7 @@ fn json_accents_work() {
let accent = read_accent(path);
for line in &lines {
for intensity in accent.intensities() {
accent.say_it(&line, intensity);
accent.say_it(line, intensity);
}
}
tested_at_least_one = true;
Expand Down
Loading

0 comments on commit 4aad5e3

Please sign in to comment.