standout-dispatch 7.9.2

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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
//! Command handler types.
//!
//! This module provides the core types for building CLI handler adapters in the
//! dispatch pipeline.
//!
//! # Design Rationale
//!
//! Handler adapters are responsible for the CLI-to-application seam. They:
//!
//! - Receive parsed CLI arguments (`&ArgMatches`) and execution context
//! - Call a CLI-free application library
//! - Map library results into serializable CLI view data for the render handler
//!
//! Handlers explicitly do not handle:
//! - Output formatting (that's the render handler's job)
//! - Template selection (that's configured at the framework level)
//! - Theme/style decisions (that's the render handler's job)
//!
//! This separation keeps handlers focused and testable - you can unit test
//! a handler by checking the data it returns, without worrying about rendering.
//!
//! # State Management: App State vs Extensions
//!
//! [`CommandContext`] provides two mechanisms for state injection:
//!
//! | Field | Mutability | Lifetime | Purpose |
//! |-------|------------|----------|---------|
//! | `app_state` | Immutable (`&`) | App lifetime (shared via Arc) | Database, Config, API clients |
//! | `extensions` | Mutable (`&mut`) | Request lifetime | Per-request state, user scope |
//!
//! **App State** is configured at app build time via `AppBuilder::app_state()` and shared
//! immutably across all command invocations. Use for long-lived resources:
//!
//! ```rust,ignore
//! // At app build time
//! App::builder()
//!     .app_state(Database::connect()?)
//!     .app_state(Config::load()?)
//!     .build()?
//!
//! // In handlers
//! fn list_handler(matches: &ArgMatches, ctx: &CommandContext) -> HandlerResult<Vec<User>> {
//!     let db = ctx.app_state.get_required::<Database>()?;
//!     Ok(Output::Render(db.list_users()?))
//! }
//! ```
//!
//! **Extensions** are injected per-request by pre-dispatch hooks. Use for request-scoped data:
//!
//! ```rust,ignore
//! Hooks::new().pre_dispatch(|matches, ctx| {
//!     let user_id = matches.get_one::<String>("user").unwrap();
//!     ctx.extensions.insert(UserScope { user_id: user_id.clone() });
//!     Ok(())
//! })
//! ```
//!
//! # Core Types
//!
//! - [`CommandContext`]: Environment information passed to handlers
//! - [`Extensions`]: Type-safe container for injecting custom state
//! - [`Output`]: What a handler produces (render data, silent, or binary)
//! - [`HandlerResult`]: The result type for handlers (`Result<Output<T>, Error>`)
//! - [`RunResult`]: The result of running the CLI dispatcher
//! - [`Handler`]: Trait for command handlers (`&mut self`)

use crate::artifact::{Artifact, ArtifactRun};
use crate::hooks::HookPhase;
use crate::verify::ExpectedArg;
use clap::ArgMatches;
use serde::Serialize;
use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::fmt;
use std::rc::Rc;
use std::sync::Arc;

/// Type-safe container for injecting custom state into handlers.
///
/// Extensions allow pre-dispatch hooks to inject state that handlers can retrieve.
/// This enables dependency injection without modifying handler signatures.
///
/// # Warning: Clone Behavior
///
/// `Extensions` is **not** cloned when the container is cloned. Cloning an `Extensions` instance
/// results in a new, empty map. This is because the underlying `Box<dyn Any>` values cannot
/// be cloned generically.
///
/// If you need to share state across threads/clones, use `Arc<T>` inside the extension.
///
/// # Example
///
/// ```rust
/// use standout_dispatch::{Extensions, CommandContext};
///
/// // Define your state types
/// struct ApiClient { base_url: String }
/// struct UserScope { user_id: u64 }
///
/// // In a pre-dispatch hook, inject state
/// let mut ctx = CommandContext::default();
/// ctx.extensions.insert(ApiClient { base_url: "https://api.example.com".into() });
/// ctx.extensions.insert(UserScope { user_id: 42 });
///
/// // In a handler, retrieve state
/// let api = ctx.extensions.get_required::<ApiClient>()?;
/// println!("API base: {}", api.base_url);
/// # Ok::<(), anyhow::Error>(())
/// ```
#[derive(Default)]
pub struct Extensions {
    map: HashMap<TypeId, Box<dyn Any>>,
}

impl Extensions {
    /// Creates a new empty extensions container.
    pub fn new() -> Self {
        Self::default()
    }

    /// Inserts a value into the extensions.
    ///
    /// If a value of this type already exists, it is replaced and returned.
    pub fn insert<T: 'static>(&mut self, val: T) -> Option<T> {
        self.map
            .insert(TypeId::of::<T>(), Box::new(val))
            .and_then(|boxed| boxed.downcast().ok().map(|b| *b))
    }

    /// Gets a reference to a value of the specified type.
    ///
    /// Returns `None` if no value of this type exists.
    pub fn get<T: 'static>(&self) -> Option<&T> {
        self.map
            .get(&TypeId::of::<T>())
            .and_then(|boxed| boxed.downcast_ref())
    }

    /// Gets a mutable reference to a value of the specified type.
    ///
    /// Returns `None` if no value of this type exists.
    pub fn get_mut<T: 'static>(&mut self) -> Option<&mut T> {
        self.map
            .get_mut(&TypeId::of::<T>())
            .and_then(|boxed| boxed.downcast_mut())
    }

    /// Gets a required reference to a value of the specified type.
    ///
    /// Returns an error if no value of this type exists.
    pub fn get_required<T: 'static>(&self) -> Result<&T, anyhow::Error> {
        self.get::<T>().ok_or_else(|| {
            anyhow::anyhow!(
                "Extension missing: type {} not found in context",
                std::any::type_name::<T>()
            )
        })
    }

    /// Gets a required mutable reference to a value of the specified type.
    ///
    /// Returns an error if no value of this type exists.
    pub fn get_mut_required<T: 'static>(&mut self) -> Result<&mut T, anyhow::Error> {
        self.get_mut::<T>().ok_or_else(|| {
            anyhow::anyhow!(
                "Extension missing: type {} not found in context",
                std::any::type_name::<T>()
            )
        })
    }

    /// Removes a value of the specified type, returning it if it existed.
    pub fn remove<T: 'static>(&mut self) -> Option<T> {
        self.map
            .remove(&TypeId::of::<T>())
            .and_then(|boxed| boxed.downcast().ok().map(|b| *b))
    }

    /// Returns `true` if the extensions contain a value of the specified type.
    pub fn contains<T: 'static>(&self) -> bool {
        self.map.contains_key(&TypeId::of::<T>())
    }

    /// Returns the number of extensions stored.
    pub fn len(&self) -> usize {
        self.map.len()
    }

    /// Returns `true` if no extensions are stored.
    pub fn is_empty(&self) -> bool {
        self.map.is_empty()
    }

    /// Removes all extensions.
    pub fn clear(&mut self) {
        self.map.clear();
    }
}

impl fmt::Debug for Extensions {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Extensions")
            .field("len", &self.map.len())
            .finish_non_exhaustive()
    }
}

impl Clone for Extensions {
    fn clone(&self) -> Self {
        // Extensions cannot be cloned because Box<dyn Any> isn't Clone.
        // Return empty extensions on clone - this is a limitation but
        // matches the behavior of http::Extensions.
        Self::new()
    }
}

/// Context passed to command handlers.
///
/// Provides information about the execution environment plus two mechanisms
/// for state injection:
///
/// - **`app_state`**: Immutable, app-lifetime state (Database, Config, API clients)
/// - **`extensions`**: Mutable, per-request state (UserScope, RequestId)
///
/// Note that output format is deliberately not included here - format decisions
/// are made by the render handler, not by logic handlers.
///
/// # App State (Immutable, Shared)
///
/// App state is configured at build time and shared across all dispatches:
///
/// ```rust,ignore
/// use standout::cli::App;
///
/// struct Database { /* ... */ }
/// struct Config { api_url: String }
///
/// App::builder()
///     .app_state(Database::connect()?)
///     .app_state(Config { api_url: "https://api.example.com".into() })
///     .command("list", list_handler, "{{ items }}")
///     .build()?
/// ```
///
/// Handlers retrieve app state immutably:
///
/// ```rust,ignore
/// fn list_handler(matches: &ArgMatches, ctx: &CommandContext) -> HandlerResult<Vec<Item>> {
///     let db = ctx.app_state.get_required::<Database>()?;
///     let config = ctx.app_state.get_required::<Config>()?;
///     Ok(Output::Render(db.list_items(&config.api_url)?))
/// }
/// ```
///
/// ## Shared Mutable State
///
/// Since `app_state` is shared via `Arc`, it is immutable by default. To share mutable state
/// (like counters or caches), use interior mutability primitives like `RwLock`, `Mutex`, or atomic types:
///
/// ```rust,ignore
/// use std::sync::atomic::AtomicUsize;
///
/// struct Metrics { request_count: AtomicUsize }
///
/// // Builder
/// App::builder().app_state(Metrics { request_count: AtomicUsize::new(0) });
///
/// // Handler
/// let metrics = ctx.app_state.get_required::<Metrics>()?;
/// metrics.request_count.fetch_add(1, Ordering::Relaxed);
/// ```
///
/// # Extensions (Mutable, Per-Request)
///
/// Pre-dispatch hooks inject per-request state into `extensions`:
///
/// ```rust
/// use standout_dispatch::{Hooks, HookError, CommandContext};
///
/// struct UserScope { user_id: String }
///
/// let hooks = Hooks::new()
///     .pre_dispatch(|matches, ctx| {
///         let user_id = matches.get_one::<String>("user").unwrap();
///         ctx.extensions.insert(UserScope { user_id: user_id.clone() });
///         Ok(())
///     });
///
/// // In handler:
/// fn my_handler(matches: &clap::ArgMatches, ctx: &CommandContext) -> anyhow::Result<()> {
///     let scope = ctx.extensions.get_required::<UserScope>()?;
///     // use scope.user_id...
///     Ok(())
/// }
/// ```
#[derive(Debug)]
pub struct CommandContext {
    /// The command path being executed (e.g., ["config", "get"])
    pub command_path: Vec<String>,

    /// Immutable app-level state shared across all dispatches.
    ///
    /// Configured via `AppBuilder::app_state()`. Contains long-lived resources
    /// like database connections, configuration, and API clients.
    ///
    /// Use `get::<T>()` or `get_required::<T>()` to retrieve values.
    pub app_state: Rc<Extensions>,

    /// Mutable per-request state container.
    ///
    /// Pre-dispatch hooks can insert values that handlers retrieve.
    /// Each dispatch gets a fresh Extensions instance.
    pub extensions: Extensions,
}

impl CommandContext {
    /// Creates a new CommandContext with the given path and shared app state.
    ///
    /// This is more efficient than `Default::default()` when you already have app_state.
    pub fn new(command_path: Vec<String>, app_state: Rc<Extensions>) -> Self {
        Self {
            command_path,
            app_state,
            extensions: Extensions::new(),
        }
    }
}

impl Default for CommandContext {
    fn default() -> Self {
        Self {
            command_path: Vec::new(),
            app_state: Rc::new(Extensions::new()),
            extensions: Extensions::new(),
        }
    }
}

/// What a handler produces.
///
/// This enum represents the different types of output a command handler can produce.
///
/// # Binary vs. Artifact
///
/// [`Output::Binary`] hands bytes and a filename *hint* back to the caller: the
/// framework writes them to stdout, or to an explicit `--output-file-path`
/// override, and the filename never authorizes a filesystem write on its own.
///
/// [`Output::Artifact`] is the opt-in compound shape: bytes plus an optional
/// suggested destination that *does* authorize a write, plus an
/// application-owned semantic report the framework renders only after the write
/// succeeds. See the [`artifact`](crate::artifact) module for the destination
/// policy and report channel.
///
/// Marked `#[non_exhaustive]` so future output shapes can be added without
/// breaking exhaustive matchers.
#[derive(Debug)]
#[non_exhaustive]
pub enum Output<T: Serialize> {
    /// Data to render with a template or serialize to JSON/YAML/etc.
    Render(T),
    /// Silent exit (no output produced)
    Silent,
    /// Binary output for file exports
    Binary {
        /// The binary data
        data: Vec<u8>,
        /// Suggested filename for the output
        filename: String,
    },
    /// Owned artifact bytes with an optional suggested destination and an
    /// application-owned report rendered after the framework-owned write.
    Artifact(Artifact<T>),
}

impl<T: Serialize> Output<T> {
    /// Returns true if this is a render result.
    pub fn is_render(&self) -> bool {
        matches!(self, Output::Render(_))
    }

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

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

    /// Returns true if this is a compound artifact result.
    pub fn is_artifact(&self) -> bool {
        matches!(self, Output::Artifact(_))
    }
}

/// The result type for command handlers.
///
/// Enables use of the `?` operator for error propagation.
pub type HandlerResult<T> = Result<Output<T>, anyhow::Error>;

/// Trait for types that can be converted into a [`HandlerResult`].
///
/// This enables handlers to return either `Result<T, E>` directly (auto-wrapped
/// in [`Output::Render`]) or the explicit [`HandlerResult<T>`] when fine-grained
/// control is needed (for [`Output::Silent`] or [`Output::Binary`]).
///
/// # Example
///
/// ```rust
/// use standout_dispatch::{HandlerResult, Output, IntoHandlerResult};
///
/// // Direct Result<T, E> is auto-wrapped in Output::Render
/// fn simple() -> Result<String, anyhow::Error> {
///     Ok("hello".to_string())
/// }
/// let result: HandlerResult<String> = simple().into_handler_result();
/// assert!(matches!(result, Ok(Output::Render(_))));
///
/// // HandlerResult<T> passes through unchanged
/// fn explicit() -> HandlerResult<String> {
///     Ok(Output::Silent)
/// }
/// let result: HandlerResult<String> = explicit().into_handler_result();
/// assert!(matches!(result, Ok(Output::Silent)));
/// ```
pub trait IntoHandlerResult<T: Serialize> {
    /// Convert this type into a [`HandlerResult<T>`].
    fn into_handler_result(self) -> HandlerResult<T>;
}

/// Implementation for `Result<T, E>` - auto-wraps successful values in [`Output::Render`].
///
/// This is the ergonomic path: handlers can return `Result<T, E>` directly
/// and the framework wraps it appropriately.
impl<T, E> IntoHandlerResult<T> for Result<T, E>
where
    T: Serialize,
    E: Into<anyhow::Error>,
{
    fn into_handler_result(self) -> HandlerResult<T> {
        self.map(Output::Render).map_err(Into::into)
    }
}

/// Implementation for `HandlerResult<T>` - passes through unchanged.
///
/// This is the explicit path: handlers that need [`Output::Silent`] or
/// [`Output::Binary`] can return `HandlerResult<T>` directly.
impl<T: Serialize> IntoHandlerResult<T> for HandlerResult<T> {
    fn into_handler_result(self) -> HandlerResult<T> {
        self
    }
}

/// Process exit status selected by Standout's execution policy.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ExitStatus(u8);

impl ExitStatus {
    /// Successful command, help, or version display.
    pub const SUCCESS: Self = Self(0);
    /// Runtime or final-write failure.
    pub const FAILURE: Self = Self(1);
    /// Clap command-line usage error.
    pub const USAGE_ERROR: Self = Self(2);

    /// Returns the numeric status reported to the operating system.
    pub const fn code(self) -> u8 {
        self.0
    }
}

/// Error returned when an external failure attempts to declare success.
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[error("an external failure status must be nonzero")]
pub struct InvalidExternalStatus;

/// An application-declared failure from an authoritative external operation.
///
/// Use this narrow escape hatch when a command delegates to another executable
/// and must preserve that executable's exact nonzero status and stderr payload.
/// Returning ordinary handler or hook errors continues to use Standout's normal
/// runtime status (`1`); this type does not configure general error mapping.
///
/// `ExternalFailure` can flow through a handler's existing [`HandlerResult`]
/// error seam via `?`/`Into<anyhow::Error>`. For pre-dispatch, wrap it with
/// [`HookError::pre_dispatch_external`](crate::HookError::pre_dispatch_external).
#[derive(Debug, Clone)]
pub struct ExternalFailure {
    status: ExitStatus,
    diagnostic: String,
    source: Option<Arc<dyn std::error::Error + Send + Sync + 'static>>,
}

impl ExternalFailure {
    /// Declares an external failure with an exact nonzero process status and
    /// verbatim stderr payload.
    pub fn new(status: u8, diagnostic: impl Into<String>) -> Result<Self, InvalidExternalStatus> {
        if status == 0 {
            return Err(InvalidExternalStatus);
        }
        Ok(Self {
            status: ExitStatus(status),
            diagnostic: diagnostic.into(),
            source: None,
        })
    }

    /// Returns the exact status declared by the application.
    pub const fn exit_status(&self) -> ExitStatus {
        self.status
    }

    /// Returns the stderr payload without adding or removing any text.
    pub fn diagnostic(&self) -> &str {
        &self.diagnostic
    }

    /// Attaches the error that caused the external operation to fail.
    pub fn with_source<E>(mut self, source: E) -> Self
    where
        E: std::error::Error + Send + Sync + 'static,
    {
        self.source = Some(Arc::new(source));
        self
    }
}

impl fmt::Display for ExternalFailure {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.diagnostic())
    }
}

impl std::error::Error for ExternalFailure {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        self.source
            .as_deref()
            .map(|source| source as &(dyn std::error::Error + 'static))
    }
}

/// Successful text outcome carried by [`RunResult::Handled`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SuccessKind {
    /// A registered command completed successfully.
    Command,
    /// Clap requested a help display.
    ClapHelp,
    /// Clap requested a version display.
    ClapVersion,
}

/// The final payload whose write failed.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum OutputKind {
    /// Rendered text output.
    Text,
    /// Binary output.
    Binary,
    /// Compound artifact bytes. Also covers a destination that could not be
    /// selected: an unwritable artifact is a final-write failure, not a
    /// silently discarded one.
    Artifact,
}

/// Typed origin for an unsuccessful framework run.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum RunErrorKind {
    /// Clap rejected the command line.
    ClapUsage,
    /// A default-command resolver named a command the CLI does not have.
    DefaultCommand,
    /// The application handler returned an error.
    Handler,
    /// A hook failed in the identified phase. Pipe failures are post-output hooks.
    Hook(HookPhase),
    /// Handler data serialization or presentation rendering failed.
    Render,
    /// A framework-owned file/stdout write failed.
    FinalWrite(OutputKind),
    /// The application declared a failure from an authoritative external operation.
    External,
}

