use serde::{Deserialize, Serialize};
#[cfg(feature = "unstable-msc4095")]
use super::url_preview::UrlPreview;
use super::FormattedBody;
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[serde(tag = "msgtype", rename = "m.text")]
pub struct TextMessageEventContent {
pub body: String,
#[serde(flatten)]
pub formatted: Option<FormattedBody>,
#[cfg(feature = "unstable-msc4095")]
#[serde(
rename = "com.beeper.linkpreviews",
skip_serializing_if = "Option::is_none",
alias = "m.url_previews"
)]
pub url_previews: Option<Vec<UrlPreview>>,
}
impl TextMessageEventContent {
pub fn plain(body: impl Into<String>) -> Self {
let body = body.into();
Self {
body,
formatted: None,
#[cfg(feature = "unstable-msc4095")]
url_previews: None,
}
}
pub fn html(body: impl Into<String>, html_body: impl Into<String>) -> Self {
let body = body.into();
Self {
body,
formatted: Some(FormattedBody::html(html_body)),
#[cfg(feature = "unstable-msc4095")]
url_previews: None,
}
}
#[cfg(feature = "markdown")]
pub fn markdown(body: impl AsRef<str> + Into<String>) -> Self {
if let Some(formatted) = FormattedBody::markdown(&body) {
Self::html(body, formatted.body)
} else {
Self::plain(body)
}
}
}