Skip to main content

protonmail_client/
config.rs

1//! IMAP connection configuration
2
3use crate::error::{Error, Result};
4use std::env;
5
6/// IMAP connection configuration for Proton Bridge
7#[derive(Debug, Clone)]
8pub struct ImapConfig {
9    pub host: String,
10    pub port: u16,
11    pub username: String,
12    pub password: String,
13}
14
15impl ImapConfig {
16    /// Load IMAP configuration from environment variables
17    ///
18    /// Reads from `.env` file if present. Required variables:
19    /// - `IMAP_USERNAME`
20    /// - `IMAP_PASSWORD`
21    ///
22    /// Optional (with defaults):
23    /// - `IMAP_HOST` (default: `127.0.0.1`)
24    /// - `IMAP_PORT` (default: `1143`)
25    ///
26    /// # Errors
27    ///
28    /// Returns an error if required environment variables are
29    /// missing or `IMAP_PORT` is not a valid port number.
30    pub fn from_env() -> Result<Self> {
31        dotenvy::dotenv().ok();
32
33        Ok(Self {
34            host: env::var("IMAP_HOST").unwrap_or_else(|_| "127.0.0.1".to_string()),
35            port: env::var("IMAP_PORT")
36                .unwrap_or_else(|_| "1143".to_string())
37                .parse()
38                .map_err(|e| Error::Config(format!("Invalid IMAP_PORT: {e}")))?,
39            username: env::var("IMAP_USERNAME")
40                .map_err(|_| Error::Config("IMAP_USERNAME not set".into()))?,
41            password: env::var("IMAP_PASSWORD")
42                .map_err(|_| Error::Config("IMAP_PASSWORD not set".into()))?,
43        })
44    }
45}