summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main.rs68
1 files changed, 54 insertions, 14 deletions
diff --git a/src/main.rs b/src/main.rs
index 0c994ef..2518e02 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -23,7 +23,7 @@ const EXCEPTIONS: &[&str] = &[
fn main() {
let mut args = args();
args.next(); // ignore self arg
- let mut ignorables = Vec::new();
+ let mut ignorables = HashSet::new();
while let Some(arg) = args.next() {
match arg.as_str() {
@@ -33,6 +33,7 @@ fn main() {
}
"--help" | "-h" => {
println!("{}", HELP_MSG);
+ return;
}
unrecognized => {
panic!(format!("Unrecognized option: {}", unrecognized));
@@ -51,7 +52,11 @@ fn main() {
}
/// Corrects a single line (title) based on placement and exceptions.
-fn correct_line(line: &str, ignorables: &[String], exceptions: &HashSet<&'static str>) -> String {
+fn correct_line(
+ line: &str,
+ ignorables: &HashSet<String>,
+ exceptions: &HashSet<&'static str>,
+) -> String {
let mut line = line.split_whitespace().peekable();
let mut result = String::new();
@@ -81,7 +86,7 @@ fn correct_line(line: &str, ignorables: &[String], exceptions: &HashSet<&'static
fn correct_word(
word: &str,
first_or_last: bool,
- ignorables: &[String],
+ ignorables: &HashSet<String>,
exceptions: &HashSet<&'static str>,
) -> String {
if ignorables.iter().any(|s| s == word) {
@@ -99,8 +104,14 @@ fn correct_word(
/// Capitalizes the first letter of a single word.
fn capitalize_word(word: &str) -> String {
let mut chars = word.chars();
- let first = chars.next().unwrap_or_default();
- first.to_uppercase().chain(chars).collect()
+
+ if let Some(first) = chars.next() {
+ // uppercase first character and append the rest
+ first.to_uppercase().chain(chars).collect()
+ } else {
+ // handle empty words
+ String::new()
+ }
}
#[cfg(test)]
@@ -116,23 +127,43 @@ mod tests {
#[test]
fn test_correct_word() {
assert_eq!(
- correct_word("Like", false, &[], &EXCEPTIONS.iter().cloned().collect()),
+ correct_word(
+ "Like",
+ false,
+ &HashSet::new(),
+ &EXCEPTIONS.iter().cloned().collect()
+ ),
String::from("like")
);
assert_eq!(
- correct_word("like", false, &[], &EXCEPTIONS.iter().cloned().collect()),
+ correct_word(
+ "like",
+ false,
+ &HashSet::new(),
+ &EXCEPTIONS.iter().cloned().collect()
+ ),
String::from("like")
);
assert_eq!(
- correct_word("like", true, &[], &EXCEPTIONS.iter().cloned().collect()),
+ correct_word(
+ "like",
+ true,
+ &HashSet::new(),
+ &EXCEPTIONS.iter().cloned().collect()
+ ),
String::from("Like")
);
assert_eq!(
- correct_word("liKe", false, &[], &EXCEPTIONS.iter().cloned().collect()),
+ correct_word(
+ "liKe",
+ false,
+ &HashSet::new(),
+ &EXCEPTIONS.iter().cloned().collect()
+ ),
String::from("like")
);
assert_eq!(
- correct_word("computers", false, &[], &HashSet::new()),
+ correct_word("computers", false, &HashSet::new(), &HashSet::new()),
String::from("Computers")
);
}
@@ -140,13 +171,17 @@ mod tests {
#[test]
fn test_correct_line() {
assert_eq!(
- correct_line("this is a test", &[], &EXCEPTIONS.iter().cloned().collect()),
+ correct_line(
+ "this is a test",
+ &HashSet::new(),
+ &EXCEPTIONS.iter().cloned().collect()
+ ),
String::from("This Is a Test")
);
assert_eq!(
correct_line(
"similEs: lIke Or As",
- &[],
+ &HashSet::new(),
&EXCEPTIONS.iter().cloned().collect()
),
String::from("Similes: like or As")
@@ -155,14 +190,19 @@ mod tests {
#[test]
fn test_proper_nouns_acronyms() {
+ let mut ignorables = HashSet::new();
+ ignorables.insert(String::from("FreeBSD"));
assert_eq!(
- correct_line("FreeBSD", &[String::from("FreeBSD")], &HashSet::new()),
+ correct_line("FreeBSD", &ignorables, &HashSet::new()),
String::from("FreeBSD")
);
}
#[test]
fn test_empty_lines() {
- assert_eq!(correct_line("", &[], &HashSet::new()), String::from(""),);
+ assert_eq!(
+ correct_line("", &HashSet::new(), &HashSet::new()),
+ String::from(""),
+ );
}
}