/// Metadata-bearing successful text compatible with string-oriented access.
#[derive(Debug, Clone)]
pub struct RunOutput {
    text: String,
    kind: SuccessKind,
}

impl RunOutput {
    /// Creates a successful command output.
    pub fn command(text: impl Into<String>) -> Self {
        Self {
            text: text.into(),
            kind: SuccessKind::Command,
        }
    }

    /// Creates a Clap help display.
    pub fn clap_help(text: impl Into<String>) -> Self {
        Self {
            text: text.into(),
            kind: SuccessKind::ClapHelp,
        }
    }

    /// Creates a Clap version display.
    pub fn clap_version(text: impl Into<String>) -> Self {
        Self {
            text: text.into(),
            kind: SuccessKind::ClapVersion,
        }
    }

    /// Returns the captured text.
    pub fn as_str(&self) -> &str {
        &self.text
    }

    /// Returns the typed success origin.
    pub const fn kind(&self) -> SuccessKind {
        self.kind
    }

    /// Consumes the wrapper and returns the captured text.
    pub fn into_string(self) -> String {
        self.text
    }
}

impl std::ops::Deref for RunOutput {
    type Target = str;
    fn deref(&self) -> &Self::Target {
        self.as_str()
    }
}

impl AsRef<str> for RunOutput {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl fmt::Display for RunOutput {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl PartialEq<str> for RunOutput {
    fn eq(&self, other: &str) -> bool {
        self.as_str() == other
    }
}

impl PartialEq<&str> for RunOutput {
    fn eq(&self, other: &&str) -> bool {
        self.as_str() == *other
    }
}

impl PartialEq<String> for RunOutput {
    fn eq(&self, other: &String) -> bool {
        self.as_str() == other
    }
}

impl From<String> for RunOutput {
    fn from(text: String) -> Self {
        Self::command(text)
    }
}

impl From<&str> for RunOutput {
    fn from(text: &str) -> Self {
        Self::command(text)
    }
}

impl From<RunOutput> for String {
    fn from(output: RunOutput) -> Self {
        output.into_string()
    }
}

/// Metadata-bearing failure compatible with string-oriented access.
#[derive(Debug, Clone)]
pub struct RunError {
    message: String,
    kind: RunErrorKind,
    status: ExitStatus,
    source: Option<Arc<dyn std::error::Error + Send + Sync + 'static>>,
}

impl RunError {
    /// Creates a failure with its framework origin.
    ///
    /// # Panics
    ///
    /// Panics for [`RunErrorKind::External`]. External outcomes must be
    /// constructed through [`ExternalFailure`] so their nonzero declared
    /// status and verbatim diagnostic cannot become inconsistent.
    pub fn new(message: impl Into<String>, kind: RunErrorKind) -> Self {
        assert!(
            kind != RunErrorKind::External,
            "external run errors must be constructed from ExternalFailure"
        );
        let status = match kind {
            RunErrorKind::ClapUsage => ExitStatus::USAGE_ERROR,
            _ => ExitStatus::FAILURE,
        };
        Self {
            message: message.into(),
            kind,
            status,
            source: None,
        }
    }

    /// Attaches the error that caused this captured failure.
    pub fn with_source<E>(mut self, source: E) -> Self
    where
        E: std::error::Error + Send + Sync + 'static,
    {
        self.source = Some(Arc::new(source));
        self
    }

    /// Returns the existing human-readable diagnostic.
    pub fn as_str(&self) -> &str {
        &self.message
    }

    /// Returns the typed failure origin.
    pub const fn kind(&self) -> RunErrorKind {
        self.kind
    }

    /// Returns the shell status selected for this failure.
    pub const fn exit_status(&self) -> ExitStatus {
        self.status
    }

    /// Consumes the wrapper and returns the diagnostic text.
    pub fn into_string(self) -> String {
        self.message
    }
}

impl std::ops::Deref for RunError {
    type Target = str;
    fn deref(&self) -> &Self::Target {
        self.as_str()
    }
}

impl AsRef<str> for RunError {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl fmt::Display for RunError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl std::error::Error for RunError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        self.source
            .as_deref()
            .map(|source| source as &(dyn std::error::Error + 'static))
    }
}

impl From<ExternalFailure> for RunError {
    fn from(failure: ExternalFailure) -> Self {
        Self {
            message: failure.diagnostic,
            kind: RunErrorKind::External,
            status: failure.status,
            source: failure.source,
        }
    }
}

impl From<String> for RunError {
    fn from(message: String) -> Self {
        Self::new(message, RunErrorKind::Handler)
    }
}

impl From<&str> for RunError {
    fn from(message: &str) -> Self {
        Self::new(message, RunErrorKind::Handler)
    }
}

impl From<RunError> for String {
    fn from(error: RunError) -> Self {
        error.into_string()
    }
}

/// Result of running the CLI dispatcher.
///
/// After processing arguments, the dispatcher either handles a command,
/// surfaces an error, or falls through for manual handling.
///
/// Marked `#[non_exhaustive]` so future variants can be added without
/// breaking exhaustive matchers.
#[derive(Debug)]
#[non_exhaustive]
pub enum RunResult {
    /// A handler processed the command successfully; contains the rendered output
    Handled(RunOutput),
    /// A handler produced binary output (bytes, suggested filename)
    Binary(Vec<u8>, String),
    /// A handler produced a compound artifact the framework completed: the
    /// bytes, the receipt naming the destination the write completed to, and
    /// the report rendered after that write.
    ///
    /// For a file destination the bytes are already on disk. For the stdout
    /// destination the byte write is deferred to the framework's stdout writer.
    Artifact(ArtifactRun),
    /// Silent output (handler completed but produced no output)
    Silent,
    /// A handler, hook, or output step failed; contains the formatted error message.
    /// Consumers should write this to stderr and exit non-zero.
    Error(RunError),
    /// No handler matched; contains the ArgMatches for manual handling
    NoMatch(ArgMatches),
}

impl RunResult {
    /// Returns true if a handler processed the command successfully (text output).
    pub fn is_handled(&self) -> bool {
        matches!(self, RunResult::Handled(_))
    }

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

    /// Returns true if the result is a completed artifact run.
    pub fn is_artifact(&self) -> bool {
        matches!(self, RunResult::Artifact(_))
    }

    /// Returns true if the result is silent.
    pub fn is_silent(&self) -> bool {
        matches!(self, RunResult::Silent)
    }

