Skip to main content

rusty_bubbletea/
environ.rs

1//! Cleanroom Rust port of upstream Go source file: `environ.go`
2//! Upstream Target Tag / Version: `v2.0.8`
3//!
4//! <public-docs>
5//! # Environment Messages
6//!
7//! `EnvMsg` representing program environment variables for local/SSH sessions in Bubble Tea v2.0.8.
8//! </public-docs>
9
10use std::collections::HashMap;
11
12/// EnvMsg represents program environment variables.
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct EnvMsg {
15    /// Environment variables map.
16    pub vars: HashMap<String, String>,
17}
18
19impl EnvMsg {
20    /// Creates a new EnvMsg from key-value pairs.
21    pub fn new(pairs: Vec<(String, String)>) -> Self {
22        let vars = pairs.into_iter().collect();
23        Self { vars }
24    }
25
26    /// Creates an EnvMsg from the process environment.
27    pub fn from_std() -> Self {
28        let vars = std::env::vars().collect();
29        Self { vars }
30    }
31
32    /// Returns value of environment variable or empty string if unset.
33    pub fn getenv(&self, key: &str) -> String {
34        self.vars.get(key).cloned().unwrap_or_default()
35    }
36
37    /// Retrieves value and boolean presence flag.
38    pub fn lookup_env(&self, key: &str) -> (String, bool) {
39        match self.vars.get(key) {
40            Some(v) => (v.clone(), true),
41            None => (String::new(), false),
42        }
43    }
44}