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