Skip to content

Commit

Permalink
feat: squeeze stage, Scoped concept
Browse files Browse the repository at this point in the history
Also a bunch of tests, fixing CLI tests, cleaning up...

A huge commit...
  • Loading branch information
alexpovel committed Sep 16, 2023
1 parent db4990f commit 4c18820
Show file tree
Hide file tree
Showing 41 changed files with 813 additions and 440 deletions.
77 changes: 77 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ regex = "1.9.5"

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

[dev-dependencies]
assert_cmd = "2.0.12"
Expand All @@ -45,6 +46,9 @@ rstest = "0.18.2"
serde = { version = "1.0.188", features = ["derive"] }
glob = "0.3.1"
num_cpus = "1.16.0"
rand = "0.8.5"
rand_regex = "0.16.0"
test-log = "0.2.12"

[profile.dev.package.insta]
# https://insta.rs/docs/quickstart/#optional-faster-runs
Expand Down
13 changes: 9 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
pub use crate::stages::Stage;
use log::{debug, info};
use scoped::Scope;
use std::io::{BufRead, Error, Write};

/// Internal macros. Have to live here to be usable in unit, not just integration
Expand All @@ -26,9 +27,13 @@ use std::io::{BufRead, Error, Write};
#[macro_use]
pub mod macros;

/// Items related to scopes, which are used to limit the application of stages.
pub mod scoped;
/// Main components around [`Stage`]s and their [processing][Stage::substitute].
pub mod stages;

/// Pattern signalling global scope, aka matching entire inputs.
pub const GLOBAL_SCOPE: &str = r".*";
const EXPECTABLE_AVERAGE_WORD_LENGTH_BYTES: u8 = 16;
const EXPECTABLE_AVERAGE_MATCHES_PER_WORD: u8 = 2;

Expand All @@ -45,15 +50,15 @@ const EXPECTABLE_AVERAGE_MATCHES_PER_WORD: u8 = 2;
///
///
/// ```
/// use betterletters::{apply, stages::GermanStage, Stage};
/// use betterletters::{apply, scoped::Scope, stages::GermanStage, Stage};
/// use std::io::Cursor;
///
/// let stages = vec![Box::new(GermanStage::default())].into_iter().map(|g| g as Box<dyn Stage>).collect();
///
/// let mut input = Cursor::new("Gruess Gott!\n");
/// let mut output: Vec<u8> = Vec::new();
///
/// apply(&stages, &mut input, &mut output);
/// apply(&stages, &Scope::default(), &mut input, &mut output);
///
/// assert_eq!(output, "Grüß Gott!\n".as_bytes());
/// ```
Expand All @@ -68,6 +73,7 @@ const EXPECTABLE_AVERAGE_MATCHES_PER_WORD: u8 = 2;
/// - when the destination cannot be flushed before exiting
pub fn apply(
stages: &Vec<Box<dyn Stage>>,
scope: &Scope,
source: &mut impl BufRead,
destination: &mut impl Write,
) -> Result<(), Error> {
Expand All @@ -79,8 +85,7 @@ pub fn apply(
debug!("Starting processing line: '{}'", buf.escape_debug());

for stage in stages {
let result = stage.substitute(&buf)?;
buf = result.into();
buf = stage.apply(&buf, scope);
}

debug!("Processed line, will write out: '{}'", buf.escape_debug());
Expand Down
Loading

0 comments on commit 4c18820

Please sign in to comment.