Skip to main content

serenade_mailer/
body.rs

1//! Message body (text and/or HTML).
2
3/// Plain text and/or HTML body parts.
4#[derive(Clone, Debug, Default, Eq, PartialEq)]
5pub struct Body {
6    text: Option<String>,
7    html: Option<String>,
8}
9
10impl Body {
11    /// Empty body.
12    #[must_use]
13    pub const fn empty() -> Self {
14        Self {
15            text: None,
16            html: None,
17        }
18    }
19
20    /// Text-only body.
21    #[must_use]
22    pub fn text(body: impl Into<String>) -> Self {
23        Self {
24            text: Some(body.into()),
25            html: None,
26        }
27    }
28
29    /// HTML-only body.
30    #[must_use]
31    pub fn html(body: impl Into<String>) -> Self {
32        Self {
33            text: None,
34            html: Some(body.into()),
35        }
36    }
37
38    /// Both text and HTML parts.
39    #[must_use]
40    pub fn both(text: impl Into<String>, html: impl Into<String>) -> Self {
41        Self {
42            text: Some(text.into()),
43            html: Some(html.into()),
44        }
45    }
46
47    /// Plain text part when set.
48    #[must_use]
49    pub fn text_part(&self) -> Option<&str> {
50        self.text.as_deref()
51    }
52
53    /// HTML part when set.
54    #[must_use]
55    pub fn html_part(&self) -> Option<&str> {
56        self.html.as_deref()
57    }
58
59    /// Whether any body part is present.
60    #[must_use]
61    pub const fn is_empty(&self) -> bool {
62        self.text.is_none() && self.html.is_none()
63    }
64}