openai_realtime/
config.rs

1use std::fmt::Display;
2
3#[derive(Debug, Clone)]
4pub enum ApiKeyRef {
5    Value(String),
6    Env(Option<String>),
7}
8
9impl Display for ApiKeyRef {
10    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
11        match self {
12            ApiKeyRef::Value(key) => write!(f, "{}", key),
13            ApiKeyRef::Env(env) => write!(f, "{}", env.as_deref().unwrap_or("OPENAI_KEY")),
14        }
15    }
16}
17
18impl ApiKeyRef {
19    /// Resolve the Api-Key
20    pub fn api_key(&self) -> String {
21        match &self {
22            ApiKeyRef::Value(key) => key.to_string(),
23            ApiKeyRef::Env(env) => {
24                let env_key = env.clone().unwrap_or("OPENAI_KEY".to_string());
25                std::env::var(env_key).unwrap_or_else(|_| "".to_string())
26            }
27        }
28    }
29}
30
31impl Default for ApiKeyRef {
32    fn default() -> Self {
33        Self::Env("OPENAI_KEY".to_string().into())
34    }
35}