warned 0.1.1

struct Warned<T, W> { value: T, warnings: Vec<W> }, which represents a value with warnings.
Documentation
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! The `Warned<T, W>` type represents a value with warnings, where as the `Result<T, E>` type represents a value or error.
//!
//! # Basic
//! ```
//! // new
//! let pi = warned::Warned::new(3.14, vec!["bad precision"]);
//! assert_eq!(pi.value, 3.14);
//! assert_eq!(pi.warnings, vec!["bad precision"]);
//!
//! // dereference
//! assert_eq!(*pi, 3.14);
//!
//! // unwrap
//! let mut warnings = vec!["several", "existing", "warnings"];
//! assert_eq!(pi.unwrap(&mut warnings), 3.14);
//! assert_eq!(warnings, vec!["several", "existing", "warnings", "bad precision"]);
//!
//! // into
//! let a: warned::Warned<i32, String> = 123.into();
//! assert_eq!(a.value, 123);
//! assert!(a.warnings.is_empty());
//! ```
//!
//! # Conversion between `Warned<T, W>` and `Result<T, W>`
//!
//! * [Warned::into_result] returns `Ok` only if `self` has no warnings.
//!   Otherwise, returns `Err`.
//! * `From<Warned<T, W>>` trait is implemented for `Result<T, Vec<W>>` with the [Warned::into_result].
//! * [Warned::from_result_or_else] returns a value with no warnings if the `src` is `Ok`.
//!   Otherwise, returns a value of `f()` with single warning.
//! * [Warned::from_result_or] returns a value with no warnings if the `src` is `Ok`.
//!   Otherwise, returns a given `default` value with single warning.
//! * [Warned::from_result_or_default] Returns a value with no warnings if the `src` is `Ok`.
//!   Otherwise, returns a `T::default()` value with single warning.
//! * [Warned::from_result] returns a `Some` value with no warnings if the `src` is `Ok`.
//!   Otherwise, returns `None` with single warning.
//!
//! # `FromIterator` implementation
//! ## `FromIterator<Warned<T, W>>`
//! A sequence of `Warned<T, W>` can be collected as a `Warned<Vec<T>, W>`.
//! ```
//! let src = vec![
//!     warned::Warned::new(111, vec![]),
//!     warned::Warned::new(222, vec!["oops"]),
//!     warned::Warned::new(333, vec!["foo", "bar"])
//! ];
//! let dst = src.into_iter().collect::<warned::Warned<Vec<_>, _>>();
//! assert_eq!(dst.value, vec![111, 222, 333]);
//! assert_eq!(dst.warnings, vec!["oops", "foo", "bar"]);
//! ```
//! ## `FromIterator<Result<T, E>>`
//! A sequence of `Result<T, E>` can be collected as a `Warned<Vec<T>, E>`.
//! ```
//! let src = vec![Ok(111), Err("oops"), Err("oops2"), Ok(222)];
//! let dst = src.into_iter().collect::<warned::Warned<Vec<_>, _>>();
//! assert_eq!(dst.value, vec![111, 222]);
//! assert_eq!(dst.warnings, vec!["oops", "oops2"]);
//! ```
//!
//! # `ForceFrom` trait, `ForceInto` trait
//! The pair of the traits are similar to [TryFrom]/[TryInto] traits pair.
//! [ForceFrom]/[ForceInto] returns [Warned] value, while [TryFrom]/[TryInto]
//! returns [Result], as follows.
//! ```ignore
//! pub trait ForceFrom<T>: Sized {
//!    type Warning;
//!    fn force_from(src: T) -> Warned<Self, Self::Warning>;
//!}
//! pub trait ForceInto<T> {
//!     type Warning;
//!     fn force_into(self) -> Warned<T, Self::Warning>;
//! }
//! ```
//! When you implement [ForceFrom] conversion, [ForceInto] implementation is
//! automatically defined by the blanket implementation below:
//! ```ignore
//! impl<T, U: ForceFrom<T>> ForceInto<U> for T {
//!    type Warning = U::Warning;
//!    fn force_into(self) -> Warned<U, Self::Warning> {
//!        U::force_from(self)
//!    }
//! }
//! ```
//! And the following blanket implementation is also supported.
//! ```ignore
//! impl<T: Into<U>, U> ForceFrom<T> for U {
//!     type Warning = std::convert::Infallible;
//!     fn force_from(src: T) -> Warned<Self, Self::Warning> {
//!         src.into().into()
//!     }
//! }
//! ```

