Skip to main content

codewhale_command_contract/
handler.rs

1//! Generic handler transport for staged command migration.
2//!
3//! The output type is generic so FEAT-014 does not move or duplicate the
4//! TUI-owned `CommandResult`. During in-place adoption, the TUI instantiates
5//! `CommandHandler<crate::commands::CommandResult>`.
6
7use crate::facets::{
8    CommandCostContext, CommandMediaContext, CommandModePolicyContext, CommandModelContext,
9    CommandPresentationContext, CommandSessionContext, CommandSkillsContext,
10    CommandSystemPromptContext, CommandWorkspaceContext,
11};
12
13/// A command handler that is either argument-only or capability-scoped.
14#[derive(Clone, Copy)]
15pub enum CommandHandler<R> {
16    Pure(fn(Option<&str>) -> R),
17    Contextual(fn(CommandContexts<'_>, Option<&str>) -> R),
18}
19
20/// Transport envelope with one independently optional facet slot.
21pub struct CommandContexts<'a> {
22    session: Option<&'a mut dyn CommandSessionContext>,
23    model: Option<&'a mut dyn CommandModelContext>,
24    cost: Option<&'a mut dyn CommandCostContext>,
25    mode_policy: Option<&'a mut dyn CommandModePolicyContext>,
26    system_prompt: Option<&'a mut dyn CommandSystemPromptContext>,
27    skills: Option<&'a mut dyn CommandSkillsContext>,
28    workspace: Option<&'a mut dyn CommandWorkspaceContext>,
29    presentation: Option<&'a mut dyn CommandPresentationContext>,
30    media: Option<&'a mut dyn CommandMediaContext>,
31}
32
33/// Consumed envelope used when one handler needs several independent facets.
34pub struct ContextParts<'a> {
35    pub session: Option<&'a mut dyn CommandSessionContext>,
36    pub model: Option<&'a mut dyn CommandModelContext>,
37    pub cost: Option<&'a mut dyn CommandCostContext>,
38    pub mode_policy: Option<&'a mut dyn CommandModePolicyContext>,
39    pub system_prompt: Option<&'a mut dyn CommandSystemPromptContext>,
40    pub skills: Option<&'a mut dyn CommandSkillsContext>,
41    pub workspace: Option<&'a mut dyn CommandWorkspaceContext>,
42    pub presentation: Option<&'a mut dyn CommandPresentationContext>,
43    pub media: Option<&'a mut dyn CommandMediaContext>,
44}
45
46impl<'a> CommandContexts<'a> {
47    pub fn empty() -> Self {
48        Self {
49            session: None,
50            model: None,
51            cost: None,
52            mode_policy: None,
53            system_prompt: None,
54            skills: None,
55            workspace: None,
56            presentation: None,
57            media: None,
58        }
59    }
60
61    pub fn into_parts(self) -> ContextParts<'a> {
62        ContextParts {
63            session: self.session,
64            model: self.model,
65            cost: self.cost,
66            mode_policy: self.mode_policy,
67            system_prompt: self.system_prompt,
68            skills: self.skills,
69            workspace: self.workspace,
70            presentation: self.presentation,
71            media: self.media,
72        }
73    }
74
75    pub fn with_session(mut self, value: &'a mut dyn CommandSessionContext) -> Self {
76        assert!(
77            self.session.replace(value).is_none(),
78            "session facet already set"
79        );
80        self
81    }
82
83    pub fn with_model(mut self, value: &'a mut dyn CommandModelContext) -> Self {
84        assert!(
85            self.model.replace(value).is_none(),
86            "model facet already set"
87        );
88        self
89    }
90
91    pub fn with_cost(mut self, value: &'a mut dyn CommandCostContext) -> Self {
92        assert!(self.cost.replace(value).is_none(), "cost facet already set");
93        self
94    }
95
96    pub fn with_mode_policy(mut self, value: &'a mut dyn CommandModePolicyContext) -> Self {
97        assert!(
98            self.mode_policy.replace(value).is_none(),
99            "mode-policy facet already set"
100        );
101        self
102    }
103
104    pub fn with_system_prompt(mut self, value: &'a mut dyn CommandSystemPromptContext) -> Self {
105        assert!(
106            self.system_prompt.replace(value).is_none(),
107            "system-prompt facet already set"
108        );
109        self
110    }
111
112    pub fn with_skills(mut self, value: &'a mut dyn CommandSkillsContext) -> Self {
113        assert!(
114            self.skills.replace(value).is_none(),
115            "skills facet already set"
116        );
117        self
118    }
119
120    pub fn with_workspace(mut self, value: &'a mut dyn CommandWorkspaceContext) -> Self {
121        assert!(
122            self.workspace.replace(value).is_none(),
123            "workspace facet already set"
124        );
125        self
126    }
127
128    pub fn with_presentation(mut self, value: &'a mut dyn CommandPresentationContext) -> Self {
129        assert!(
130            self.presentation.replace(value).is_none(),
131            "presentation facet already set"
132        );
133        self
134    }
135
136    pub fn with_media(mut self, value: &'a mut dyn CommandMediaContext) -> Self {
137        assert!(
138            self.media.replace(value).is_none(),
139            "media facet already set"
140        );
141        self
142    }
143}
144
145impl Default for CommandContexts<'_> {
146    fn default() -> Self {
147        Self::empty()
148    }
149}