#![allow(clippy::needless_late_init)] use std::fmt::{self, Debug, Display}; use thiserror::Error; pub struct NoFormat; #[derive(Debug)] pub struct DebugOnly; pub struct DisplayOnly; impl Display for DisplayOnly { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_str("display only") } } #[derive(Debug)] pub struct DebugAndDisplay; impl Display for DebugAndDisplay { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_str("debug and display") } } // Should expand to: // // impl Display for EnumDebugField // where // E: Debug; // // impl Error for EnumDebugField // where // Self: Debug + Display; // #[derive(Error, Debug)] pub enum EnumDebugGeneric { #[error("{0:?}")] FatalError(E), } // Should expand to: // // impl Display for EnumFromGeneric; // // impl Error for EnumFromGeneric // where // EnumDebugGeneric: Error + 'static, // Self: Debug + Display; // #[derive(Error, Debug)] pub enum EnumFromGeneric { #[error("enum from generic")] Source(#[from] EnumDebugGeneric), } // Should expand to: // // impl Display // for EnumCompound // where // HasDisplay: Display, // HasDebug: Debug; // // impl Error // for EnumCompound // where // Self: Debug + Display; // #[derive(Error)] pub enum EnumCompound { #[error("{0} {1:?}")] DisplayDebug(HasDisplay, HasDebug), #[error("{0}")] Display(HasDisplay, HasNeither), #[error("{1:?}")] Debug(HasNeither, HasDebug), } impl Debug for EnumCompound { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_str("EnumCompound") } } #[test] fn test_display_enum_compound() { let mut instance: EnumCompound; instance = EnumCompound::DisplayDebug(DisplayOnly, DebugOnly); assert_eq!(format!("{}", instance), "display only DebugOnly"); instance = EnumCompound::Display(DisplayOnly, NoFormat); assert_eq!(format!("{}", instance), "display only"); instance = EnumCompound::Debug(NoFormat, DebugOnly); assert_eq!(format!("{}", instance), "DebugOnly"); } // Should expand to: // // impl Display for EnumTransparentGeneric // where // E: Display; // // impl Error for EnumTransparentGeneric // where // E: Error, // Self: Debug + Display; // #[derive(Error, Debug)] pub enum EnumTransparentGeneric { #[error(transparent)] Other(E), } // Should expand to: // // impl Display for StructDebugGeneric // where // E: Debug; // // impl Error for StructDebugGeneric // where // Self: Debug + Display; // #[derive(Error, Debug)] #[error("{underlying:?}")] pub struct StructDebugGeneric { pub underlying: E, } // Should expand to: // // impl Error for StructFromGeneric // where // StructDebugGeneric: Error + 'static, // Self: Debug + Display; // #[derive(Error, Debug)] pub struct StructFromGeneric { #[from] pub source: StructDebugGeneric, } // Should expand to: // // impl Display for StructTransparentGeneric // where // E: Display; // // impl Error for StructTransparentGeneric // where // E: Error, // Self: Debug + Display; // #[derive(Error, Debug)] #[error(transparent)] pub struct StructTransparentGeneric(E);