markdown_builder/builders/
image.rs1use crate::types::image::Image;
2
3#[derive(Clone, Debug, Default, Eq, PartialEq)]
4pub struct ImageBuilder {
5 text: String,
6 url: String,
7 footer: bool,
8}
9
10impl ImageBuilder {
11 pub fn new() -> Self {
12 Self::default()
13 }
14
15 pub fn footer(mut self) -> Self {
16 self.footer = true;
17 self
18 }
19
20 pub fn set_footer(mut self, value: bool) -> Self {
21 self.footer = value;
22 self
23 }
24
25 pub fn text(mut self, text: impl Into<String>) -> Self {
26 self.text = text.into();
27 self
28 }
29
30 pub fn url(mut self, url: impl Into<String>) -> Self {
31 self.url = url.into();
32 self
33 }
34
35 pub fn build(self) -> Image {
36 Image::from(self.url, self.text, self.footer)
37 }
38}
39
40impl Image {
41 pub fn builder() -> ImageBuilder {
42 ImageBuilder::new()
43 }
44}
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49
50 #[test]
51 fn test_image_builder_url_footer() {
52 let image = Image::builder()
53 .url("https://example.com/picture.png")
54 .text("A cute picture of a sandcat")
55 .footer()
56 .build();
57
58 assert_eq!(image.footer, true);
59 assert_eq!(image.url, "https://example.com/picture.png");
60 assert_eq!(image.text, "A cute picture of a sandcat");
61 }
62
63 #[test]
64 fn test_image_builder_url_set_footer() {
65 let footer = Image::builder()
66 .url("https://example.com/picture.png")
67 .text("A cute picture of a sandcat")
68 .set_footer(true)
69 .build();
70
71 assert_eq!(footer.footer, true);
72 assert_eq!(footer.url, "https://example.com/picture.png");
73 assert_eq!(footer.text, "A cute picture of a sandcat");
74
75 let no_footer = Image::builder()
76 .url("https://example.com/picture.png")
77 .text("A cute picture of a sandcat")
78 .set_footer(false)
79 .build();
80
81 assert_eq!(no_footer.footer, false);
82 assert_eq!(no_footer.url, "https://example.com/picture.png");
83 assert_eq!(no_footer.text, "A cute picture of a sandcat");
84 }
85}