| 1 |
#![allow(clippy::needless_late_init, clippy::uninlined_format_args)] |
| 2 |
|
| 3 |
use std::fmt::{self, Debug, Display}; |
| 4 |
use thiserror::Error; |
| 5 |
|
| 6 |
pub struct NoFormat; |
| 7 |
|
| 8 |
#[derive(Debug)] |
| 9 |
pub struct DebugOnly; |
| 10 |
|
| 11 |
pub struct DisplayOnly; |
| 12 |
|
| 13 |
impl Display for DisplayOnly { |
| 14 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 15 |
f.write_str("display only") |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
#[derive(Debug)] |
| 20 |
pub struct DebugAndDisplay; |
| 21 |
|
| 22 |
impl Display for DebugAndDisplay { |
| 23 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 24 |
f.write_str("debug and display") |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
// Should expand to: |
| 29 |
// |
| 30 |
// impl<E> Display for EnumDebugField<E> |
| 31 |
// where |
| 32 |
// E: Debug; |
| 33 |
// |
| 34 |
// impl<E> Error for EnumDebugField<E> |
| 35 |
// where |
| 36 |
// Self: Debug + Display; |
| 37 |
// |
| 38 |
#[derive(Error, Debug)] |
| 39 |
pub enum EnumDebugGeneric<E> { |
| 40 |
#[error("{0:?}")] |
| 41 |
FatalError(E), |
| 42 |
} |
| 43 |
|
| 44 |
// Should expand to: |
| 45 |
// |
| 46 |
// impl<E> Display for EnumFromGeneric<E>; |
| 47 |
// |
| 48 |
// impl<E> Error for EnumFromGeneric<E> |
| 49 |
// where |
| 50 |
// EnumDebugGeneric<E>: Error + 'static, |
| 51 |
// Self: Debug + Display; |
| 52 |
// |
| 53 |
#[derive(Error, Debug)] |
| 54 |
pub enum EnumFromGeneric<E> { |
| 55 |
#[error("enum from generic")] |
| 56 |
Source(#[from] EnumDebugGeneric<E>), |
| 57 |
} |
| 58 |
|
| 59 |
// Should expand to: |
| 60 |
// |
| 61 |
// impl<HasDisplay, HasDebug, HasNeither> Display |
| 62 |
// for EnumCompound<HasDisplay, HasDebug, HasNeither> |
| 63 |
// where |
| 64 |
// HasDisplay: Display, |
| 65 |
// HasDebug: Debug; |
| 66 |
// |
| 67 |
// impl<HasDisplay, HasDebug, HasNeither> Error |
| 68 |
// for EnumCompound<HasDisplay, HasDebug, HasNeither> |
| 69 |
// where |
| 70 |
// Self: Debug + Display; |
| 71 |
// |
| 72 |
#[derive(Error)] |
| 73 |
pub enum EnumCompound<HasDisplay, HasDebug, HasNeither> { |
| 74 |
#[error("{0} {1:?}")] |
| 75 |
DisplayDebug(HasDisplay, HasDebug), |
| 76 |
#[error("{0}")] |
| 77 |
Display(HasDisplay, HasNeither), |
| 78 |
#[error("{1:?}")] |
| 79 |
Debug(HasNeither, HasDebug), |
| 80 |
} |
| 81 |
|
| 82 |
impl<HasDisplay, HasDebug, HasNeither> Debug for EnumCompound<HasDisplay, HasDebug, HasNeither> { |
| 83 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 84 |
f.write_str("EnumCompound") |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
#[test] |
| 89 |
fn test_display_enum_compound() { |
| 90 |
let mut instance: EnumCompound<DisplayOnly, DebugOnly, NoFormat>; |
| 91 |
|
| 92 |
instance = EnumCompound::DisplayDebug(DisplayOnly, DebugOnly); |
| 93 |
assert_eq!(format!("{}", instance), "display only DebugOnly"); |
| 94 |
|
| 95 |
instance = EnumCompound::Display(DisplayOnly, NoFormat); |
| 96 |
assert_eq!(format!("{}", instance), "display only"); |
| 97 |
|
| 98 |
instance = EnumCompound::Debug(NoFormat, DebugOnly); |
| 99 |
assert_eq!(format!("{}", instance), "DebugOnly"); |
| 100 |
} |
| 101 |
|
| 102 |
// Should expand to: |
| 103 |
// |
| 104 |
// impl<E> Display for EnumTransparentGeneric<E> |
| 105 |
// where |
| 106 |
// E: Display; |
| 107 |
// |
| 108 |
// impl<E> Error for EnumTransparentGeneric<E> |
| 109 |
// where |
| 110 |
// E: Error, |
| 111 |
// Self: Debug + Display; |
| 112 |
// |
| 113 |
#[derive(Error, Debug)] |
| 114 |
pub enum EnumTransparentGeneric<E> { |
| 115 |
#[error(transparent)] |
| 116 |
Other(E), |
| 117 |
} |
| 118 |
|
| 119 |
// Should expand to: |
| 120 |
// |
| 121 |
// impl<E> Display for StructDebugGeneric<E> |
| 122 |
// where |
| 123 |
// E: Debug; |
| 124 |
// |
| 125 |
// impl<E> Error for StructDebugGeneric<E> |
| 126 |
// where |
| 127 |
// Self: Debug + Display; |
| 128 |
// |
| 129 |
#[derive(Error, Debug)] |
| 130 |
#[error("{underlying:?}")] |
| 131 |
pub struct StructDebugGeneric<E> { |
| 132 |
pub underlying: E, |
| 133 |
} |
| 134 |
|
| 135 |
// Should expand to: |
| 136 |
// |
| 137 |
// impl<E> Error for StructFromGeneric<E> |
| 138 |
// where |
| 139 |
// StructDebugGeneric<E>: Error + 'static, |
| 140 |
// Self: Debug + Display; |
| 141 |
// |
| 142 |
#[derive(Error, Debug)] |
| 143 |
pub struct StructFromGeneric<E> { |
| 144 |
#[from] |
| 145 |
pub source: StructDebugGeneric<E>, |
| 146 |
} |
| 147 |
|
| 148 |
// Should expand to: |
| 149 |
// |
| 150 |
// impl<E> Display for StructTransparentGeneric<E> |
| 151 |
// where |
| 152 |
// E: Display; |
| 153 |
// |
| 154 |
// impl<E> Error for StructTransparentGeneric<E> |
| 155 |
// where |
| 156 |
// E: Error, |
| 157 |
// Self: Debug + Display; |
| 158 |
// |
| 159 |
#[derive(Error, Debug)] |
| 160 |
#[error(transparent)] |
| 161 |
pub struct StructTransparentGeneric<E>(E); |