gadk_platform/env.rs
1// Copyright 2026 Chris Olson
2// Portions derived from ADK-Go (https://github.com/google/adk-go), Copyright 2026 Google LLC.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Environment variable access, injectable for tests.
17//!
18//! Moved here from gadk-gemini so every model backend shares it; gadk-gemini re-exports it
19//! under the old paths (`gadk_gemini::{EnvProvider, MapEnv, ProcessEnv}`).
20
21use std::collections::HashMap;
22
23/// Source of environment variables used by model backends' configuration resolution.
24pub trait EnvProvider: Send + Sync {
25 /// Returns the value of `key`, or `None` if it is not set. A variable set to the empty
26 /// string returns `Some("")`: a variable that is set but empty counts as present.
27 fn var(&self, key: &str) -> Option<String>;
28}
29
30/// Reads the process environment.
31#[derive(Debug, Clone, Copy, Default)]
32pub struct ProcessEnv;
33
34impl EnvProvider for ProcessEnv {
35 fn var(&self, key: &str) -> Option<String> {
36 std::env::var(key).ok()
37 }
38}
39
40/// A fixed map of variables, for tests.
41#[derive(Debug, Clone, Default)]
42pub struct MapEnv(pub HashMap<String, String>);
43
44impl EnvProvider for MapEnv {
45 fn var(&self, key: &str) -> Option<String> {
46 self.0.get(key).cloned()
47 }
48}
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn map_env_reports_presence_including_empty_values() {
56 let env = MapEnv(HashMap::from([
57 ("SET".to_string(), "v".to_string()),
58 ("EMPTY".to_string(), String::new()),
59 ]));
60 assert_eq!(env.var("SET").as_deref(), Some("v"));
61 assert_eq!(env.var("EMPTY").as_deref(), Some(""));
62 assert_eq!(env.var("UNSET"), None);
63 }
64
65 #[test]
66 fn process_env_misses_an_unset_variable() {
67 assert_eq!(
68 ProcessEnv.var("GADK_PLATFORM_ENV_TEST_SURELY_UNSET_7F3A"),
69 None
70 );
71 }
72}