/// Represents a value with warnings.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Warned<T, W> {
    pub value: T,
    pub warnings: Vec<W>,
}

impl<T, W> Warned<T, W> {
    /// ```
    /// use warned::*;
    /// let a = Warned::new(123, vec!["x", "y"]);
    /// assert_eq!(a.value, 123);
    /// assert_eq!(a.warnings, vec!["x", "y"]);
    /// ```
    pub fn new(value: T, warnings: impl IntoIterator<Item = W>) -> Self {
        Self {
            value,
            warnings: Vec::from_iter(warnings),
        }
    }

    /// ```
    /// use warned::*;
    /// let mut warnings: Vec<&str> = vec![];
    /// assert_eq!(Warned::new(123, vec!["x", "y"]).unwrap(&mut warnings), 123);
    /// assert_eq!(warnings, vec!["x", "y"]);
    /// ```
    pub fn unwrap<V>(self, warnings: &mut Vec<V>) -> T
    where
        W: Into<V>,
    {
        warnings.extend(self.warnings.into_iter().map(Into::into));
        self.value
    }

    /// ```
    /// use warned::*;
    /// let a = Warned::new(123, vec!["x", "y"]);
    /// let b = a.map(|value| 2 * value);
    /// assert_eq!(b.value, 246);
    /// assert_eq!(b.warnings, vec!["x", "y"]);
    /// ```
    pub fn map<U>(self, f: impl FnOnce(T) -> U) -> Warned<U, W> {
        Warned {
            value: f(self.value),
            warnings: self.warnings,
        }
    }

    /// ```
    /// use warned::*;
    /// let a = Warned::new(123, vec!["x", "y"]);
    /// let b = a.map_warnings(|s| s.to_uppercase());
    /// assert_eq!(b.value, 123);
    /// assert_eq!(b.warnings, vec!["X", "Y"]);
    /// ```
    pub fn map_warnings<V>(self, f: impl Fn(W) -> V) -> Warned<T, V> {
        Warned {
            value: self.value,
            warnings: self.warnings.into_iter().map(f).collect(),
        }
    }

    /// ```
    /// use warned::*;
    /// let a = Warned::new(123, vec!["x", "y"]);
    /// let b = a.and_then(|value| Warned::new(value as f64 / 2.0, vec!["z"]));
    /// assert_eq!(b.value, 123.0 / 2.0);
    /// assert_eq!(b.warnings, vec!["x", "y", "z"]);
    /// ```
    pub fn and_then<U>(mut self, f: impl Fn(T) -> Warned<U, W>) -> Warned<U, W> {
        Warned::new(f(self.value).unwrap(&mut self.warnings), self.warnings)
    }

    /// Returns `Ok` only if `self` has no warnings. Otherwise, returns `Err`.
    /// ```
    /// use warned::*;
    /// assert_eq!(Warned::<_, &str>::new(123, []).into_result(), Ok(123));
    /// assert_eq!(Warned::new(123, ["warning"]).into_result(), Err(vec!["warning"]));
    /// ```
    pub fn into_result(self) -> Result<T, Vec<W>> {
        if self.warnings.is_empty() {
            Ok(self.value)
        } else {
            Err(self.warnings)
        }
    }

    /// Returns a value with no warnings if the `src` is `Ok`.
    /// Otherwise, returns a value of `f()` with single warning.
    /// ```
    /// use warned::Warned;
    ///
    /// let a = Warned::<_, &str>::from_result_or_else(Ok(1), || 2);
    /// assert_eq!(a.value, 1);
    /// assert!(a.warnings.is_empty());
    ///
    /// let b = Warned::<_, &str>::from_result_or_else(Err("oops"), || 2);
    /// assert_eq!(b.value, 2);
    /// assert_eq!(b.warnings, vec!["oops"]);
    /// ```
    pub fn from_result_or_else(src: Result<T, W>, f: impl FnOnce() -> T) -> Self {
        match src {
            Ok(x) => x.into(),
            Err(e) => Self::new(f(), vec![e]),
        }
    }

