standout-dispatch 7.4.0

Command dispatch and routing for clap-based CLIs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//! Hook system for pre/post command execution.
//!
//! Hooks allow you to run custom code at specific points in the dispatch pipeline.
//! They enable cross-cutting concerns (logging, validation, transformation) without
//! polluting handler logic.
//!
//! # Pipeline Position
//!
//! Hooks fit into the dispatch flow as follows:
//!
//! ```text
//! parsed CLI args
//!   → PRE-DISPATCH HOOK ← (validation, auth checks, setup)
//!   → logic handler
//!   → POST-DISPATCH HOOK ← (data transformation, enrichment)
//!   → render handler
//!   → POST-OUTPUT HOOK ← (output transformation, logging)
//! ```
//!
//! # Hook Points
//!
//! - Pre-dispatch: Runs before the command handler. Can abort execution.
//!   Use for: authentication, input validation, resource acquisition.
//!
//! - Post-dispatch: Runs after the handler but before rendering. Receives the raw
//!   handler data as `serde_json::Value`. Can inspect, modify, or replace the data.
//!   Use for: adding metadata, data transformation, caching.
//!
//! - Post-output: Runs after output is generated. Can transform output or abort.
//!   Use for: logging, clipboard copy, output filtering.

use std::fmt;
use std::rc::Rc;
use thiserror::Error;

use crate::handler::CommandContext;
use clap::ArgMatches;

/// Text output with both formatted and raw versions.
///
/// This struct carries both the terminal-formatted output (with ANSI codes)
/// and the raw output (with style tags but no ANSI codes). This allows
/// post-output hooks like piping to choose the appropriate version.
#[derive(Debug, Clone)]
pub struct TextOutput {
    /// The formatted output with ANSI codes applied (for terminal display)
    pub formatted: String,
    /// The raw output with `[tag]...[/tag]` markers but no ANSI codes.
    /// This is the intermediate output after template rendering but before
    /// style tag processing. Piping uses this by default.
    pub raw: String,
}

impl TextOutput {
    /// Creates a new TextOutput with both formatted and raw versions.
    pub fn new(formatted: String, raw: String) -> Self {
        Self { formatted, raw }
    }

    /// Creates a TextOutput where formatted and raw are the same.
    /// Use this for output that doesn't go through style tag processing
    /// (e.g., JSON output, error messages).
    pub fn plain(text: String) -> Self {
        Self {
            formatted: text.clone(),
            raw: text,
        }
    }
}

/// Output from a command, used in post-output hooks.
///
/// This represents the final output from a command handler after rendering.
#[derive(Debug, Clone)]
pub enum RenderedOutput {
    /// Text output with both formatted (ANSI) and raw versions.
    /// The `formatted` field contains ANSI codes for terminal display.
    /// The `raw` field contains the intermediate output for piping.
    Text(TextOutput),
    /// Binary output with suggested filename
    Binary(Vec<u8>, String),
    /// No output (silent command)
    Silent,
}

impl RenderedOutput {
    /// Returns true if this is text output.
    pub fn is_text(&self) -> bool {
        matches!(self, RenderedOutput::Text(_))
    }

    /// Returns true if this is binary output.
    pub fn is_binary(&self) -> bool {
        matches!(self, RenderedOutput::Binary(_, _))
    }

    /// Returns true if this is silent (no output).
    pub fn is_silent(&self) -> bool {
        matches!(self, RenderedOutput::Silent)
    }

    /// Returns the formatted text content (with ANSI codes) if this is text output.
    pub fn as_text(&self) -> Option<&str> {
        match self {
            RenderedOutput::Text(t) => Some(&t.formatted),
            _ => None,
        }
    }

    /// Returns the raw text content (without ANSI codes) if this is text output.
    /// This is the intermediate output suitable for piping.
    pub fn as_raw_text(&self) -> Option<&str> {
        match self {
            RenderedOutput::Text(t) => Some(&t.raw),
            _ => None,
        }
    }

