proptest/test_runner/
reason.rs

1//-
2// Copyright 2017, 2018 The proptest developers
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 crate::std_facade::{fmt, Box, Cow, String};
11
12/// The reason for why something, such as a generated value, was rejected.
13///
14/// Currently this is merely a wrapper around a message, but more properties
15/// may be added in the future.
16///
17/// This is constructed via `.into()` on a `String`, `&'static str`, or
18/// `Box<str>`.
19#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
20pub struct Reason(Cow<'static, str>);
21
22impl Reason {
23    /// Return the message for this `Reason`.
24    ///
25    /// The message is intended for human consumption, and is not guaranteed to
26    /// have any format in particular.
27    pub fn message(&self) -> &str {
28        &*self.0
29    }
30}
31
32impl From<&'static str> for Reason {
33    fn from(s: &'static str) -> Self {
34        Reason(s.into())
35    }
36}
37
38impl From<String> for Reason {
39    fn from(s: String) -> Self {
40        Reason(s.into())
41    }
42}
43
44impl From<Box<str>> for Reason {
45    fn from(s: Box<str>) -> Self {
46        Reason(String::from(s).into())
47    }
48}
49
50impl fmt::Display for Reason {
51    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
52        fmt::Display::fmt(self.message(), f)
53    }
54}