use std::fmt::Display; use time::OffsetDateTime; use pretty::RcDoc; pub struct PrintableConversation { pub guid: String, pub date: OffsetDateTime, pub unread_count: i32, pub last_message_preview: Option, pub participants: Vec, pub display_name: Option, } impl From for PrintableConversation { fn from(value: kordophone::model::Conversation) -> Self { Self { guid: value.guid, date: value.date, unread_count: value.unread_count, last_message_preview: value.last_message_preview, participants: value.participant_display_names, display_name: value.display_name, } } } impl From for PrintableConversation { fn from(value: kordophone_db::models::Conversation) -> Self { Self { guid: value.guid, date: OffsetDateTime::from_unix_timestamp(value.date.and_utc().timestamp()).unwrap(), unread_count: value.unread_count.into(), last_message_preview: value.last_message_preview, participants: value.participants.into_iter().map(|p| p.display_name).collect(), display_name: value.display_name, } } } pub struct ConversationPrinter<'a> { doc: RcDoc<'a, PrintableConversation> } impl<'a> ConversationPrinter<'a> { pub fn new(conversation: &'a PrintableConversation) -> Self { let preview = conversation.last_message_preview .as_deref() .unwrap_or("") .replace('\n', " "); let doc = RcDoc::text(format!("")) .append(RcDoc::line()) .append("Date: ") .append(conversation.date.to_string()) .append(RcDoc::line()) .append("Participants: ") .append("[") .append(RcDoc::line() .append( conversation.participants .iter() .map(|name| RcDoc::text(name) .append(",") .append(RcDoc::line()) ) .fold(RcDoc::nil(), |acc, x| acc.append(x)) ) .nest(4) ) .append("]") .append(RcDoc::line()) .append("Last Message Preview: ") .append(preview) .nest(4) ) .append(RcDoc::line()) .append(">"); ConversationPrinter { doc } } } impl<'a> Display for ConversationPrinter<'a> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.doc.render_fmt(180, f) } }