use std::fmt::{self, Debug, Display, Formatter};
struct DisplayOnly;
impl Display for DisplayOnly {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str("display-only")
}
}
struct DebugOnly;
impl Debug for DebugOnly {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str("debug-only")
}
}
struct DisplayAndDebug;
impl Display for DisplayAndDebug {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str("display-wins")
}
}
impl Debug for DisplayAndDebug {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str("debug-loses")
}
}
struct Plain;
#[test]
fn try_to_string_returns_cloned_string_for_borrowed_and_owned_values() {
let value = "hello".to_string();
assert_eq!(
phlow::try_to_string!(value.clone()),
Some("hello".to_string())
);
assert_eq!(phlow::try_to_string!(&value), Some("hello".to_string()));
}
#[test]
fn try_to_string_uses_display_when_available_for_borrowed_and_owned_values() {
assert_eq!(
phlow::try_to_string!(DisplayOnly),
Some("display-only".to_string())
);
assert_eq!(
phlow::try_to_string!(&DisplayOnly),
Some("display-only".to_string())
);
}
#[test]
fn try_to_string_uses_debug_when_display_is_unavailable_for_borrowed_and_owned_values() {
assert_eq!(
phlow::try_to_string!(DebugOnly),
Some("debug-only".to_string())
);
assert_eq!(
phlow::try_to_string!(&DebugOnly),
Some("debug-only".to_string())
);
}
#[test]
fn try_to_string_prefers_display_over_debug_for_borrowed_and_owned_values() {
assert_eq!(
phlow::try_to_string!(DisplayAndDebug),
Some("display-wins".to_string())
);
assert_eq!(
phlow::try_to_string!(&DisplayAndDebug),
Some("display-wins".to_string())
);
}
#[test]
fn try_to_string_returns_none_for_plain_types_in_both_forms() {
assert_eq!(phlow::try_to_string!(Plain), None);
assert_eq!(phlow::try_to_string!(&Plain), None);
}