Skip to main content

larust_support/
config_helper.rs

1use larust_core::Config;
2
3/// Laravel-shaped `config('app.name')` string lookup - a deliberate,
4/// isolated exception to this framework's usual compile-checked config
5/// access (`app.config().app_name`, still available and still preferred
6/// for anything statically known); kept narrow (one match arm per known
7/// key) rather than a general dynamic-config system.
8pub fn config(key: &str) -> Option<String> {
9    lookup(larust_core::config(), key)
10}
11
12/// The actual key→value mapping, factored out from `config()` so it's
13/// testable against a manually-built `Config` without touching
14/// `larust_core::config()`'s process-wide `OnceLock` (only
15/// `Application::new()` can populate that, once per process - not
16/// practical to exercise per-test-case here).
17///
18/// Deliberately excludes `mail.username`/`mail.password` - this helper is
19/// reachable from a `{{ }}` template interpolation, and a credential is
20/// one accidental `{{ config("mail.password") }}` away from being
21/// rendered into a page. `app.config().mail_password` (the compile-checked
22/// path) stays available for anything that legitimately needs it.
23fn lookup(cfg: &Config, key: &str) -> Option<String> {
24    match key {
25        "app.name" => Some(cfg.app_name.clone()),
26        "app.env" => Some(cfg.app_env.clone()),
27        "app.url" => Some(cfg.app_url.clone()),
28        "app.port" => Some(cfg.app_port.to_string()),
29        "app.debug" => Some(cfg.app_debug.to_string()),
30        "session.secure_cookie" => Some(cfg.session_secure_cookie.to_string()),
31        "mail.driver" => Some(cfg.mail_driver.clone()),
32        "mail.host" => Some(cfg.mail_host.clone()),
33        "mail.port" => Some(cfg.mail_port.to_string()),
34        "mail.from_address" => Some(cfg.mail_from_address.clone()),
35        "mail.from_name" => Some(cfg.mail_from_name.clone()),
36        _ => None,
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44    fn test_config() -> Config {
45        Config {
46            app_name: "Test App".to_string(),
47            app_env: "testing".to_string(),
48            app_port: 9999,
49            session_secure_cookie: false,
50            app_debug: true,
51            app_url: "http://example.test".to_string(),
52            app_locale: "en".to_string(),
53            app_fallback_locale: "en".to_string(),
54            api_prefix: "/api".to_string(),
55            mail_driver: "log".to_string(),
56            mail_host: "smtp.example.test".to_string(),
57            mail_port: 2525,
58            mail_username: "user".to_string(),
59            mail_password: "secret".to_string(),
60            mail_encryption: "tls".to_string(),
61            mail_from_address: "hello@example.test".to_string(),
62            mail_from_name: "Test App".to_string(),
63            cache_driver: "database".to_string(),
64            queue_driver: "database".to_string(),
65            log_channel: "stdout".to_string(),
66            log_level: String::new(),
67            log_max_size: 10 * 1024 * 1024,
68            log_keep_files: 5,
69            deploy_type: "web".to_string(),
70            session_driver: "database".to_string(),
71        }
72    }
73
74    #[test]
75    fn returns_known_keys() {
76        let cfg = test_config();
77        assert_eq!(lookup(&cfg, "app.name"), Some("Test App".to_string()));
78        assert_eq!(lookup(&cfg, "app.env"), Some("testing".to_string()));
79        assert_eq!(
80            lookup(&cfg, "app.url"),
81            Some("http://example.test".to_string())
82        );
83        assert_eq!(lookup(&cfg, "app.port"), Some("9999".to_string()));
84        assert_eq!(lookup(&cfg, "app.debug"), Some("true".to_string()));
85        assert_eq!(
86            lookup(&cfg, "session.secure_cookie"),
87            Some("false".to_string())
88        );
89        assert_eq!(lookup(&cfg, "mail.driver"), Some("log".to_string()));
90        assert_eq!(
91            lookup(&cfg, "mail.host"),
92            Some("smtp.example.test".to_string())
93        );
94        assert_eq!(lookup(&cfg, "mail.port"), Some("2525".to_string()));
95        assert_eq!(
96            lookup(&cfg, "mail.from_address"),
97            Some("hello@example.test".to_string())
98        );
99        assert_eq!(lookup(&cfg, "mail.from_name"), Some("Test App".to_string()));
100    }
101
102    #[test]
103    fn does_not_expose_mail_credentials() {
104        let cfg = test_config();
105        assert_eq!(lookup(&cfg, "mail.username"), None);
106        assert_eq!(lookup(&cfg, "mail.password"), None);
107    }
108
109    #[test]
110    fn returns_none_for_an_unknown_key() {
111        assert_eq!(lookup(&test_config(), "app.nonexistent"), None);
112    }
113}