    /// Returns the full TextOutput if this is text output.
    pub fn as_text_output(&self) -> Option<&TextOutput> {
        match self {
            RenderedOutput::Text(t) => Some(t),
            _ => None,
        }
    }

    /// Returns the binary content and filename if this is binary output.
    pub fn as_binary(&self) -> Option<(&[u8], &str)> {
        match self {
            RenderedOutput::Binary(bytes, filename) => Some((bytes, filename)),
            _ => None,
        }
    }
}

/// The phase at which a hook error occurred.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HookPhase {
    /// Error occurred during pre-dispatch phase
    PreDispatch,
    /// Error occurred during post-dispatch phase
    PostDispatch,
    /// Error occurred during post-output phase
    PostOutput,
}

impl fmt::Display for HookPhase {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            HookPhase::PreDispatch => write!(f, "pre-dispatch"),
            HookPhase::PostDispatch => write!(f, "post-dispatch"),
            HookPhase::PostOutput => write!(f, "post-output"),
        }
    }
}

/// Error returned by a hook.
#[derive(Debug, Error)]
#[error("hook error ({phase}): {message}")]
pub struct HookError {
    /// Human-readable error message
    pub message: String,
    /// The hook phase where the error occurred
    pub phase: HookPhase,
    /// The underlying error source, if any
    #[source]
    pub source: Option<Box<dyn std::error::Error + Send + Sync + 'static>>,
}

impl HookError {
    /// Creates a new hook error for the pre-dispatch phase.
    pub fn pre_dispatch(message: impl Into<String>) -> Self {
        Self {
            message: message.into(),
            phase: HookPhase::PreDispatch,
            source: None,
        }
    }

    /// Creates a new hook error for the post-dispatch phase.
    pub fn post_dispatch(message: impl Into<String>) -> Self {
        Self {
            message: message.into(),
            phase: HookPhase::PostDispatch,
            source: None,
        }
    }

    /// Creates a new hook error for the post-output phase.
    pub fn post_output(message: impl Into<String>) -> Self {
        Self {
            message: message.into(),
            phase: HookPhase::PostOutput,
            source: None,
        }
    }

    /// Sets the source error.
    pub fn with_source<E>(mut self, source: E) -> Self
    where
        E: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
    {
        self.source = Some(source.into());
        self
    }
}

/// Type alias for pre-dispatch hook functions.
///
/// Pre-dispatch hooks receive mutable access to [`CommandContext`], allowing them
/// to inject state into `ctx.extensions` that handlers can retrieve.
pub type PreDispatchFn = Rc<dyn Fn(&ArgMatches, &mut CommandContext) -> Result<(), HookError>>;

/// Type alias for post-dispatch hook functions.
pub type PostDispatchFn = Rc<
    dyn Fn(&ArgMatches, &CommandContext, serde_json::Value) -> Result<serde_json::Value, HookError>,
>;

/// Type alias for post-output hook functions.
pub type PostOutputFn =
    Rc<dyn Fn(&ArgMatches, &CommandContext, RenderedOutput) -> Result<RenderedOutput, HookError>>;

/// Per-command hook configuration.
///
/// Hooks are registered per-command path and executed in order.
#[derive(Clone, Default)]
pub struct Hooks {
    pre_dispatch: Vec<PreDispatchFn>,
    post_dispatch: Vec<PostDispatchFn>,
    post_output: Vec<PostOutputFn>,
}

impl Hooks {
    /// Creates a new empty hooks configuration.
    pub fn new() -> Self {
        Self::default()
    }

    /// Returns true if no hooks are registered.
    pub fn is_empty(&self) -> bool {
        self.pre_dispatch.is_empty() && self.post_dispatch.is_empty() && self.post_output.is_empty()
    }

