use alloc::string::String;
use core::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Journal {
Plain(String),
Html(String),
}
impl Journal {
pub const fn as_text(&self) -> &str {
match self {
Self::Plain(s) | Self::Html(s) => s.as_str(),
}
}
pub const fn as_html(&self) -> Option<&str> {
match self {
Self::Html(s) => Some(s.as_str()),
Self::Plain(_) => None,
}
}
pub const fn is_html(&self) -> bool {
matches!(self, Self::Html(_))
}
}
impl fmt::Display for Journal {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Plain(s) => write!(f, "Journal::Plain({} chars)", s.len()),
Self::Html(s) => write!(f, "Journal::Html({} chars)", s.len()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn plain_journal_as_text() {
let j = Journal::Plain(String::from("experiment notes"));
assert_eq!(j.as_text(), "experiment notes");
assert!(j.as_html().is_none());
assert!(!j.is_html());
}
#[test]
fn html_journal_as_html() {
let j = Journal::Html(String::from("<html><body>notes</body></html>"));
assert!(j.as_html().is_some());
assert!(j.is_html());
assert_eq!(j.as_text(), "<html><body>notes</body></html>");
}
}