spec_driven_docs/context.rs
1//! Shared per-invocation context handed to every command handler.
2//!
3//! Holds only what every handler needs: where the process runs and how loud
4//! it should be. Configuration files, color policy, and runtimes are
5//! deliberately absent — this binary has none.
6
7use camino::Utf8PathBuf;
8
9use crate::error::AppError;
10
11/// The resolved invocation environment.
12#[derive(Debug, Clone)]
13pub struct AppContext {
14 /// The working directory the process was invoked from.
15 pub cwd: Utf8PathBuf,
16 /// The `-v` flag count.
17 pub verbosity: u8,
18}
19
20impl AppContext {
21 /// Resolve the context from the process environment.
22 ///
23 /// # Errors
24 ///
25 /// [`AppError::Io`] when the working directory cannot be read, and
26 /// [`AppError::Usage`] when its path is not UTF-8.
27 pub fn new(verbosity: u8) -> Result<Self, AppError> {
28 let cwd = std::env::current_dir()?;
29 let cwd = Utf8PathBuf::from_path_buf(cwd).map_err(|p| {
30 AppError::Usage(format!("working directory is not UTF-8: {}", p.display()))
31 })?;
32 Ok(Self { cwd, verbosity })
33 }
34}