    /// Adds a pre-dispatch hook.
    ///
    /// Pre-dispatch hooks receive mutable access to [`CommandContext`], allowing
    /// state injection via `ctx.extensions`. Handlers can then retrieve this state.
    ///
    /// # Example
    ///
    /// ```rust
    /// use standout_dispatch::{Hooks, HookError};
    ///
    /// struct ApiClient { base_url: String }
    ///
    /// let hooks = Hooks::new()
    ///     .pre_dispatch(|_matches, ctx| {
    ///         ctx.extensions.insert(ApiClient {
    ///             base_url: "https://api.example.com".into()
    ///         });
    ///         Ok(())
    ///     });
    /// ```
    pub fn pre_dispatch<F>(mut self, f: F) -> Self
    where
        F: Fn(&ArgMatches, &mut CommandContext) -> Result<(), HookError> + 'static,
    {
        self.pre_dispatch.push(Rc::new(f));
        self
    }

    /// Adds a post-dispatch hook.
    pub fn post_dispatch<F>(mut self, f: F) -> Self
    where
        F: Fn(
                &ArgMatches,
                &CommandContext,
                serde_json::Value,
            ) -> Result<serde_json::Value, HookError>
            + 'static,
    {
        self.post_dispatch.push(Rc::new(f));
        self
    }

    /// Adds a post-output hook.
    pub fn post_output<F>(mut self, f: F) -> Self
    where
        F: Fn(&ArgMatches, &CommandContext, RenderedOutput) -> Result<RenderedOutput, HookError>
            + 'static,
    {
        self.post_output.push(Rc::new(f));
        self
    }

    /// Runs all pre-dispatch hooks.
    ///
    /// Hooks receive mutable access to the context, allowing state injection.
    pub fn run_pre_dispatch(
        &self,
        matches: &ArgMatches,
        ctx: &mut CommandContext,
    ) -> Result<(), HookError> {
        for hook in &self.pre_dispatch {
            hook(matches, ctx)?;
        }
        Ok(())
    }

    /// Runs all post-dispatch hooks, chaining transformations.
    pub fn run_post_dispatch(
        &self,
        matches: &ArgMatches,
        ctx: &CommandContext,
        data: serde_json::Value,
    ) -> Result<serde_json::Value, HookError> {
        let mut current = data;
        for hook in &self.post_dispatch {
            current = hook(matches, ctx, current)?;
        }
        Ok(current)
    }

    /// Runs all post-output hooks, chaining transformations.
    pub fn run_post_output(
        &self,
        matches: &ArgMatches,
        ctx: &CommandContext,
        output: RenderedOutput,
    ) -> Result<RenderedOutput, HookError> {
        let mut current = output;
        for hook in &self.post_output {
            current = hook(matches, ctx, current)?;
        }
        Ok(current)
    }
}

impl fmt::Debug for Hooks {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Hooks")
            .field("pre_dispatch_count", &self.pre_dispatch.len())
            .field("post_dispatch_count", &self.post_dispatch.len())
            .field("post_output_count", &self.post_output.len())
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn test_context() -> CommandContext {
        CommandContext {
            command_path: vec!["test".into()],
            ..Default::default()
        }
    }

    fn test_matches() -> ArgMatches {
        clap::Command::new("test").get_matches_from(vec!["test"])
    }

    #[test]
    fn test_rendered_output_variants() {
        let text = RenderedOutput::Text(TextOutput::new("formatted".into(), "raw".into()));
        assert!(text.is_text());
        assert!(!text.is_binary());
        assert!(!text.is_silent());
        assert_eq!(text.as_text(), Some("formatted"));
        assert_eq!(text.as_raw_text(), Some("raw"));

        // Test plain constructor (formatted == raw)
        let plain = RenderedOutput::Text(TextOutput::plain("hello".into()));
        assert_eq!(plain.as_text(), Some("hello"));
        assert_eq!(plain.as_raw_text(), Some("hello"));

        let binary = RenderedOutput::Binary(vec![1, 2, 3], "file.bin".into());
        assert!(!binary.is_text());
        assert!(binary.is_binary());
        assert_eq!(binary.as_binary(), Some((&[1u8, 2, 3][..], "file.bin")));

        let silent = RenderedOutput::Silent;
        assert!(silent.is_silent());
    }

