markdown_builder/types/
image.rs

1use crate::traits::{AsFooter, MarkdownElement};
2use std::fmt;
3
4/// A markdown image.
5#[derive(Clone, Debug, Default)]
6pub struct Image {
7    /// Whether the image's link should be added as a footer reference.
8    pub footer: bool,
9    /// The text of the image.
10    pub text: String,
11    /// The url of the image.
12    pub url: String,
13}
14
15impl Image {
16    /// Creates a new default `Image`.
17    pub fn new() -> Self {
18        Self::default()
19    }
20
21    /// Creates a new `Image` with the given values.
22    pub fn from(url: impl Into<String>, text: impl Into<String>, footer: bool) -> Self {
23        Self {
24            text: text.into(),
25            url: url.into(),
26            footer,
27        }
28    }
29}
30
31impl AsFooter for Image {
32    fn as_footer(&self) -> Box<dyn MarkdownElement> {
33        Box::new(format!("[{}]: {}", self.text, self.url))
34    }
35}
36
37impl fmt::Display for Image {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        if self.footer {
40            writeln!(f, "![{}][{}]", self.text, self.text)
41        } else {
42            writeln!(f, "![{}]({})", self.text, self.url)
43        }
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn test_image_default() {
53        let image = Image::new();
54        assert_eq!(image.footer, false);
55        assert_eq!(image.text, "");
56        assert_eq!(image.url, "");
57    }
58
59    #[test]
60    fn test_image_from() {
61        let image = Image::from(
62            "https://example.com/picture.png",
63            "A cute image of a sandcat",
64            true,
65        );
66        assert_eq!(image.footer, true);
67        assert_eq!(image.text, "A cute image of a sandcat");
68        assert_eq!(image.url, "https://example.com/picture.png");
69    }
70
71    #[test]
72    fn test_image_url() {
73        assert_eq!(
74            Image::from("https://example.com/picture.png", "", false).render(),
75            "![](https://example.com/picture.png)\n"
76        );
77    }
78
79    #[test]
80    fn test_image_url_text() {
81        assert_eq!(
82            Image::from(
83                "https://example.com/picture.png",
84                "A cute picture of a sandcat",
85                false
86            )
87            .render(),
88            "![A cute picture of a sandcat](https://example.com/picture.png)\n"
89        );
90    }
91
92    #[test]
93    fn test_image_url_text_footer() {
94        let image = Image::from(
95            "https://example.com/picture.png",
96            "A cute picture of a sandcat",
97            true,
98        );
99        assert_eq!(
100            image.render(),
101            "![A cute picture of a sandcat][A cute picture of a sandcat]\n"
102        );
103        assert_eq!(
104            image.as_footer().render(),
105            "[A cute picture of a sandcat]: https://example.com/picture.png"
106        )
107    }
108}