Skip to content

Commit

Permalink
feat: Provide ass. functions on view for all actions
Browse files Browse the repository at this point in the history
Convenience... supposed to simplify the programmatic API
  • Loading branch information
alexpovel committed Oct 22, 2023
1 parent 6c5173c commit ca52905
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/actions/replace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ pub struct Replacement {
replacement: String,
}

impl Replacement {
#[must_use]
pub fn new(replacement: String) -> Self {
Self { replacement }
}
}

impl TryFrom<String> for Replacement {
type Error = String;

Expand Down
65 changes: 65 additions & 0 deletions src/scoping/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Items for defining the scope actions are applied within.
use crate::actions::{self, Action};

use self::literal::LiteralError;
use self::regex::RegexError;
use itertools::Itertools;
Expand Down Expand Up @@ -259,6 +261,69 @@ impl<'viewee> ScopedView<'viewee> {
}
}

/// Implementations of all available actions as dedicated methods.
///
/// Where actions don't take arguments, neither do the methods.
impl<'viewee> ScopedView<'viewee> {
pub fn map_action<A: Action>(&mut self, action: &A) -> &mut Self {
self.map(&|s| action.act(s))
}

pub fn delete(&mut self) -> &mut Self {
let action = actions::Deletion::default();

self.map_action(&action)
}

pub fn german(&mut self) -> &mut Self {
let action = actions::German::default();

self.map_action(&action)
}

pub fn lower(&mut self) -> &mut Self {
let action = actions::Lower::default();

self.map_action(&action)
}

pub fn normalize(&mut self) -> &mut Self {
let action = actions::Normalization::default();

self.map_action(&action)
}

pub fn replace(&mut self, replacement: String) -> &mut Self {
let action = actions::Replacement::new(replacement);

self.map_action(&action)
}

pub fn squeeze(&mut self) -> &mut Self {
let action = actions::Squeeze::default();

self.map_action(&action)
}

pub fn symbols(&mut self) -> &mut Self {
let action = actions::Symbols::default();

self.map_action(&action)
}

pub fn titlecase(&mut self) -> &mut Self {
let action = actions::Titlecase::default();

self.map_action(&action)
}

pub fn upper(&mut self) -> &mut Self {
let action = actions::Upper::default();

self.map_action(&action)
}
}

impl fmt::Display for ScopedView<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for scope in &self.scopes {
Expand Down

0 comments on commit ca52905

Please sign in to comment.