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