Created
January 16, 2020 02:58
-
-
Save rust-play/f71c496f7adaeff618a510f16a074f3e to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(dead_code)] | |
use std::fmt::Debug; | |
#[derive(Debug)] | |
struct Matchbook { | |
matchers: Vec<Box<dyn Matcher>>, | |
} | |
impl Matchbook { | |
pub fn new() -> Matchbook { | |
let matchers = vec![]; | |
Matchbook { matchers } | |
} | |
fn add<T: Matcher + 'static>(&mut self, arg: T) -> &mut Matchbook { | |
self.matchers.push(Box::new(arg)); | |
self | |
} | |
pub fn string(&mut self, arg: &'static str) -> &mut Matchbook { | |
self.add(arg) | |
} | |
pub fn number(&mut self, arg: usize) -> &mut Matchbook { | |
self.add(arg) | |
} | |
} | |
trait Matcher: Debug { | |
fn is_match(&self, arg: &'static str) -> bool; | |
fn is_exact_match(&self, arg: &'static str) -> bool; | |
fn is_not_match(&self, arg: &'static str) -> bool { | |
!self.is_match(arg) | |
} | |
fn is_not_exact_match(&self, arg: &'static str) -> bool { | |
!self.is_exact_match(arg) | |
} | |
} | |
impl Matcher for &str { | |
fn is_match(&self, arg: &'static str) -> bool { | |
arg.contains(self) | |
} | |
fn is_exact_match(&self, arg: &'static str) -> bool { | |
arg == *self | |
} | |
} | |
impl Matcher for usize { | |
fn is_match(&self, arg: &'static str) -> bool { | |
arg.contains(&self.to_string()) | |
} | |
fn is_exact_match(&self, arg: &'static str) -> bool { | |
arg == self.to_string() | |
} | |
} | |
fn main() { | |
let mut book = Matchbook::new(); | |
book.number(123); | |
book.string("456"); | |
} | |
#[cfg(test)] | |
mod test { | |
use super::*; | |
#[test] | |
fn new_matchbook_without_matchers() { | |
let book = Matchbook::new(); | |
assert_eq!(book.matchers.len(), 0); | |
} | |
#[test] | |
fn can_add_matcher_to_matchbook() { | |
let mut book = Matchbook::new(); | |
assert_eq!(book.matchers.len(), 0); | |
book.string("foo"); | |
assert_eq!(book.matchers.len(), 1); | |
book.number(123); | |
assert_eq!(book.matchers.len(), 2); | |
} | |
#[test] | |
fn str_is_match() { | |
assert!("foo".is_match("foobar")); | |
assert!("foo".is_match("barfoo")); | |
assert!("foo".is_match("foo")); | |
} | |
#[test] | |
fn str_is_not_match() { | |
assert!("foo".is_not_match("farboo")); | |
assert!("foo".is_not_match("boofar")); | |
} | |
#[test] | |
fn str_is_exact_match() { | |
assert!("foo".is_exact_match("foo")); | |
assert!("hello world".is_exact_match("hello world")); | |
} | |
#[test] | |
fn str_is_not_exact_match() { | |
assert!("foo".is_not_exact_match("foobar")); | |
assert!("foo".is_not_exact_match("barfoo")); | |
} | |
#[test] | |
fn usize_is_match() { | |
assert!(123.is_match("123456")); | |
assert!(456.is_match("123456")); | |
assert!(123.is_match("-123")); | |
assert!(456.is_match("123456789")); | |
} | |
#[test] | |
fn usize_is_not_match() { | |
assert!(123.is_not_match("456789")); | |
assert!(123.is_not_match("1.23")); | |
} | |
#[test] | |
fn usize_is_exact_match() { | |
assert!(123.is_exact_match("123")); | |
assert!((5 + 5).is_exact_match("10")); | |
} | |
#[test] | |
fn usize_is_not_exact_match() { | |
assert!(123.is_not_exact_match("123456")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment