Skip to content

Commit

Permalink
add delete tag
Browse files Browse the repository at this point in the history
  • Loading branch information
Fogapod committed Feb 24, 2024
1 parent 652cac5 commit 69e44a1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
//! Default replacments from [`tag_impls`] are:
//!
//! * [`Original`] does not replace (leaves original match as is)
//! * [`Delete`] deletes match
//! * [`Literal`] puts given string
//! * [`Any`] selects random inner tag with equal weights
//! * [`Weights`] selects random inner tag based on relative weights
Expand Down Expand Up @@ -115,6 +116,7 @@
//! [`Match`]: crate::Match
//! [`Intensity`]: crate::intensity::Intensity
//! [`Original`]: crate::tag_impls::Original
//! [`Delete`]: crate::tag_impls::Delete
//! [`Literal`]: crate::tag_impls::Literal
//! [`Any`]: crate::tag_impls::Any
//! [`Weights`]: crate::tag_impls::Weights
Expand Down
33 changes: 33 additions & 0 deletions src/tag_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,31 @@ impl Tag for Original {
}
}

/// Deletes match
///
/// This is a shortcut for [`Literal`] with `""`
#[derive(Clone, Debug)]
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
pub struct Delete;

impl Delete {
#![allow(clippy::new_without_default)]
pub fn new() -> Self {
Self
}

pub fn new_boxed() -> Box<Self> {
Box::new(Self::new())
}
}

#[cfg_attr(feature = "deserialize", typetag::deserialize)]
impl Tag for Delete {
fn generate<'a>(&self, _: &Match<'a>) -> Cow<'a, str> {
Cow::Borrowed("")
}
}

/// Static string
///
/// Acts as regex template, syntax doc: <https://docs.rs/regex/latest/regex/struct.Regex.html#example-9>
Expand Down Expand Up @@ -310,6 +335,14 @@ mod tests {
assert_eq!(apply(&tag, "foo"), "foo");
}

#[test]
fn delete() {
let tag = Delete::new();

assert_eq!(apply(&tag, "bar"), "");
assert_eq!(apply(&tag, "foo"), "");
}

#[test]
fn literal() {
let tag = Literal::new("bar");
Expand Down

0 comments on commit 69e44a1

Please sign in to comment.