    /// Returns a value with no warnings if the `src` is `Ok`.
    /// Otherwise, returns a given `default` value with single warning.
    /// ```
    /// use warned::Warned;
    ///
    /// let a = Warned::<_, &str>::from_result_or(Ok(1), 2);
    /// assert_eq!(a.value, 1);
    /// assert!(a.warnings.is_empty());
    ///
    /// let b = Warned::<_, &str>::from_result_or(Err("oops"), 2);
    /// assert_eq!(b.value, 2);
    /// assert_eq!(b.warnings, vec!["oops"]);
    /// ```
    pub fn from_result_or(src: Result<T, W>, default: T) -> Self {
        Self::from_result_or_else(src, || default)
    }

    /// Returns a value with no warnings if the `src` is `Ok`.
    /// Otherwise, returns a `T::default()` value with single warning.
    /// ```
    /// use warned::Warned;
    ///
    /// let a = Warned::<i32, &str>::from_result_or_default(Ok(1));
    /// assert_eq!(a.value, 1);
    /// assert!(a.warnings.is_empty());
    ///
    /// let b = Warned::<i32, &str>::from_result_or_default(Err("oops"));
    /// assert_eq!(b.value, 0);
    /// assert_eq!(b.warnings, vec!["oops"]);
    /// ```
    pub fn from_result_or_default(src: Result<T, W>) -> Self
    where
        T: Default,
    {
        Self::from_result_or_else(src, T::default)
    }
}

impl<T, W> Warned<Option<T>, W> {
    /// Returns a `Some` value with no warnings if the `src` is `Ok`.
    /// Otherwise, returns `None` with single warning.
    /// ```
    /// use warned::Warned;
    ///
    /// let a = Warned::<Option<i32>, &str>::from_result(Ok(111));
    /// assert_eq!(a.value, Some(111));
    /// assert!(a.warnings.is_empty());
    ///
    /// let b = Warned::<Option<i32>, &str>::from_result(Err("oops"));
    /// assert!(b.value.is_none());
    /// assert_eq!(b.warnings, vec!["oops"]);
    /// ```
    pub fn from_result(src: Result<T, W>) -> Self {
        match src {
            Ok(x) => Self::new(Some(x), vec![]),
            Err(e) => Self::new(None, vec![e]),
        }
    }
}

impl<T, W> std::ops::Deref for Warned<T, W> {
    type Target = T;
    fn deref(&self) -> &T {
        &self.value
    }
}

impl<T: Default, W> Default for Warned<T, W> {
    /// ```
    /// use warned::*;
    /// let a = Warned::<Vec<i32>, String>::default();
    /// assert!(a.value.is_empty());
    /// assert!(a.warnings.is_empty());
    /// ```
    fn default() -> Self {
        Self::new(T::default(), vec![])
    }
}

impl<T, W> From<T> for Warned<T, W> {
    /// ```
    /// use warned::*;
    /// let a: Warned<i32, String> = (111).into();
    /// assert_eq!(a.value, 111);
    /// assert!(a.warnings.is_empty());
    /// ```
    fn from(value: T) -> Self {
        Self::new(value, vec![])
    }
}

impl<T, E> From<Result<T, E>> for Warned<Option<T>, E> {
    /// ```
    /// use warned::Warned;
    ///
    /// let a: Warned<Option<i32>, &str> = Ok(111).into();
    /// assert_eq!(a.value, Some(111));
    /// assert!(a.warnings.is_empty());
    ///
    /// let b: Warned<Option<i32>, &str> = Err::<i32, _>("oops").into();
    /// assert!(b.value.is_none());
    /// assert_eq!(b.warnings, vec!["oops"]);
    /// ```
    fn from(result: Result<T, E>) -> Self {
        Self::from_result(result)
    }
}

impl<T: Default, E> From<Result<T, E>> for Warned<T, E> {
    /// ```
    /// use warned::Warned;
    ///
    /// let a: Warned<i32, &str> = Ok(111).into();
    /// assert_eq!(a.value, 111);
    /// assert!(a.warnings.is_empty());
    ///
    /// let b: Warned<i32, &str> = Err::<i32, _>("oops").into();
    /// assert_eq!(b.value, 0);
    /// assert_eq!(b.warnings, vec!["oops"]);
    /// ```
    fn from(result: Result<T, E>) -> Self {
        match result {
            Ok(x) => x.into(),
            Err(e) => Self::new(T::default(), vec![e]),
        }
    }
}

