Skip to main content

stylish_stringlike/text/
expandable.rs

1use regex::Captures;
2
3/// Expanding regex captures in text objects.
4pub trait Expandable {
5    /// Returns self with the desired capture group expanded into self.
6    /// For [`String`], this is just a wrapper around [`Captures::expand`].
7    ///
8    /// # Example
9    /// ```rust
10    /// use regex::Regex;
11    /// use stylish_stringlike::text::Expandable;
12    /// let re = Regex::new(r"(\d{3})[-. ,](\d{3})[-. ,](\d{4})").unwrap();
13    /// let captures = re.captures("555,123 4567").unwrap();
14    /// let target = String::from("($1) $2-$3");
15    /// assert_eq!(target.expand(&captures), String::from("(555) 123-4567"))
16    /// ```
17    fn expand(&self, capture: &Captures) -> Self;
18}
19
20impl Expandable for String {
21    fn expand(&self, capture: &Captures) -> String {
22        let mut dest = String::new();
23        capture.expand(self, &mut dest);
24        dest
25    }
26}