blixt 0.2.0

Blixt core framework — compile-time templates, type-safe SQL, Datastar SSE integration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use askama::Template;
use lettre::message::Mailbox;
use lettre::message::header::ContentType;
use lettre::transport::smtp::authentication::Credentials;
use lettre::{AsyncSmtpTransport, AsyncTransport, Message, Tokio1Executor};
use secrecy::{ExposeSecret, SecretString};
use std::fmt;

/// Configuration for the SMTP mailer.
///
/// All fields are required. Use [`MailerConfig::from_env`] to load from
/// environment variables, or construct manually for testing.
pub struct MailerConfig {
    /// SMTP server hostname.
    pub smtp_host: String,
    /// SMTP server port (typically 587 for STARTTLS).
    pub smtp_port: u16,
    /// SMTP authentication username.
    pub smtp_user: String,
    /// SMTP authentication password.
    pub smtp_password: SecretString,
    /// Display name for the `From` header.
    pub from_name: String,
    /// Email address for the `From` header.
    pub from_email: String,
}

impl fmt::Debug for MailerConfig {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("MailerConfig")
            .field("smtp_host", &self.smtp_host)
            .field("smtp_port", &self.smtp_port)
            .field("smtp_user", &self.smtp_user)
            .field("smtp_password", &"[REDACTED]")
            .field("from_name", &self.from_name)
            .field("from_email", &self.from_email)
            .finish()
    }
}

impl MailerConfig {
    /// Loads mailer configuration from environment variables.
    ///
    /// Required variables: `SMTP_HOST`, `SMTP_PORT`, `SMTP_USER`,
    /// `SMTP_PASSWORD`, `FROM_NAME`, `FROM_EMAIL`.
    pub fn from_env() -> crate::error::Result<Self> {
        Ok(Self {
            smtp_host: require_env("SMTP_HOST")?,
            smtp_port: require_env("SMTP_PORT")?
                .parse::<u16>()
                .map_err(|e| crate::error::Error::Internal(format!("Invalid SMTP_PORT: {e}")))?,
            smtp_user: require_env("SMTP_USER")?,
            smtp_password: SecretString::from(require_env("SMTP_PASSWORD")?),
            from_name: require_env("FROM_NAME")?,
            from_email: require_env("FROM_EMAIL")?,
        })
    }
}

/// Reads a required environment variable, returning a clear error if missing.
fn require_env(key: &str) -> crate::error::Result<String> {
    std::env::var(key).map_err(|_| {
        crate::error::Error::Internal(format!("Missing required environment variable: {key}"))
    })
}

/// Async email sender backed by an SMTP transport.
///
/// Supports both HTML (via Askama templates) and plain-text emails.
/// The SMTP password is never logged or exposed in debug output.
pub struct Mailer {
    transport: AsyncSmtpTransport<Tokio1Executor>,
    from: Mailbox,
}

impl fmt::Debug for Mailer {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Mailer")
            .field("from", &self.from)
            .finish_non_exhaustive()
    }
}

impl Mailer {
    /// Creates a new mailer from the given configuration.
    ///
    /// Builds an encrypted SMTP transport (STARTTLS) with credentials
    /// derived from the config.
    pub fn new(config: MailerConfig) -> crate::error::Result<Self> {
        let credentials = Credentials::new(
            config.smtp_user.clone(),
            config.smtp_password.expose_secret().to_owned(),
        );
        let transport = AsyncSmtpTransport::<Tokio1Executor>::starttls_relay(&config.smtp_host)
            .map_err(|e| crate::error::Error::Internal(format!("SMTP transport error: {e}")))?
            .port(config.smtp_port)
            .credentials(credentials)
            .build();

        let from = build_mailbox(&config.from_name, &config.from_email)?;

        tracing::info!(
            smtp_host = %config.smtp_host,
            smtp_port = config.smtp_port,
            from = %from,
            "Mailer initialized"
        );

        Ok(Self { transport, from })
    }

    /// Sends an HTML email rendered from an Askama template.
    ///
    /// The template is rendered at call time; rendering errors are
    /// returned as `Error::Internal`.
    pub async fn send_html<T: Template>(
        &self,
        to: &str,
        subject: &str,
        template: T,
    ) -> crate::error::Result<()> {
        let html = template
            .render()
            .map_err(|e| crate::error::Error::Internal(format!("Template error: {e}")))?;

        let email = self.build_message(to, subject, ContentType::TEXT_HTML, html)?;
        self.dispatch(email).await
    }

