Display Trait


Define a display trait for a struct or enum

impl std::fmt::Display for MechanismError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            MechanismError::NotValidMechanismFormat(mesg) => {
                write!(f, "{} does not conform to any Mechanism format.", mesg)
            }
            MechanismError::NotIP4Network(mesg) => {
                write!(f, "Was given ip4:{}. This is not an ip4 network.", mesg)
            }
            MechanismError::NotIP6Network(mesg) => {
                write!(f, "Was given ip6:{}. This is not an ip6 network.", mesg)
            }
            MechanismError::NotValidIPNetwork(mesg) => {
                write!(f, "{}.", mesg)
            }
            MechanismError::NotIpNetworkMechanism => {
                write!(f, "Attempt to access TXT as IP.")
            }
            MechanismError::NotStringMechanism => {
                write!(f, "Attempt to access IP as TXT.")
            }
        }
    }
}

Reference:

How to to_string() in Rust


See also