1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use std::convert::Infallible;

use crate::core::{Format, Formatter, MatchFailure, Matcher};
use crate::matchers::map::{IterMap, IterMapMatcher, IterTryMapMatcher, MapMatcher, TryMapMatcher};

use super::FailureFormat;

/// A formatter for matchers that never fail.
///
/// This formatter doesn't actually format anything, because it can never be called. It is mostly
/// useful for matchers like [`MapMatcher`] that can never fail.
#[derive(Debug, Default)]
pub struct InfallibleFormat;

impl Format for InfallibleFormat {
    type Value = MatchFailure<Infallible>;

    fn fmt(&self, _: &mut Formatter, _: Self::Value) -> crate::Result<()> {
        unreachable!()
    }
}

/// Infallibly map the input value by applying a function to it.
///
/// This does the same thing as [`Assertion::map`].
///
/// This matcher always succeeds, even when negated. Therefore negating it has no effect.
///
/// # Examples
///
/// ```
/// use std::convert::Infallible;
/// use xpct::{expect, map, equal};
///
/// fn do_stuff() -> Result<String, Infallible> {
///     Ok(String::from("foobar"))
/// }
///
/// expect!(do_stuff())
///     .to(map(Result::unwrap))
///     .to(equal("foobar"));
/// ```
///
/// [`Assertion::map`]: crate::core::Assertion::map
pub fn map<'a, In, Out>(func: impl FnOnce(In) -> Out + 'a) -> Matcher<'a, In, Out>
where
    In: 'a,
    Out: 'a,
{
    Matcher::transform(MapMatcher::new(func), InfallibleFormat)
}

/// Fallibly map the input value by applying a function to it.
///
/// This does the same thing as [`Assertion::try_map`].
///
/// This matcher always succeeds as long as `func` returns `Ok`, even when negated. Therefore
/// negating it has no effect.
///
/// # Examples
///
/// ```
/// use xpct::{expect, equal, try_map};
///
/// expect!(vec![0x43, 0x75, 0x6e, 0x6f])
///     .to(try_map(|bytes| Ok(String::from_utf8(bytes)?)))
///     .to(equal("Cuno"));
/// ```
///
/// [`Assertion::try_map`]: crate::core::Assertion::map
pub fn try_map<'a, In, Out>(
    func: impl FnOnce(In) -> crate::Result<Out> + 'a,
) -> Matcher<'a, In, Out>
where
    In: 'a,
    Out: 'a,
{
    Matcher::transform(TryMapMatcher::new(func), FailureFormat::new())
}

/// Infallibly convert the input value via [`From`]/[`Into`].
///
/// This does the same thing as [`Assertion::into`].
///
/// This matcher always succeeds, even when negated. Therefore negating it has no effect.
///
/// # Examples
///
/// ```
/// use xpct::{expect, equal, into};
///
/// expect!(41u32)
///     .to(into::<_, u64>())
///     .to(equal(41u64));
/// ```
///
/// [`Assertion::into`]: crate::core::Assertion::into
pub fn into<'a, In, Out>() -> Matcher<'a, In, Out>
where
    In: 'a,
    Out: From<In> + 'a,
{
    Matcher::transform(MapMatcher::new(<Out as From<In>>::from), InfallibleFormat)
}

/// Fallibly convert the input value via [`TryFrom`]/[`TryInto`].
///
/// This does the same thing as [`Assertion::try_into`].
///
/// This matcher always succeeds as long as [`TryFrom::try_from`] returns `Ok`, even when negated.
/// Therefore negating it has no effect.
///
/// # Examples
///
/// ```
/// use xpct::{expect, equal, try_into};
///
/// expect!(41u64)
///     .to(try_into::<_, u32>())
///     .to(equal(41u32));
/// ```
///
/// [`Assertion::try_into`]: crate::core::Assertion::try_into
pub fn try_into<'a, In, Out>() -> Matcher<'a, In, Out>
where
    In: 'a,
    Out: TryFrom<In> + 'a,
    <Out as TryFrom<In>>::Error: std::error::Error + Send + Sync + 'static,
{
    Matcher::transform(
        TryMapMatcher::new(|value| {
            <Out as TryFrom<In>>::try_from(value).map_err(crate::Error::new)
        }),
        FailureFormat::new(),
    )
}

/// Infallibly map each value of an iterator by applying a function to it.
///
/// This does the same thing as [`Assertion::iter_map`].
///
/// This matcher always succeeds, even when negated. Therefore negating it has no effect.
///
/// # Examples
///
/// ```
/// use xpct::{be_some, every, expect, iter_map};
///
/// let items = vec![Some("foo"), Some("bar")];
///
/// let output: Vec<&str> = expect!(&items)
///     .to(iter_map(Option::as_deref))
///     .to(every(be_some))
///     .into_inner();
/// ```
///
/// [`Assertion::iter_map`]: crate::core::Assertion::iter_map
pub fn iter_map<'a, In, Out, IntoIter>(
    func: impl Fn(In) -> Out + 'a,
) -> Matcher<'a, IntoIter, IterMap<'a, In, Out, IntoIter::IntoIter>>
where
    In: 'a,
    Out: 'a,
    IntoIter: IntoIterator<Item = In> + 'a,
{
    Matcher::transform(IterMapMatcher::new(func), InfallibleFormat)
}

/// Fallibly map each value of an iterator by applying a function to it.
///
/// This does the same thing as [`Assertion::iter_try_map`].
///
/// This matcher always succeeds as long as `func` returns `Ok`, even when negated. Therefore
/// negating it has no effect.
///
/// # Examples
///
/// ```
/// use xpct::{expect, iter_try_map, consist_of};
///
/// let small_integers: [u64; 2] = [41, 57];
///
/// expect!(small_integers)
///     .to(iter_try_map(|value| Ok(u32::try_from(value)?)))
///     .to(consist_of([41u32, 57u32]));
/// ```
///
/// [`Assertion::iter_try_map`]: crate::core::Assertion::iter_try_map
pub fn iter_try_map<'a, In, Out, IntoIter>(
    func: impl Fn(In) -> crate::Result<Out> + 'a,
) -> Matcher<'a, IntoIter, Vec<Out>>
where
    In: 'a,
    Out: 'a,
    IntoIter: IntoIterator<Item = In> + 'a,
{
    Matcher::transform(IterTryMapMatcher::new(func), InfallibleFormat)
}