    /// Sends a plain-text email.
    pub async fn send_text(
        &self,
        to: &str,
        subject: &str,
        body: String,
    ) -> crate::error::Result<()> {
        let email = self.build_message(to, subject, ContentType::TEXT_PLAIN, body)?;
        self.dispatch(email).await
    }

    /// Constructs a `Message` with the given content type and body.
    fn build_message(
        &self,
        to: &str,
        subject: &str,
        content_type: ContentType,
        body: String,
    ) -> crate::error::Result<Message> {
        let to_mailbox: Mailbox = to
            .parse()
            .map_err(|e| crate::error::Error::BadRequest(format!("Invalid recipient: {e}")))?;

        Message::builder()
            .from(self.from.clone())
            .to(to_mailbox)
            .subject(subject)
            .header(content_type)
            .body(body)
            .map_err(|e| crate::error::Error::Internal(format!("Email build error: {e}")))
    }

    /// Sends a pre-built message via the SMTP transport.
    async fn dispatch(&self, message: Message) -> crate::error::Result<()> {
        self.transport
            .send(message)
            .await
            .map_err(|e| crate::error::Error::Internal(format!("SMTP send error: {e}")))?;
        Ok(())
    }
}

/// Parses a name and email into a [`Mailbox`].
fn build_mailbox(name: &str, email: &str) -> crate::error::Result<Mailbox> {
    let address = email
        .parse()
        .map_err(|e| crate::error::Error::Internal(format!("Invalid from address: {e}")))?;
    Ok(Mailbox::new(Some(name.to_owned()), address))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Mutex;

    static ENV_LOCK: Mutex<()> = Mutex::new(());

    /// Sets environment variables for the duration of the closure, then
    /// restores previous values. Serialized via `ENV_LOCK` to avoid races.
    fn with_env_vars<F, R>(vars: &[(&str, Option<&str>)], f: F) -> R
    where
        F: FnOnce() -> R,
    {
        let _guard = ENV_LOCK.lock().expect("env lock poisoned");

        let mut previous: Vec<(&str, Option<String>)> = Vec::new();
        for &(key, value) in vars {
            previous.push((key, std::env::var(key).ok()));
            // SAFETY: protected by ENV_LOCK mutex; tests run serially
            unsafe {
                match value {
                    Some(v) => std::env::set_var(key, v),
                    None => std::env::remove_var(key),
                }
            }
        }

        let result = f();

        for (key, prev) in previous {
            // SAFETY: protected by ENV_LOCK mutex; restoring original values
            unsafe {
                match prev {
                    Some(v) => std::env::set_var(key, v),
                    None => std::env::remove_var(key),
                }
            }
        }

        result
    }

    const ALL_MAILER_VARS: [&str; 6] = [
        "SMTP_HOST",
        "SMTP_PORT",
        "SMTP_USER",
        "SMTP_PASSWORD",
        "FROM_NAME",
        "FROM_EMAIL",
    ];

    fn env_with_all_set() -> Vec<(&'static str, Option<&'static str>)> {
        vec![
            ("SMTP_HOST", Some("mail.example.com")),
            ("SMTP_PORT", Some("587")),
            ("SMTP_USER", Some("user@example.com")),
            ("SMTP_PASSWORD", Some("hunter2")),
            ("FROM_NAME", Some("Test App")),
            ("FROM_EMAIL", Some("noreply@example.com")),
        ]
    }

    fn env_with_var_removed(skip: &str) -> Vec<(&'static str, Option<&'static str>)> {
        let mut vars = env_with_all_set();
        for entry in &mut vars {
            if entry.0 == skip {
                entry.1 = None;
            }
        }
        vars
    }

    #[test]
    fn from_env_fails_when_smtp_host_missing() {
        let vars = env_with_var_removed("SMTP_HOST");
        with_env_vars(&vars, || {
            let result = MailerConfig::from_env();
            assert!(result.is_err());
            let err = result.unwrap_err().to_string();
            assert!(
                err.contains("SMTP_HOST"),
                "error should mention SMTP_HOST, got: {err}"
            );
        });
    }

    #[test]
    fn from_env_fails_when_any_required_var_missing() {
        for var_name in &ALL_MAILER_VARS {
            let vars = env_with_var_removed(var_name);
            with_env_vars(&vars, || {
                let result = MailerConfig::from_env();
                assert!(result.is_err(), "expected error when {var_name} is missing");
            });
        }
    }

    #[test]
    fn from_env_fails_with_invalid_port() {
        let mut vars = env_with_all_set();
        for entry in &mut vars {
            if entry.0 == "SMTP_PORT" {
                entry.1 = Some("not_a_number");
            }
        }
        with_env_vars(&vars, || {
            let result = MailerConfig::from_env();
            assert!(result.is_err());
            let err = result.unwrap_err().to_string();
            assert!(
                err.contains("SMTP_PORT"),
                "error should mention SMTP_PORT, got: {err}"
            );
        });
    }

    #[test]
    fn from_env_succeeds_with_all_vars_set() {
        let vars = env_with_all_set();
        with_env_vars(&vars, || {
            let config = MailerConfig::from_env().expect("should succeed");
            assert_eq!(config.smtp_host, "mail.example.com");
            assert_eq!(config.smtp_port, 587);
            assert_eq!(config.smtp_user, "user@example.com");
            assert_eq!(config.smtp_password.expose_secret(), "hunter2");
            assert_eq!(config.from_name, "Test App");
            assert_eq!(config.from_email, "noreply@example.com");
        });
    }

    #[test]
    fn debug_output_redacts_smtp_password() {
        let config = MailerConfig {
            smtp_host: "mail.example.com".to_string(),
            smtp_port: 587,
            smtp_user: "user@example.com".to_string(),
            smtp_password: SecretString::from("super-secret-password"),
            from_name: "Test".to_string(),
            from_email: "test@example.com".to_string(),
        };

        let debug = format!("{config:?}");
        assert!(
            !debug.contains("super-secret-password"),
            "debug output must not contain the SMTP password"
        );
        assert!(
            debug.contains("[REDACTED]"),
            "debug output must show [REDACTED]"
        );
    }

    #[test]
    fn build_message_produces_valid_html_email() {
        let config = MailerConfig {
            smtp_host: "localhost".to_string(),
            smtp_port: 587,
            smtp_user: "user".to_string(),
            smtp_password: SecretString::from("pass"),
            from_name: "Blixt App".to_string(),
            from_email: "noreply@blixt.dev".to_string(),
        };

        let from =
            build_mailbox(&config.from_name, &config.from_email).expect("valid from address");

        let to = "recipient@example.com";
        let to_mailbox: Mailbox = to.parse().expect("valid to address");
        let subject = "Welcome!";
        let body = "<h1>Hello</h1>".to_string();

        let message = Message::builder()
            .from(from.clone())
            .to(to_mailbox)
            .subject(subject)
            .header(ContentType::TEXT_HTML)
            .body(body)
            .expect("valid message");

        let envelope = message.envelope();
        assert_eq!(
            envelope.from().expect("has sender").to_string(),
            "noreply@blixt.dev"
        );
        assert_eq!(envelope.to().len(), 1);
        assert_eq!(envelope.to()[0].to_string(), "recipient@example.com");
    }

    #[test]
    fn build_message_produces_valid_text_email() {
        let from = build_mailbox("Sender", "sender@example.com").expect("valid from address");

        let to_mailbox: Mailbox = "user@example.com".parse().expect("valid to");

        let message = Message::builder()
            .from(from)
            .to(to_mailbox)
            .subject("Plain text test")
            .header(ContentType::TEXT_PLAIN)
            .body("Hello, world!".to_string())
            .expect("valid message");

        let envelope = message.envelope();
        assert_eq!(envelope.to().len(), 1);
    }

    #[test]
    fn invalid_recipient_address_is_rejected() {
        let result: Result<Mailbox, _> = "not-an-email".parse();
        assert!(
            result.is_err(),
            "parsing an invalid email address should fail"
        );
    }

    #[test]
    fn build_mailbox_rejects_invalid_email() {
        let result = build_mailbox("Name", "definitely not an email");
        assert!(result.is_err());
    }

    #[derive(Template)]
    #[template(source = "<h1>Hello, {{ name }}!</h1>", ext = "html")]
    struct TestTemplate<'a> {
        name: &'a str,
    }

    #[test]
    fn askama_template_renders_for_email() {
        let tmpl = TestTemplate { name: "World" };
        let rendered = tmpl.render().expect("template should render");
        assert_eq!(rendered, "<h1>Hello, World!</h1>");
    }
}