| 1 | // Copyright (c) 2018 The predicates-rs Project Developers. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 6 | // option. This file may not be copied, modified, or distributed |
| 7 | // except according to those terms. |
| 8 | |
| 9 | use std::fmt; |
| 10 | |
| 11 | use crate::reflection; |
| 12 | use crate::Predicate; |
| 13 | |
| 14 | #[derive(Clone, PartialEq, Eq)] |
| 15 | pub(crate) struct DebugAdapter<T> |
| 16 | where |
| 17 | T: fmt::Debug, |
| 18 | { |
| 19 | pub(crate) debug: T, |
| 20 | } |
| 21 | |
| 22 | impl<T> DebugAdapter<T> |
| 23 | where |
| 24 | T: fmt::Debug, |
| 25 | { |
| 26 | pub fn new(debug: T) -> Self { |
| 27 | Self { debug } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | impl<T> fmt::Display for DebugAdapter<T> |
| 32 | where |
| 33 | T: fmt::Debug, |
| 34 | { |
| 35 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 36 | write!(f, "{:#?}" , self.debug) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | impl<T> fmt::Debug for DebugAdapter<T> |
| 41 | where |
| 42 | T: fmt::Debug, |
| 43 | { |
| 44 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 45 | self.debug.fmt(f) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | pub(crate) fn default_find_case<'a, P, Item>( |
| 50 | pred: &'a P, |
| 51 | expected: bool, |
| 52 | variable: &Item, |
| 53 | ) -> Option<reflection::Case<'a>> |
| 54 | where |
| 55 | P: Predicate<Item>, |
| 56 | Item: ?Sized, |
| 57 | { |
| 58 | let actual = pred.eval(variable); |
| 59 | if expected == actual { |
| 60 | Some(reflection::Case::new(Some(pred), actual)) |
| 61 | } else { |
| 62 | None |
| 63 | } |
| 64 | } |
| 65 | |