use std::fmt;
use std::path::Path;
use crate::context::Context;
pub enum Message<'a> {
Borrowed(&'a dyn fmt::Display),
Owned(String),
}
pub trait ToMessage {
fn to_message(&self, ctx: &Context) -> Message<'_>;
}
impl fmt::Display for Message<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Message::Borrowed(b) => fmt::Display::fmt(b, f),
Message::Owned(o) => fmt::Display::fmt(o, f),
}
}
}
impl<T> ToMessage for &T
where
T: fmt::Display,
{
fn to_message(&self, _: &Context) -> Message<'_> {
Message::Borrowed(self)
}
}
impl ToMessage for &Path {
fn to_message(&self, ctx: &Context) -> Message<'_> {
Message::Owned(ctx.replace_home(self).display().to_string())
}
}