use std::fmt::Display;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[derive(Debug, PartialEq, Clone, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Strikethrough(pub String);
impl Strikethrough {
pub fn new<Body: Into<String>>(body: Body) -> Self {
Strikethrough(body.into())
}
}
impl Display for Strikethrough {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"~~{}~~",
self.0
.replace("\\", "\\\\")
.replace("~", "\\~")
.replace("\n\n", "\\\n\n")
)
}
}
impl From<String> for Strikethrough {
fn from(value: String) -> Self {
Strikethrough(value)
}
}
#[cfg(test)]
mod tests {
use crate::nodes::Strikethrough;
#[test]
fn strikethrough() {
let strikethrough: Strikethrough = "Strikethrough can contain any token even \n"
.to_string()
.into();
assert_eq!(
strikethrough.to_string(),
"~~Strikethrough can contain any token even \n~~"
);
}
#[test]
fn strikethrough_with_tilde() {
let strikethrough = Strikethrough::new("Strikethrough can contain any token even ~~");
assert_eq!(
strikethrough.to_string(),
"~~Strikethrough can contain any token even \\~\\~~~"
);
}
#[test]
fn strikethrough_with_terminator() {
let strikethrough = Strikethrough::new("Strikethrough with terminator\n\n");
assert_eq!(
strikethrough.to_string(),
"~~Strikethrough with terminator\\\n\n~~"
);
}
}