Skip to main content

capsula_env_context/
lib.rs

1mod config;
2mod error;
3
4use crate::config::EnvVarContextFactory;
5#[allow(unused_imports)]
6use crate::error::EnvContextError;
7use capsula_core::captured::Captured;
8use capsula_core::context::{Context, ContextFactory, RuntimeParams};
9use capsula_core::error::CoreResult;
10
11pub const KEY: &str = "env";
12
13#[derive(Debug)]
14pub struct EnvVarContext {
15    pub name: String,
16}
17
18#[derive(Debug)]
19pub struct EnvVarCaptured {
20    pub name: String,
21    pub value: Option<String>,
22}
23
24impl Context for EnvVarContext {
25    type Output = EnvVarCaptured;
26
27    fn run(&self, _params: &RuntimeParams) -> CoreResult<Self::Output> {
28        let value = std::env::var(&self.name).ok();
29        Ok(EnvVarCaptured {
30            name: self.name.clone(),
31            value,
32        })
33    }
34}
35
36impl Captured for EnvVarCaptured {
37    fn to_json(&self) -> serde_json::Value {
38        serde_json::json!({
39            "type": KEY,
40            "name": self.name,
41            "value": self.value,
42        })
43    }
44}
45
46pub fn create_factory() -> Box<dyn ContextFactory> {
47    Box::new(EnvVarContextFactory)
48}