hamcrest2/matchers/
any.rs

1// Copyright 2017 Flier Lu
2// Copyright 2018 Val Markovic
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10use std::fmt::{self, Display};
11use std::marker::PhantomData;
12
13use crate::core::*;
14
15pub struct Any<T, M>(M, PhantomData<T>);
16
17pub fn any<T, M>(matchers: M) -> Any<T, M> {
18  Any(matchers, PhantomData)
19}
20
21#[macro_export]
22macro_rules! any {
23    ($( $arg:expr ),*) => ($crate::matchers::any::any(($( $arg ),*)))
24}
25
26#[macro_export]
27#[deprecated(since = "0.2.0", note = "Use any!() instead")]
28macro_rules! any_of {
29    ($( $arg:expr ),*) => ($crate::matchers::any::any(($( $arg ),*)))
30}
31
32impl<T, M0, M1> Display for Any<T, (M0, M1)>
33where
34  M0: Display,
35  M1: Display,
36{
37  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
38    let (ref m0, ref m1) = self.0;
39
40    write!(f, "any of ({}, {})", m0, m1)
41  }
42}
43
44impl<T, M0, M1> Matcher<T> for Any<T, (M0, M1)>
45where
46  T: Clone,
47  M0: Matcher<T>,
48  M1: Matcher<T>,
49{
50  fn matches(&self, actual: T) -> MatchResult {
51    let (ref m0, ref m1) = self.0;
52
53    m0.matches(actual.clone())
54      .or_else(|_| m1.matches(actual.clone()))
55  }
56}
57
58impl<T, M0, M1, M2> Display for Any<T, (M0, M1, M2)>
59where
60  M0: Display,
61  M1: Display,
62  M2: Display,
63{
64  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
65    let (ref m0, ref m1, ref m2) = self.0;
66
67    write!(f, "any of ({}, {}, {})", m0, m1, m2)
68  }
69}
70
71impl<T, M0, M1, M2> Matcher<T> for Any<T, (M0, M1, M2)>
72where
73  T: Clone,
74  M0: Matcher<T>,
75  M1: Matcher<T>,
76  M2: Matcher<T>,
77{
78  fn matches(&self, actual: T) -> MatchResult {
79    let (ref m0, ref m1, ref m2) = self.0;
80
81    m0.matches(actual.clone())
82      .or_else(|_| m1.matches(actual.clone()))
83      .or_else(|_| m2.matches(actual.clone()))
84  }
85}
86
87impl<T, M0, M1, M2, M3> Display for Any<T, (M0, M1, M2, M3)>
88where
89  M0: Display,
90  M1: Display,
91  M2: Display,
92  M3: Display,
93{
94  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
95    let (ref m0, ref m1, ref m2, ref m3) = self.0;
96
97    write!(f, "any of ({}, {}, {}, {})", m0, m1, m2, m3)
98  }
99}
100
101impl<T, M0, M1, M2, M3> Matcher<T> for Any<T, (M0, M1, M2, M3)>
102where
103  T: Clone,
104  M0: Matcher<T>,
105  M1: Matcher<T>,
106  M2: Matcher<T>,
107  M3: Matcher<T>,
108{
109  fn matches(&self, actual: T) -> MatchResult {
110    let (ref m0, ref m1, ref m2, ref m3) = self.0;
111
112    m0.matches(actual.clone())
113      .or_else(|_| m1.matches(actual.clone()))
114      .or_else(|_| m2.matches(actual.clone()))
115      .or_else(|_| m3.matches(actual.clone()))
116  }
117}
118
119impl<T, M0, M1, M2, M3, M4> Display for Any<T, (M0, M1, M2, M3, M4)>
120where
121  M0: Display,
122  M1: Display,
123  M2: Display,
124  M3: Display,
125  M4: Display,
126{
127  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
128    let (ref m0, ref m1, ref m2, ref m3, ref m4) = self.0;
129
130    write!(f, "any of ({}, {}, {}, {}, {})", m0, m1, m2, m3, m4)
131  }
132}
133
134impl<T, M0, M1, M2, M3, M4> Matcher<T> for Any<T, (M0, M1, M2, M3, M4)>
135where
136  T: Clone,
137  M0: Matcher<T>,
138  M1: Matcher<T>,
139  M2: Matcher<T>,
140  M3: Matcher<T>,
141  M4: Matcher<T>,
142{
143  fn matches(&self, actual: T) -> MatchResult {
144    let (ref m0, ref m1, ref m2, ref m3, ref m4) = self.0;
145
146    m0.matches(actual.clone())
147      .or_else(|_| m1.matches(actual.clone()))
148      .or_else(|_| m2.matches(actual.clone()))
149      .or_else(|_| m3.matches(actual.clone()))
150      .or_else(|_| m4.matches(actual.clone()))
151  }
152}
153
154impl<T, M0, M1, M2, M3, M4, M5> Display for Any<T, (M0, M1, M2, M3, M4, M5)>
155where
156  M0: Display,
157  M1: Display,
158  M2: Display,
159  M3: Display,
160  M4: Display,
161  M5: Display,
162{
163  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
164    let (ref m0, ref m1, ref m2, ref m3, ref m4, ref m5) = self.0;
165
166    write!(f, "any of ({}, {}, {}, {}, {}, {})", m0, m1, m2, m3, m4, m5)
167  }
168}
169
170impl<T, M0, M1, M2, M3, M4, M5> Matcher<T> for Any<T, (M0, M1, M2, M3, M4, M5)>
171where
172  T: Clone,
173  M0: Matcher<T>,
174  M1: Matcher<T>,
175  M2: Matcher<T>,
176  M3: Matcher<T>,
177  M4: Matcher<T>,
178  M5: Matcher<T>,
179{
180  fn matches(&self, actual: T) -> MatchResult {
181    let (ref m0, ref m1, ref m2, ref m3, ref m4, ref m5) = self.0;
182
183    m0.matches(actual.clone())
184      .or_else(|_| m1.matches(actual.clone()))
185      .or_else(|_| m2.matches(actual.clone()))
186      .or_else(|_| m3.matches(actual.clone()))
187      .or_else(|_| m4.matches(actual.clone()))
188      .or_else(|_| m5.matches(actual.clone()))
189  }
190}