From ca529057fef2e50db49148b6914eb292ee9ac755 Mon Sep 17 00:00:00 2001 From: Alex Povel Date: Sun, 22 Oct 2023 20:54:27 +0200 Subject: [PATCH] feat: Provide ass. functions on view for all actions Convenience... supposed to simplify the programmatic API --- src/actions/replace/mod.rs | 7 ++++ src/scoping/mod.rs | 65 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/src/actions/replace/mod.rs b/src/actions/replace/mod.rs index fa443961..c68fd86e 100644 --- a/src/actions/replace/mod.rs +++ b/src/actions/replace/mod.rs @@ -50,6 +50,13 @@ pub struct Replacement { replacement: String, } +impl Replacement { + #[must_use] + pub fn new(replacement: String) -> Self { + Self { replacement } + } +} + impl TryFrom for Replacement { type Error = String; diff --git a/src/scoping/mod.rs b/src/scoping/mod.rs index 5ccefe83..590da693 100644 --- a/src/scoping/mod.rs +++ b/src/scoping/mod.rs @@ -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; @@ -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(&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 {