templateless/
collection.rs1use serde::Serialize;
2
3use crate::{
4 components::{
5 Button, Component, Image, Link, Otp, QrCode, Signature, SocialItem,
6 Socials, StoreBadgeItem, StoreBadges, Text, ViewInBrowser,
7 },
8 Result,
9};
10
11#[derive(Clone, Serialize)]
12pub struct Collection {
13 pub components: Vec<Box<dyn Component>>,
14}
15
16impl Collection {
17 pub fn builder() -> Self {
18 Self { components: vec![] }
19 }
20
21 pub fn button(self, text: &str, url: &str) -> Self {
22 self.push(Box::new(Button::new(text, url)))
23 }
24
25 pub fn image(self, src: &str) -> Self {
26 self.push(Box::new(Image::new(src)))
27 }
28
29 pub fn link(self, text: &str, url: &str) -> Self {
30 self.push(Box::new(Link::new(text, url)))
31 }
32
33 pub fn otp(self, text: &str) -> Self {
34 self.push(Box::new(Otp::new(text)))
35 }
36
37 pub fn socials(self, data: &[SocialItem]) -> Self {
38 self.push(Box::new(Socials::new(data.to_vec())))
39 }
40
41 pub fn text(self, text: &str) -> Self {
42 self.push(Box::new(Text::new(text)))
43 }
44
45 pub fn view_in_browser(self) -> Self {
46 self.push(Box::new(ViewInBrowser::new(None)))
47 }
48
49 pub fn qr_code(self, url: &str) -> Self {
50 self.push(Box::new(QrCode::url(url)))
51 }
52
53 pub fn store_badges(self, data: &[StoreBadgeItem]) -> Self {
54 self.push(Box::new(StoreBadges::new(data.to_vec())))
55 }
56
57 pub fn signature(self, text: &str) -> Self {
58 self.push(Box::new(Signature::new(text, None)))
59 }
60
61 pub fn component<T>(self, c: T) -> Self
62 where
63 T: Component + 'static,
64 {
65 self.push(Box::new(c))
66 }
67
68 pub fn build(&self) -> Result<Self> {
69 Ok(self.clone())
70 }
71
72 fn push(mut self, component: Box<dyn Component>) -> Self {
73 self.components.push(component);
74 self
75 }
76}