    /// Returns true if the result is an error.
    pub fn is_error(&self) -> bool {
        matches!(self, RunResult::Error(_))
    }

    /// Returns the output if handled, or None otherwise.
    pub fn output(&self) -> Option<&str> {
        match self {
            RunResult::Handled(s) => Some(s),
            _ => None,
        }
    }

    /// Returns the error message if this is an error, or None otherwise.
    pub fn error(&self) -> Option<&str> {
        match self {
            RunResult::Error(s) => Some(s),
            _ => None,
        }
    }

    /// Returns the typed success origin for captured text.
    pub fn success_kind(&self) -> Option<SuccessKind> {
        match self {
            RunResult::Handled(output) => Some(output.kind()),
            RunResult::Binary(_, _) | RunResult::Artifact(_) | RunResult::Silent => {
                Some(SuccessKind::Command)
            }
            _ => None,
        }
    }

    /// Returns the typed error origin, if this run failed.
    pub fn error_kind(&self) -> Option<RunErrorKind> {
        match self {
            RunResult::Error(error) => Some(error.kind()),
            _ => None,
        }
    }

    /// Returns the completed run's shell status.
    ///
    /// `NoMatch` returns `None`: it is a fallback handoff, not a completed
    /// framework execution and is deliberately not treated as a usage error.
    pub fn exit_status(&self) -> Option<ExitStatus> {
        match self {
            RunResult::Handled(_)
            | RunResult::Binary(_, _)
            | RunResult::Artifact(_)
            | RunResult::Silent => Some(ExitStatus::SUCCESS),
            RunResult::Error(error) => Some(error.exit_status()),
            RunResult::NoMatch(_) => None,
        }
    }

    /// Returns the binary data and filename if binary, or None otherwise.
    pub fn binary(&self) -> Option<(&[u8], &str)> {
        match self {
            RunResult::Binary(bytes, filename) => Some((bytes, filename)),
            _ => None,
        }
    }

    /// Returns the completed artifact run, or None otherwise.
    pub fn artifact(&self) -> Option<&ArtifactRun> {
        match self {
            RunResult::Artifact(run) => Some(run),
            _ => None,
        }
    }

    /// Returns the matches if unhandled, or None if handled.
    pub fn matches(&self) -> Option<&ArgMatches> {
        match self {
            RunResult::NoMatch(m) => Some(m),
            _ => None,
        }
    }
}

/// Trait for command handlers.
///
/// Handlers take `&mut self` allowing direct mutation of internal state.
/// This is the common case for CLI applications which are single-threaded.
///
/// # Example
///
/// ```rust
/// use standout_dispatch::{Handler, HandlerResult, Output, CommandContext};
/// use clap::ArgMatches;
/// use serde::Serialize;
///
/// struct Counter { count: u32 }
///
/// impl Handler for Counter {
///     type Output = u32;
///
///     fn handle(&mut self, _m: &ArgMatches, _ctx: &CommandContext) -> HandlerResult<u32> {
///         self.count += 1;
///         Ok(Output::Render(self.count))
///     }
/// }
/// ```
pub trait Handler {
    /// The output type produced by this handler (must be serializable)
    type Output: Serialize;

    /// Execute the handler with the given matches and context.
    fn handle(&mut self, matches: &ArgMatches, ctx: &CommandContext)
        -> HandlerResult<Self::Output>;

    /// Returns the arguments expected by this handler for verification.
    ///
    /// This is used to verify that the CLI command definition matches the handler's expectations.
    /// Handlers generated by the `#[handler]` macro implement this automatically.
    fn expected_args(&self) -> Vec<ExpectedArg> {
        Vec::new()
    }
}

/// A wrapper that implements Handler for FnMut closures.
///
/// The closure can return either:
/// - `Result<T, E>` - automatically wrapped in [`Output::Render`]
/// - `HandlerResult<T>` - passed through unchanged (for [`Output::Silent`] or [`Output::Binary`])
///
/// # Example
///
/// ```rust
/// use standout_dispatch::{FnHandler, Handler, CommandContext, Output};
/// use clap::ArgMatches;
///
/// // Returning Result<T, E> directly (auto-wrapped)
/// let mut handler = FnHandler::new(|_m: &ArgMatches, _ctx: &CommandContext| {
///     Ok::<_, anyhow::Error>("hello".to_string())
/// });
///
/// // Returning HandlerResult<T> explicitly (for Silent/Binary)
/// let mut silent_handler = FnHandler::new(|_m: &ArgMatches, _ctx: &CommandContext| {
///     Ok(Output::<()>::Silent)
/// });
/// ```
pub struct FnHandler<F, T, R = HandlerResult<T>>
where
    T: Serialize,
{
    f: F,
    _phantom: std::marker::PhantomData<fn() -> (T, R)>,
}

impl<F, T, R> FnHandler<F, T, R>
where
    F: FnMut(&ArgMatches, &CommandContext) -> R,
    R: IntoHandlerResult<T>,
    T: Serialize,
{
    /// Creates a new FnHandler wrapping the given FnMut closure.
    pub fn new(f: F) -> Self {
        Self {
            f,
            _phantom: std::marker::PhantomData,
        }
    }
}

impl<F, T, R> Handler for FnHandler<F, T, R>
where
    F: FnMut(&ArgMatches, &CommandContext) -> R,
    R: IntoHandlerResult<T>,
    T: Serialize,
{
    type Output = T;

    fn handle(&mut self, matches: &ArgMatches, ctx: &CommandContext) -> HandlerResult<T> {
        (self.f)(matches, ctx).into_handler_result()
    }
}

/// A handler wrapper for functions that don't need [`CommandContext`].
///
/// This is the simpler variant of [`FnHandler`] for handlers that only need
/// [`ArgMatches`]. The context parameter is accepted but ignored internally.
///
/// The closure can return either:
/// - `Result<T, E>` - automatically wrapped in [`Output::Render`]
/// - `HandlerResult<T>` - passed through unchanged (for [`Output::Silent`] or [`Output::Binary`])
///
/// # Example
///
/// ```rust
/// use standout_dispatch::{SimpleFnHandler, Handler, CommandContext, Output};
/// use clap::ArgMatches;
///
/// // Handler that doesn't need context - just uses ArgMatches
/// let mut handler = SimpleFnHandler::new(|_m: &ArgMatches| {
///     Ok::<_, anyhow::Error>("Hello, world!".to_string())
/// });
///
/// // Can still be used via Handler trait (context is ignored)
/// let ctx = CommandContext::default();
/// let matches = clap::Command::new("test").get_matches_from(vec!["test"]);
/// let result = handler.handle(&matches, &ctx);
/// assert!(matches!(result, Ok(Output::Render(_))));
/// ```
pub struct SimpleFnHandler<F, T, R = HandlerResult<T>>
where
    T: Serialize,
{
    f: F,
    _phantom: std::marker::PhantomData<fn() -> (T, R)>,
}

impl<F, T, R> SimpleFnHandler<F, T, R>
where
    F: FnMut(&ArgMatches) -> R,
    R: IntoHandlerResult<T>,
    T: Serialize,
{
    /// Creates a new SimpleFnHandler wrapping the given FnMut closure.
    pub fn new(f: F) -> Self {
        Self {
            f,
            _phantom: std::marker::PhantomData,
        }
    }
}

impl<F, T, R> Handler for SimpleFnHandler<F, T, R>
where
    F: FnMut(&ArgMatches) -> R,
    R: IntoHandlerResult<T>,
    T: Serialize,
{
    type Output = T;

    fn handle(&mut self, matches: &ArgMatches, _ctx: &CommandContext) -> HandlerResult<T> {
        (self.f)(matches).into_handler_result()
    }
}

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