    #[test]
    fn test_hook_error_creation() {
        let err = HookError::pre_dispatch("test error");
        assert_eq!(err.phase, HookPhase::PreDispatch);
        assert_eq!(err.message, "test error");
    }

    #[test]
    fn test_hooks_empty() {
        let hooks = Hooks::new();
        assert!(hooks.is_empty());
    }

    #[test]
    fn test_pre_dispatch_success() {
        use std::cell::Cell;
        use std::rc::Rc;

        let called = Rc::new(Cell::new(false));
        let called_clone = called.clone();

        let hooks = Hooks::new().pre_dispatch(move |_, _| {
            called_clone.set(true);
            Ok(())
        });

        let mut ctx = test_context();
        let matches = test_matches();
        let result = hooks.run_pre_dispatch(&matches, &mut ctx);

        assert!(result.is_ok());
        assert!(called.get());
    }

    #[test]
    fn test_pre_dispatch_error_aborts() {
        let hooks = Hooks::new()
            .pre_dispatch(|_, _| Err(HookError::pre_dispatch("first fails")))
            .pre_dispatch(|_, _| panic!("should not be called"));

        let mut ctx = test_context();
        let matches = test_matches();
        let result = hooks.run_pre_dispatch(&matches, &mut ctx);

        assert!(result.is_err());
    }

    #[test]
    fn test_pre_dispatch_injects_extensions() {
        struct TestState {
            value: i32,
        }

        let hooks = Hooks::new().pre_dispatch(|_, ctx| {
            ctx.extensions.insert(TestState { value: 42 });
            Ok(())
        });

        let mut ctx = test_context();
        let matches = test_matches();

        // Before hook runs, extension is not present
        assert!(!ctx.extensions.contains::<TestState>());

        hooks.run_pre_dispatch(&matches, &mut ctx).unwrap();

        // After hook runs, extension is available
        let state = ctx.extensions.get::<TestState>().unwrap();
        assert_eq!(state.value, 42);
    }

    #[test]
    fn test_pre_dispatch_multiple_hooks_share_context() {
        struct Counter {
            count: i32,
        }

        let hooks = Hooks::new()
            .pre_dispatch(|_, ctx| {
                ctx.extensions.insert(Counter { count: 1 });
                Ok(())
            })
            .pre_dispatch(|_, ctx| {
                // Second hook can read and modify what first hook inserted
                if let Some(counter) = ctx.extensions.get_mut::<Counter>() {
                    counter.count += 10;
                }
                Ok(())
            });

        let mut ctx = test_context();
        let matches = test_matches();
        hooks.run_pre_dispatch(&matches, &mut ctx).unwrap();

        let counter = ctx.extensions.get::<Counter>().unwrap();
        assert_eq!(counter.count, 11);
    }

    #[test]
    fn test_post_dispatch_transformation() {
        use serde_json::json;

        let hooks = Hooks::new().post_dispatch(|_, _, mut data| {
            if let Some(obj) = data.as_object_mut() {
                obj.insert("modified".into(), json!(true));
            }
            Ok(data)
        });

        let ctx = test_context();
        let matches = test_matches();
        let data = json!({"value": 42});
        let result = hooks.run_post_dispatch(&matches, &ctx, data);

        assert!(result.is_ok());
        let output = result.unwrap();
        assert_eq!(output["value"], 42);
        assert_eq!(output["modified"], true);
    }

    #[test]
    fn test_post_output_transformation() {
        let hooks = Hooks::new().post_output(|_, _, output| {
            if let RenderedOutput::Text(text_output) = output {
                Ok(RenderedOutput::Text(TextOutput::new(
                    text_output.formatted.to_uppercase(),
                    text_output.raw.to_uppercase(),
                )))
            } else {
                Ok(output)
            }
        });

        let ctx = test_context();
        let matches = test_matches();
        let input = RenderedOutput::Text(TextOutput::plain("hello".into()));
        let result = hooks.run_post_output(&matches, &ctx, input);

        assert!(result.is_ok());
        assert_eq!(result.unwrap().as_text(), Some("HELLO"));
    }
}