Skip to main content

commit_wizard/core/
context.rs

1//! Core application context and state management.
2//!
3//! The [`Context`] struct holds the application state including runtime configuration,
4//! mode settings, and provides methods to access capabilities and perform operations.
5
6use std::path::PathBuf;
7
8use crate::{
9    engine::{
10        Error,
11        models::{
12            policy::Policy,
13            runtime::{AvailableConfigOptions, ResolvedConfig, Runtime, mode::RunMode},
14        },
15    },
16    infra::{git::Git, ui::Ui},
17};
18
19pub type AppResult<T> = Result<T, Error>;
20
21#[derive(Debug)]
22pub struct Context {
23    runtime: Runtime,
24}
25
26impl Context {
27    pub fn new(runtime: Runtime) -> Self {
28        Self { runtime }
29    }
30
31    pub fn runtime(&self) -> &Runtime {
32        &self.runtime
33    }
34
35    pub fn set_workdir(&mut self, path: PathBuf) {
36        self.runtime.set_cwd(path);
37    }
38
39    pub fn set_run_mode(&mut self, ci: bool, non_interactive: bool) {
40        self.runtime
41            .set_mode(RunMode::from_flags(ci, non_interactive));
42    }
43
44    pub(crate) fn set_in_git_repo(&mut self, value: bool) {
45        self.runtime.set_in_git_repo(value);
46    }
47
48    pub(crate) fn set_repo_root(&mut self, path: PathBuf) {
49        self.runtime.set_repo_root(path);
50    }
51
52    pub(crate) fn resolve_available_sources(&mut self) -> crate::engine::error::Result<()> {
53        let ui = self.ui();
54        self.runtime.resolve_available_sources(&ui)
55    }
56
57    pub(crate) fn resolve_active_config(&mut self) -> crate::engine::error::Result<()> {
58        let ui = self.ui();
59        self.runtime.resolve_active_config(&ui)
60    }
61
62    pub fn is_interactive(&self) -> bool {
63        self.runtime.is_interactive()
64    }
65
66    pub fn cwd(&self) -> &PathBuf {
67        self.runtime.cwd()
68    }
69
70    pub fn repo_root(&self) -> &PathBuf {
71        self.runtime.repo_root()
72    }
73
74    pub fn dry_run(&self) -> bool {
75        self.runtime.options().dry_run()
76    }
77
78    pub fn auto_yes(&self) -> bool {
79        self.runtime.options().auto_yes()
80    }
81
82    pub fn force(&self) -> bool {
83        self.runtime.options().force()
84    }
85
86    pub fn in_git_repo(&self) -> bool {
87        self.runtime.in_git_repo()
88    }
89
90    pub fn config(&self) -> Option<&ResolvedConfig> {
91        self.runtime.config()
92    }
93    pub fn explicit_config_path(&self) -> Option<&PathBuf> {
94        self.runtime.explicit_config_path()
95    }
96
97    pub fn project_config_path(&self) -> Option<PathBuf> {
98        self.runtime.project_config_path()
99    }
100
101    pub fn sources(&self) -> &AvailableConfigOptions {
102        self.runtime.sources()
103    }
104
105    pub fn policy(&self) -> &Policy {
106        self.runtime.policy()
107    }
108
109    pub fn ui_config(&self) -> scriba::Config {
110        self.runtime.output_config()
111    }
112
113    pub fn ui(&self) -> Ui {
114        Ui::cached_with_config(self.ui_config(), self.runtime.options().output_envelope())
115    }
116
117    pub fn git(&self) -> Git {
118        Git::new(self.cwd().clone())
119    }
120}