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