use std::fmt::{Display, Formatter, Result as FmtResult};
#[derive(Clone)]
pub struct WildcardPattern {
prefix: String,
suffix: String,
}
impl WildcardPattern {
pub fn new(prefix: &str, suffix: &str) -> Self {
Self {
prefix: prefix.to_string(),
suffix: suffix.to_string(),
}
}
pub fn matches(&self, s: &str) -> bool {
s.starts_with(self.prefix.as_str()) && s.ends_with(self.suffix.as_str())
}
}
impl Display for WildcardPattern {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(f, "{}*{}", self.prefix, self.suffix)
}
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use super::*;
#[rstest]
#[case("cat", "dog", "catcatdog")]
#[case("Mem", "fault", "Memefault")]
#[case("Hello, ", "!", "Hello, Bob!")]
#[case("Zero Character", " Wildcard Match", "Zero Character Wildcard Match")]
fn test_matches(#[case] prefix: &str, #[case] suffix: &str, #[case] s: &str) {
assert!(WildcardPattern::new(prefix, suffix).matches(s))
}
#[rstest]
#[case("cat", "dog", "cacatdog")]
#[case("Mem", "fault", "MemFault")]
#[case("Hello, ", "!", "Hello! Bob!")]
fn test_nonmatches_do_not_match(#[case] prefix: &str, #[case] suffix: &str, #[case] s: &str) {
assert!(!WildcardPattern::new(prefix, suffix).matches(s))
}
}