Skip to content

Commit

Permalink
[Enhancement] uncrlf: Outsource Logic to Dedicated Struct (#1015)
Browse files Browse the repository at this point in the history
* Added ::= Cargo.toml:  declare new feature ``uncrlf`` as part of ``utilities``

* Changed ::= uncrlf:  outsource logic to dedicated struct

* Added ::= Uncrlf

* Skip ::= adjust application behaviour

* Added ::= Just:  CI step for feature ``uncrlf``

* Skip ::= prepare tests

* Added ::= uncrlf:  unit tests

* Skip ::= prepare bugfix

* Fixed ::= uncrlf:  removing all `\r` occurences

* Create summary of recent changes

* Skip ::= fix typo

---------

Co-authored-by: GitHub Actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
kevinmatthes and github-actions[bot] committed Jan 2, 2024
1 parent 5f3d4c0 commit 2fdaae4
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 28 deletions.
1 change: 1 addition & 0 deletions .justfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ ci:
just feature cff-create
just feature mkcws
just feature rs2md
just feature uncrlf
just feature utilities
just features

Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ cff-create = []
default = ["cff", "utilities"]
mkcws = []
rs2md = []
utilities = ["mkcws", "rs2md"]
uncrlf = []
utilities = ["mkcws", "rs2md", "uncrlf"]

[package]
authors = ["Kevin Matthes <aeruginous.rs@gmail.com>"]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(
references: {},
changes: {
"Added": [
"Cargo.toml: declare new feature ``uncrlf`` as part of ``utilities``",
"Just: CI step for feature ``uncrlf``",
"Uncrlf",
"uncrlf: unit tests",
],
"Changed": [
"uncrlf: outsource logic to dedicated struct",
],
"Fixed": [
"uncrlf: removing all `\\r` occurrences",
],
},
)
33 changes: 6 additions & 27 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
| |
\******************************************************************************/

use crate::{AppendAsLine, PatternIOProcessor, PatternWriter, Prefer};
use crate::{AppendAsLine, PatternWriter};
use clap::{Parser, Subcommand};
use std::{io::BufRead, path::PathBuf};
use sysexits::Result;
Expand Down Expand Up @@ -81,27 +81,16 @@ pub enum Action {
Rs2md(crate::Rs2md),

/// Remove CRLFs from the given file.
Uncrlf {
/// The file to edit; overrides `input_file` and `output_file`.
#[arg(long = "edit", short = 'e')]
file_to_edit: Option<PathBuf>,

/// The file to read from, defaulting to [`std::io::Stdin`], if omitted.
#[arg(long = "input", short)]
input_file: Option<PathBuf>,

/// The file to write to, defaulting to [`std::io::Stdout`], if omitted.
#[arg(long = "output", short)]
output_file: Option<PathBuf>,
},
#[cfg(feature = "uncrlf")]
Uncrlf(crate::Uncrlf),
}

impl Action {
/// Execute the selected action.
///
/// # Errors
///
/// See [`PatternIOProcessor::io`].
/// See [`crate::PatternIOProcessor::io`].
pub fn run(&self) -> Result<()> {
match self {
#[cfg(feature = "cff-create")]
Expand Down Expand Up @@ -149,18 +138,8 @@ impl Action {
Self::Ronlog(r) => r.main(),
#[cfg(feature = "rs2md")]
Self::Rs2md(r) => r.main(),
Self::Uncrlf {
file_to_edit,
input_file,
output_file,
} => |mut s: String| -> String {
s.retain(|c| c != '\r');
s
}
.io(
input_file.prefer(file_to_edit.clone()),
output_file.prefer(file_to_edit.clone()),
),
#[cfg(feature = "uncrlf")]
Self::Uncrlf(u) => u.main(),
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,9 @@ pub use crate::utilities::Mkcws;
#[cfg(feature = "rs2md")]
pub use crate::utilities::Rs2md;

#[cfg(feature = "uncrlf")]
pub use crate::utilities::Uncrlf;

/// This crate's name.
pub const NAME: &str = "aeruginous";

Expand Down
52 changes: 52 additions & 0 deletions src/utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,56 @@ impl Rs2md {
}
}

/// Remove CRLFs from the given file.
#[cfg(feature = "uncrlf")]
#[derive(clap::Parser, Clone)]
pub struct Uncrlf {
/// The file to edit; overrides `input_file` and `output_file`.
#[arg(long = "edit", short = 'e')]
file_to_edit: Option<std::path::PathBuf>,

/// The file to read from, defaulting to [`std::io::Stdin`], if omitted.
#[arg(long = "input", short)]
input_file: Option<std::path::PathBuf>,

/// The file to write to, defaulting to [`std::io::Stdout`], if omitted.
#[arg(long = "output", short)]
output_file: Option<std::path::PathBuf>,
}

#[cfg(feature = "uncrlf")]
impl Uncrlf {
/// Remove CRLFs from the given file.
///
/// # Errors
///
/// See
///
/// - [`crate::PatternIOProcessor::io`]
pub fn main(&self) -> sysexits::Result<()> {
use crate::{PatternIOProcessor, Prefer};

(|s: String| s.replace("\r\n", "\n")).io(
self.input_file.prefer(self.file_to_edit.clone()),
self.output_file.prefer(self.file_to_edit.clone()),
)
}

/// Create a new instance.
pub fn new<T>(
input_file: Option<T>,
output_file: Option<T>,
file_to_edit: Option<T>,
) -> Self
where
std::path::PathBuf: From<T>,
{
Self {
file_to_edit: file_to_edit.map(Into::into),
input_file: input_file.map(Into::into),
output_file: output_file.map(Into::into),
}
}
}

/******************************************************************************/
17 changes: 17 additions & 0 deletions tests/utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,21 @@ fn mkcws() {
std::fs::remove_file("cwd.code-workspace").unwrap();
}

#[cfg(feature = "uncrlf")]
#[test]
fn uncrlf() {
use aeruginous::{PatternWriter, ReadFile};

"uncrlf.txt"
.truncate(Box::new("TEST\r\nTest\n\rtest\r\n".to_string()))
.unwrap();

assert!(aeruginous::Uncrlf::new(None, None, Some("uncrlf.txt"))
.main()
.is_ok());

assert_eq!("uncrlf.txt".read().unwrap(), "TEST\nTest\n\rtest\n");
std::fs::remove_file("uncrlf.txt").unwrap();
}

/******************************************************************************/

0 comments on commit 2fdaae4

Please sign in to comment.