impl<T, W> From<Warned<T, W>> for Result<T, Vec<W>> {
    /// ```
    /// use warned::*;
    /// assert_eq!(Ok(123), Warned::<_, &str>::new(123, []).into());
    /// assert_eq!(Err(vec!["warning"]), Warned::new(123, ["warning"]).into());
    /// ```
    fn from(src: Warned<T, W>) -> Self {
        src.into_result()
    }
}

impl<T1, T2, W> From<(Warned<T1, W>, Warned<T2, W>)> for Warned<(T1, T2), W> {
    /// ```
    /// use warned::Warned;
    /// let a = Warned::new(123, vec!["x", "xx"]);
    /// let b = Warned::new(321, vec!["y", "yy"]);
    /// let c: Warned<(_, _), _> = (a, b).into();
    /// assert_eq!(c.value, (123, 321));
    /// assert_eq!(c.warnings, vec!["x", "xx", "y", "yy"]);
    /// ```
    fn from((a, b): (Warned<T1, W>, Warned<T2, W>)) -> Self {
        Self::new((a.value, b.value), a.warnings.into_iter().chain(b.warnings))
    }
}

impl<T, Ts: std::iter::FromIterator<T>, W> std::iter::FromIterator<Warned<T, W>> for Warned<Ts, W> {
    /// ```
    /// use warned::Warned;
    /// let src: Vec<Warned<i32, &str>> = vec![
    ///     Warned::new(111, vec![]),
    ///     Warned::new(222, vec!["oops"]),
    ///     Warned::new(333, vec!["foo", "bar"])
    /// ];
    /// let dst = src.into_iter().collect::<Warned<Vec<_>, _>>();
    /// assert_eq!(dst.value, vec![111, 222, 333]);
    /// assert_eq!(dst.warnings, vec!["oops", "foo", "bar"]);
    /// ```
    fn from_iter<I: IntoIterator<Item = Warned<T, W>>>(iter: I) -> Self {
        let mut warnings = vec![];
        let value = iter.into_iter().map(|x| x.unwrap(&mut warnings)).collect();
        Self { value, warnings }
    }
}

impl<T, Ts: std::iter::FromIterator<T>, W> std::iter::FromIterator<Result<T, W>> for Warned<Ts, W> {
    /// ```
    /// use warned::Warned;
    /// let src = vec![Ok(111), Err("oops"), Err("oops2"), Ok(222)];
    /// let dst = src.into_iter().collect::<Warned<Vec<_>, _>>();
    /// assert_eq!(dst.value, vec![111, 222]);
    /// assert_eq!(dst.warnings, vec!["oops", "oops2"]);
    /// ```
    fn from_iter<I: IntoIterator<Item = Result<T, W>>>(iter: I) -> Self {
        let mut warnings = vec![];
        Self {
            value: iter
                .into_iter()
                .filter_map(|item| Warned::from_result(item).unwrap(&mut warnings))
                .collect(),
            warnings,
        }
    }
}

/// Convert `T` into `Warned<T, Warning>`
pub trait ForceFrom<T>: Sized {
    type Warning;
    fn force_from(src: T) -> Warned<Self, Self::Warning>;
}

/// Convert `T` into `Warned<T, Warning>`
pub trait ForceInto<T> {
    type Warning;
    fn force_into(self) -> Warned<T, Self::Warning>;
}

/// ```
/// use warned::*;
/// struct A;
/// struct B;
/// impl ForceFrom<A> for B {
///     type Warning = String;
///     fn force_from(src: A) -> Warned<Self, Self::Warning> { Warned::new(B, vec![]) }
/// }
/// let _: Warned<B, String> = A.force_into();
/// ```
impl<T, U: ForceFrom<T>> ForceInto<U> for T {
    type Warning = U::Warning;
    fn force_into(self) -> Warned<U, Self::Warning> {
        U::force_from(self)
    }
}

/// ```
/// use warned::*;
/// struct A;
/// struct B;
/// impl From<A> for B {
///     fn from(src: A) -> B { B }
/// }
/// let _: Warned<B, std::convert::Infallible> = A.force_into();
/// ```
impl<T: Into<U>, U> ForceFrom<T> for U {
    type Warning = std::convert::Infallible;
    fn force_from(src: T) -> Warned<Self, Self::Warning> {
        src.into().into()
    }
}