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 Image {
pub alt: String,
pub src: String,
}
impl Image {
pub fn new<S: Into<String>>(alt: S, src: S) -> Self {
Self {
alt: alt.into(),
src: src.into(),
}
}
}
impl Display for Image {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"",
self.alt.replace("]", "\\]").replace("\n\n", "\\\n\n"),
self.src.replace("\n\n", "\\\n\n")
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn image() {
let image = Image::new("alt", "src");
assert_eq!(image.to_string(), "");
}
#[test]
fn image_with_nested_squares() {
let image = Image::new("alt [nested squares]", "src");
assert_eq!(image.to_string(), "![alt [nested squares\\]](src)");
}
#[test]
fn image_with_nested_parentheses() {
let image = Image::new("alt", "src(with nested)parentheses");
assert_eq!(image.to_string(), "parentheses)");
}
#[test]
fn image_with_terminator() {
let image = Image::new("alt\n\n", "src\n\n");
assert_eq!(image.to_string(), "");
}
}