Skip to main content

capsula_cwd_context/
lib.rs

1mod config;
2mod error;
3
4use crate::config::CwdContextFactory;
5use crate::error::CwdContextError;
6use capsula_core::captured::Captured;
7use capsula_core::context::{Context, ContextFactory, RuntimeParams};
8use capsula_core::error::CoreResult;
9use serde_json::json;
10use std::path::PathBuf;
11
12pub const KEY: &str = "cwd";
13
14#[derive(Debug, Default)]
15pub struct CwdContext;
16
17#[derive(Debug)]
18pub struct CwdCaptured {
19    pub cwd_abs: PathBuf,
20}
21
22impl Context for CwdContext {
23    type Output = CwdCaptured;
24
25    fn run(&self, _params: &RuntimeParams) -> CoreResult<Self::Output> {
26        let cwd_abs = std::env::current_dir().map_err(|source| CwdContextError::CurrentDirError { source })?;
27        Ok(CwdCaptured { cwd_abs })
28    }
29}
30
31impl Captured for CwdCaptured {
32    fn to_json(&self) -> serde_json::Value {
33        json!({
34            "type": KEY.to_string(),
35            "cwd": self.cwd_abs.to_string_lossy(),
36        })
37    }
38}
39
40/// Create a factory for CwdContext
41pub fn create_factory() -> Box<dyn ContextFactory> {
42    Box::new(CwdContextFactory)
43}