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