    #[test]
    fn test_command_context_creation() {
        let ctx = CommandContext {
            command_path: vec!["config".into(), "get".into()],
            app_state: Rc::new(Extensions::new()),
            extensions: Extensions::new(),
        };
        assert_eq!(ctx.command_path, vec!["config", "get"]);
    }

    #[test]
    fn external_failure_rejects_success_and_preserves_metadata() {
        assert_eq!(
            ExternalFailure::new(0, "not a failure").unwrap_err(),
            InvalidExternalStatus
        );

        let failure = ExternalFailure::new(128, "fatal: repository missing\n")
            .unwrap()
            .with_source(std::io::Error::other("git failed"));
        assert_eq!(failure.exit_status().code(), 128);
        assert_eq!(failure.diagnostic(), "fatal: repository missing\n");
        assert_eq!(
            std::error::Error::source(&failure).unwrap().to_string(),
            "git failed"
        );

        let captured = RunError::from(failure);
        assert_eq!(captured.kind(), RunErrorKind::External);
        assert_eq!(captured.exit_status().code(), 128);
        assert_eq!(captured.as_str(), "fatal: repository missing\n");
        assert_eq!(
            std::error::Error::source(&captured).unwrap().to_string(),
            "git failed"
        );
    }

    #[test]
    #[should_panic(expected = "external run errors must be constructed from ExternalFailure")]
    fn run_error_new_rejects_external_kind() {
        let _ = RunError::new("inconsistent", RunErrorKind::External);
    }

    #[test]
    fn test_command_context_default() {
        let ctx = CommandContext::default();
        assert!(ctx.command_path.is_empty());
        assert!(ctx.extensions.is_empty());
        assert!(ctx.app_state.is_empty());
    }

    #[test]
    fn test_command_context_with_app_state() {
        struct Database {
            url: String,
        }
        struct Config {
            debug: bool,
        }

        // Build app state
        let mut app_state = Extensions::new();
        app_state.insert(Database {
            url: "postgres://localhost".into(),
        });
        app_state.insert(Config { debug: true });
        let app_state = Rc::new(app_state);

        // Create context with app state
        let ctx = CommandContext {
            command_path: vec!["list".into()],
            app_state: app_state.clone(),
            extensions: Extensions::new(),
        };

        // Retrieve app state
        let db = ctx.app_state.get::<Database>().unwrap();
        assert_eq!(db.url, "postgres://localhost");

        let config = ctx.app_state.get::<Config>().unwrap();
        assert!(config.debug);

        // App state is shared via Rc
        assert_eq!(Rc::strong_count(&ctx.app_state), 2);
    }

    #[test]
    fn test_command_context_app_state_get_required() {
        struct Present;

        let mut app_state = Extensions::new();
        app_state.insert(Present);

        let ctx = CommandContext {
            command_path: vec![],
            app_state: Rc::new(app_state),
            extensions: Extensions::new(),
        };

        // Success case
        assert!(ctx.app_state.get_required::<Present>().is_ok());

        // Failure case
        #[derive(Debug)]
        struct Missing;
        let err = ctx.app_state.get_required::<Missing>();
        assert!(err.is_err());
        assert!(err.unwrap_err().to_string().contains("Extension missing"));
    }

    // Extensions tests
    #[test]
    fn test_extensions_insert_and_get() {
        struct MyState {
            value: i32,
        }

        let mut ext = Extensions::new();
        assert!(ext.is_empty());

        ext.insert(MyState { value: 42 });
        assert!(!ext.is_empty());
        assert_eq!(ext.len(), 1);

        let state = ext.get::<MyState>().unwrap();
        assert_eq!(state.value, 42);
    }

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

        let mut ext = Extensions::new();
        ext.insert(Counter { count: 0 });

        if let Some(counter) = ext.get_mut::<Counter>() {
            counter.count += 1;
        }

        assert_eq!(ext.get::<Counter>().unwrap().count, 1);
    }

    #[test]
    fn test_extensions_multiple_types() {
        struct TypeA(i32);
        struct TypeB(String);

        let mut ext = Extensions::new();
        ext.insert(TypeA(1));
        ext.insert(TypeB("hello".into()));

        assert_eq!(ext.len(), 2);
        assert_eq!(ext.get::<TypeA>().unwrap().0, 1);
        assert_eq!(ext.get::<TypeB>().unwrap().0, "hello");
    }

    #[test]
    fn test_extensions_replace() {
        struct Value(i32);

        let mut ext = Extensions::new();
        ext.insert(Value(1));

        let old = ext.insert(Value(2));
        assert_eq!(old.unwrap().0, 1);
        assert_eq!(ext.get::<Value>().unwrap().0, 2);
    }

    #[test]
    fn test_extensions_remove() {
        struct Value(i32);

        let mut ext = Extensions::new();
        ext.insert(Value(42));

        let removed = ext.remove::<Value>();
        assert_eq!(removed.unwrap().0, 42);
        assert!(ext.is_empty());
        assert!(ext.get::<Value>().is_none());
    }

