Skip to main content

ferrum_email_core/
component.rs

1//! The `Component` trait — the core abstraction of Ferrum Email.
2//!
3//! Every email template and reusable email element implements this trait.
4
5use crate::node::Node;
6
7/// The core trait for all Ferrum Email components.
8///
9/// Components are composable building blocks for email templates. Each component
10/// defines how it renders to a `Node` tree, which the renderer then converts
11/// to email-safe HTML.
12///
13/// # Examples
14///
15/// ```
16/// use ferrum_email_core::{Component, Node, node::Element, node::Tag};
17///
18/// struct HelloEmail {
19///     name: String,
20/// }
21///
22/// impl Component for HelloEmail {
23///     fn render(&self) -> Node {
24///         Node::text(format!("Hello, {}!", self.name))
25///     }
26///
27///     fn subject(&self) -> Option<&str> {
28///         Some("Hello!")
29///     }
30/// }
31/// ```
32pub trait Component: Send + Sync {
33    /// Render this component into a `Node` tree.
34    fn render(&self) -> Node;
35
36    /// Optional: provide a custom plain text version.
37    ///
38    /// If `None`, the renderer will auto-extract plain text from the node tree.
39    fn plain_text(&self) -> Option<String> {
40        None
41    }
42
43    /// The email subject line. Only meaningful on top-level email components.
44    fn subject(&self) -> Option<&str> {
45        None
46    }
47}