Skip to content

Commit

Permalink
feat: Lowercasing stage
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpovel committed Sep 17, 2023
1 parent 540b0b7 commit e0b097a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ regex = "1.9.5"

[features]
default = ["all"]
all = ["german", "symbols", "deletion", "squeeze", "upper"]
all = ["german", "symbols", "deletion", "squeeze", "upper", "lower"]
german = []
symbols = []
deletion = []
squeeze = []
upper = []
lower = []

[dev-dependencies]
assert_cmd = "2.0.12"
Expand Down
10 changes: 10 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use betterletters::apply;
use betterletters::stages::DeletionStage;
#[cfg(feature = "german")]
use betterletters::stages::GermanStage;
#[cfg(feature = "lower")]
use betterletters::stages::LowerStage;
#[cfg(feature = "squeeze")]
use betterletters::stages::SqueezeStage;
#[cfg(feature = "symbols")]
Expand Down Expand Up @@ -51,6 +53,11 @@ fn main() -> Result<(), Error> {
debug!("Loaded stage: Upper");
}

if args.lower {
stages.push(Box::<LowerStage>::default());
debug!("Loaded stage: Lower");
}

let mut source = BufReader::new(io::stdin());
let mut destination = io::stdout();

Expand Down Expand Up @@ -79,6 +86,9 @@ mod cli {
/// Uppercase what was matched
#[arg(short, long, env = "UPPER")]
pub upper: bool,
/// Lowercase what was matched
#[arg(short, long, env = "LOWER")]
pub lower: bool,
/// Perform substitutions on German words, such as 'Abenteuergruesse' to
/// 'Abenteuergrüße'
///
Expand Down
56 changes: 56 additions & 0 deletions src/stages/lower/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use crate::scoped::Scoped;

use super::Stage;

/// Renders in lowercase.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[allow(clippy::module_name_repetitions)]
pub struct LowerStage {}

impl Scoped for LowerStage {}

impl Stage for LowerStage {
fn substitute(&self, input: &str) -> String {
input.to_lowercase()
}
}

#[cfg(test)]
mod tests {
use rstest::rstest;

use super::*;

#[rstest]
// German
#[case("A", "a")]
#[case("a", "a")]
#[case("Ä", "ä")]
#[case("ä", "ä")]
#[case("Ö", "ö")]
#[case("ö", "ö")]
#[case("Ü", "ü")]
#[case("ü", "ü")]
#[case("ẞ", "ß")]
#[case("ß", "ß")]
#[case("AaÄäÖöÜüẞß!", "aaääööüüßß!")]
#[case("SS", "ss")]
//
// Chinese
#[case("你好!", "你好!")]
//
// Japanese
#[case("こんにちは!", "こんにちは!")]
//
// Korean
#[case("안녕하세요!", "안녕하세요!")]
//
// Russian
#[case("ПРИВЕТ!", "привет!")]
//
// Emojis
#[case("👋\0", "👋\0")]
fn substitute(#[case] input: &str, #[case] expected: &str) {
assert_eq!(LowerStage {}.substitute(input), expected);
}
}
3 changes: 3 additions & 0 deletions src/stages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
mod deletion;
#[cfg(feature = "german")]
mod german;
#[cfg(feature = "lower")]
mod lower;
#[cfg(feature = "squeeze")]
mod squeeze;
#[cfg(feature = "symbols")]
Expand All @@ -13,6 +15,7 @@ use std::fmt::Debug;

pub use deletion::DeletionStage;
pub use german::GermanStage;
pub use lower::LowerStage;
pub use squeeze::SqueezeStage;
pub use symbols::SymbolsStage;
pub use upper::UpperStage;
Expand Down

0 comments on commit e0b097a

Please sign in to comment.