    #[test]
    fn test_extensions_contains() {
        struct Present;
        struct Absent;

        let mut ext = Extensions::new();
        ext.insert(Present);

        assert!(ext.contains::<Present>());
        assert!(!ext.contains::<Absent>());
    }

    #[test]
    fn test_extensions_clear() {
        struct A;
        struct B;

        let mut ext = Extensions::new();
        ext.insert(A);
        ext.insert(B);
        assert_eq!(ext.len(), 2);

        ext.clear();
        assert!(ext.is_empty());
    }

    #[test]
    fn test_extensions_missing_type_returns_none() {
        struct NotInserted;

        let ext = Extensions::new();
        assert!(ext.get::<NotInserted>().is_none());
    }

    #[test]
    fn test_extensions_get_required() {
        #[derive(Debug)]
        struct Config {
            value: i32,
        }

        let mut ext = Extensions::new();
        ext.insert(Config { value: 100 });

        // Success case
        let val = ext.get_required::<Config>();
        assert!(val.is_ok());
        assert_eq!(val.unwrap().value, 100);

        // Failure case
        #[derive(Debug)]
        struct Missing;
        let err = ext.get_required::<Missing>();
        assert!(err.is_err());
        assert!(err
            .unwrap_err()
            .to_string()
            .contains("Extension missing: type"));
    }

    #[test]
    fn test_extensions_get_mut_required() {
        #[derive(Debug)]
        struct State {
            count: i32,
        }

        let mut ext = Extensions::new();
        ext.insert(State { count: 0 });

        // Success case
        {
            let val = ext.get_mut_required::<State>();
            assert!(val.is_ok());
            val.unwrap().count += 1;
        }
        assert_eq!(ext.get_required::<State>().unwrap().count, 1);

        // Failure case
        #[derive(Debug)]
        struct Missing;
        let err = ext.get_mut_required::<Missing>();
        assert!(err.is_err());
    }

    #[test]
    fn test_extensions_clone_behavior() {
        // Verify the documented behavior that Clone drops extensions
        struct Data(#[allow(dead_code)] i32);

        let mut original = Extensions::new();
        original.insert(Data(42));

        let cloned = original.clone();

        // Original has data
        assert!(original.get::<Data>().is_some());

        // Cloned is empty
        assert!(cloned.is_empty());
        assert!(cloned.get::<Data>().is_none());
    }

    #[test]
    fn test_output_render() {
        let output: Output<String> = Output::Render("success".into());
        assert!(output.is_render());
        assert!(!output.is_silent());
        assert!(!output.is_binary());
    }

    #[test]
    fn test_output_silent() {
        let output: Output<String> = Output::Silent;
        assert!(!output.is_render());
        assert!(output.is_silent());
        assert!(!output.is_binary());
    }

    #[test]
    fn test_output_binary() {
        let output: Output<String> = Output::Binary {
            data: vec![0x25, 0x50, 0x44, 0x46],
            filename: "report.pdf".into(),
        };
        assert!(!output.is_render());
        assert!(!output.is_silent());
        assert!(output.is_binary());
    }

    #[test]
    fn test_run_result_handled() {
        let result = RunResult::Handled("output".into());
        assert!(result.is_handled());
        assert!(!result.is_binary());
        assert!(!result.is_silent());
        assert_eq!(result.output(), Some("output"));
        assert!(result.matches().is_none());
    }

    #[test]
    fn test_run_result_silent() {
        let result = RunResult::Silent;
        assert!(!result.is_handled());
        assert!(!result.is_binary());
        assert!(result.is_silent());
    }

    #[test]
    fn test_run_result_binary() {
        let bytes = vec![0x25, 0x50, 0x44, 0x46];
        let result = RunResult::Binary(bytes.clone(), "report.pdf".into());
        assert!(!result.is_handled());
        assert!(result.is_binary());
        assert!(!result.is_silent());

        let (data, filename) = result.binary().unwrap();
        assert_eq!(data, &bytes);
        assert_eq!(filename, "report.pdf");
    }

    #[test]
    fn test_run_result_no_match() {
        let matches = clap::Command::new("test").get_matches_from(vec!["test"]);
        let result = RunResult::NoMatch(matches);
        assert!(!result.is_handled());
        assert!(!result.is_binary());
        assert!(result.matches().is_some());
    }

    #[test]
    fn test_fn_handler() {
        let mut handler = FnHandler::new(|_m: &ArgMatches, _ctx: &CommandContext| {
            Ok(Output::Render(json!({"status": "ok"})))
        });

        let ctx = CommandContext::default();
        let matches = clap::Command::new("test").get_matches_from(vec!["test"]);

        let result = handler.handle(&matches, &ctx);
        assert!(result.is_ok());
    }

    #[test]
    fn test_fn_handler_mutation() {
        let mut counter = 0u32;

        let mut handler = FnHandler::new(|_m: &ArgMatches, _ctx: &CommandContext| {
            counter += 1;
            Ok(Output::Render(counter))
        });

        let ctx = CommandContext::default();
        let matches = clap::Command::new("test").get_matches_from(vec!["test"]);

        let _ = handler.handle(&matches, &ctx);
        let _ = handler.handle(&matches, &ctx);
        let result = handler.handle(&matches, &ctx);

        assert!(result.is_ok());
        if let Ok(Output::Render(count)) = result {
            assert_eq!(count, 3);
        }
    }

    // IntoHandlerResult tests
    #[test]
    fn test_into_handler_result_from_result_ok() {
        use super::IntoHandlerResult;

        let result: Result<String, anyhow::Error> = Ok("hello".to_string());
        let handler_result = result.into_handler_result();

        assert!(handler_result.is_ok());
        match handler_result.unwrap() {
            Output::Render(s) => assert_eq!(s, "hello"),
            _ => panic!("Expected Output::Render"),
        }
    }

    #[test]
    fn test_into_handler_result_from_result_err() {
        use super::IntoHandlerResult;

        let result: Result<String, anyhow::Error> = Err(anyhow::anyhow!("test error"));
        let handler_result = result.into_handler_result();

        assert!(handler_result.is_err());
        assert!(handler_result
            .unwrap_err()
            .to_string()
            .contains("test error"));
    }

    #[test]
    fn test_into_handler_result_passthrough_render() {
        use super::IntoHandlerResult;

        let handler_result: HandlerResult<String> = Ok(Output::Render("hello".to_string()));
        let result = handler_result.into_handler_result();

        assert!(result.is_ok());
        match result.unwrap() {
            Output::Render(s) => assert_eq!(s, "hello"),
            _ => panic!("Expected Output::Render"),
        }
    }

    #[test]
    fn test_into_handler_result_passthrough_silent() {
        use super::IntoHandlerResult;

        let handler_result: HandlerResult<String> = Ok(Output::Silent);
        let result = handler_result.into_handler_result();

        assert!(result.is_ok());
        assert!(matches!(result.unwrap(), Output::Silent));
    }

    #[test]
    fn test_into_handler_result_passthrough_binary() {
        use super::IntoHandlerResult;

        let handler_result: HandlerResult<String> = Ok(Output::Binary {
            data: vec![1, 2, 3],
            filename: "test.bin".to_string(),
        });
        let result = handler_result.into_handler_result();

        assert!(result.is_ok());
        match result.unwrap() {
            Output::Binary { data, filename } => {
                assert_eq!(data, vec![1, 2, 3]);
                assert_eq!(filename, "test.bin");
            }
            _ => panic!("Expected Output::Binary"),
        }
    }

    #[test]
    fn test_fn_handler_with_auto_wrap() {
        // Handler that returns Result<T, E> directly (not HandlerResult)
        let mut handler = FnHandler::new(|_m: &ArgMatches, _ctx: &CommandContext| {
            Ok::<_, anyhow::Error>("auto-wrapped".to_string())
        });

        let ctx = CommandContext::default();
        let matches = clap::Command::new("test").get_matches_from(vec!["test"]);

        let result = handler.handle(&matches, &ctx);
        assert!(result.is_ok());
        match result.unwrap() {
            Output::Render(s) => assert_eq!(s, "auto-wrapped"),
            _ => panic!("Expected Output::Render"),
        }
    }

    #[test]
    fn test_fn_handler_with_explicit_output() {
        // Handler that returns HandlerResult directly (for Silent/Binary)
        let mut handler =
            FnHandler::new(|_m: &ArgMatches, _ctx: &CommandContext| Ok(Output::<()>::Silent));

        let ctx = CommandContext::default();
        let matches = clap::Command::new("test").get_matches_from(vec!["test"]);

        let result = handler.handle(&matches, &ctx);
        assert!(result.is_ok());
        assert!(matches!(result.unwrap(), Output::Silent));
    }

    #[test]
    fn test_fn_handler_with_custom_error_type() {
        // Custom error type that implements Into<anyhow::Error>
        #[derive(Debug)]
        struct CustomError(String);

        impl std::fmt::Display for CustomError {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                write!(f, "CustomError: {}", self.0)
            }
        }

        impl std::error::Error for CustomError {}

        let mut handler = FnHandler::new(|_m: &ArgMatches, _ctx: &CommandContext| {
            Err::<String, CustomError>(CustomError("oops".to_string()))
        });

        let ctx = CommandContext::default();
        let matches = clap::Command::new("test").get_matches_from(vec!["test"]);

        let result = handler.handle(&matches, &ctx);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("CustomError: oops"));
    }

