Skip to main content

claude_wrapper/command/
mod.rs

1pub mod agents;
2pub mod auth;
3pub mod auto_mode;
4pub mod doctor;
5pub mod install;
6pub mod marketplace;
7pub mod mcp;
8pub mod plugin;
9pub mod project;
10pub mod query;
11pub mod raw;
12pub mod ultrareview;
13pub mod update;
14pub mod version;
15
16#[cfg(feature = "async")]
17use std::future::Future;
18
19use crate::Claude;
20use crate::error::Result;
21
22/// Trait implemented by all claude CLI command builders.
23///
24/// Each command defines its own `Output` type and builds its argument
25/// list via `args()`. Execution is dispatched through the shared `Claude`
26/// client which provides binary path, environment, and timeout config.
27///
28/// The async `execute` method is only present when the `async` feature
29/// is enabled. In sync-only builds, callers reach the blocking path
30/// via [`ClaudeCommandSyncExt::execute_sync`].
31pub trait ClaudeCommand: Send + Sync {
32    /// The typed result of executing this command.
33    type Output: Send;
34
35    /// Build the CLI argument list for this command.
36    fn args(&self) -> Vec<String>;
37
38    /// Execute the command using the given claude client.
39    #[cfg(feature = "async")]
40    fn execute(&self, claude: &Claude) -> impl Future<Output = Result<Self::Output>> + Send;
41}
42
43/// Blocking `execute_sync` for any command that returns `CommandOutput`.
44///
45/// Most command builders (all except the json-decoding convenience
46/// methods) produce `CommandOutput` — this extension trait gives them
47/// a one-line blocking entry point that routes through
48/// [`crate::exec::run_claude_sync`].
49///
50/// ```no_run
51/// # #[cfg(feature = "sync")]
52/// # {
53/// use claude_wrapper::{Claude, ClaudeCommandSyncExt, VersionCommand};
54///
55/// # fn example() -> claude_wrapper::Result<()> {
56/// let claude = Claude::builder().build()?;
57/// let out = VersionCommand::new().execute_sync(&claude)?;
58/// println!("{}", out.stdout);
59/// # Ok(())
60/// # }
61/// # }
62/// ```
63///
64/// Commands with custom execute paths (e.g. [`crate::QueryCommand`],
65/// which honours `retry_policy`) override this via an inherent method
66/// of the same name — inherent-method resolution wins, so callers
67/// don't need to disambiguate.
68#[cfg(feature = "sync")]
69pub trait ClaudeCommandSyncExt {
70    /// Blocking analog of [`ClaudeCommand::execute`] for commands
71    /// producing `CommandOutput`.
72    fn execute_sync(&self, claude: &Claude) -> Result<crate::exec::CommandOutput>;
73}
74
75#[cfg(feature = "sync")]
76impl<T> ClaudeCommandSyncExt for T
77where
78    T: ClaudeCommand<Output = crate::exec::CommandOutput>,
79{
80    fn execute_sync(&self, claude: &Claude) -> Result<crate::exec::CommandOutput> {
81        crate::exec::run_claude_sync(claude, self.args())
82    }
83}