    // SimpleFnHandler tests (no CommandContext)
    #[test]
    fn test_simple_fn_handler_basic() {
        use super::SimpleFnHandler;

        let mut handler = SimpleFnHandler::new(|_m: &ArgMatches| {
            Ok::<_, anyhow::Error>("no context needed".to_string())
        });

        let ctx = CommandContext::default();
        let matches = clap::Command::new("test").get_matches_from(vec!["test"]);

        let result = handler.handle(&matches, &ctx);
        assert!(result.is_ok());
        match result.unwrap() {
            Output::Render(s) => assert_eq!(s, "no context needed"),
            _ => panic!("Expected Output::Render"),
        }
    }

    #[test]
    fn test_simple_fn_handler_with_args() {
        use super::SimpleFnHandler;

        let mut handler = SimpleFnHandler::new(|m: &ArgMatches| {
            let verbose = m.get_flag("verbose");
            Ok::<_, anyhow::Error>(verbose)
        });

        let ctx = CommandContext::default();
        let matches = clap::Command::new("test")
            .arg(
                clap::Arg::new("verbose")
                    .short('v')
                    .action(clap::ArgAction::SetTrue),
            )
            .get_matches_from(vec!["test", "-v"]);

        let result = handler.handle(&matches, &ctx);
        assert!(result.is_ok());
        match result.unwrap() {
            Output::Render(v) => assert!(v),
            _ => panic!("Expected Output::Render"),
        }
    }

    #[test]
    fn test_simple_fn_handler_explicit_output() {
        use super::SimpleFnHandler;

        let mut handler = SimpleFnHandler::new(|_m: &ArgMatches| Ok(Output::<()>::Silent));

        let ctx = CommandContext::default();
        let matches = clap::Command::new("test").get_matches_from(vec!["test"]);

        let result = handler.handle(&matches, &ctx);
        assert!(result.is_ok());
        assert!(matches!(result.unwrap(), Output::Silent));
    }

    #[test]
    fn test_simple_fn_handler_error() {
        use super::SimpleFnHandler;

        let mut handler = SimpleFnHandler::new(|_m: &ArgMatches| {
            Err::<String, _>(anyhow::anyhow!("simple error"))
        });

        let ctx = CommandContext::default();
        let matches = clap::Command::new("test").get_matches_from(vec!["test"]);

        let result = handler.handle(&matches, &ctx);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("simple error"));
    }

    #[test]
    fn test_simple_fn_handler_mutation() {
        use super::SimpleFnHandler;

        let mut counter = 0u32;
        let mut handler = SimpleFnHandler::new(|_m: &ArgMatches| {
            counter += 1;
            Ok::<_, anyhow::Error>(counter)
        });

        let ctx = CommandContext::default();
        let matches = clap::Command::new("test").get_matches_from(vec!["test"]);

        let _ = handler.handle(&matches, &ctx);
        let _ = handler.handle(&matches, &ctx);
        let result = handler.handle(&matches, &ctx);

        assert!(result.is_ok());
        match result.unwrap() {
            Output::Render(n) => assert_eq!(n, 3),
            _ => panic!("Expected Output::Render"),
        }
    }
}