Skip to main content

vtcode_commons/
cgp.rs

1//! Context-Generic Programming (CGP) core wiring trait.
2//!
3//! CGP is a type-level composition pattern where a "context" struct maps
4//! component names to concrete provider types. The foundational trait is
5//! [`HasComponent`], which performs this mapping. The [`crate::delegate_components`]
6//! macro generates bulk implementations.
7//!
8//! # Compile-time wiring checks (CGP v0.8.0 style)
9//!
10//! `delegate_components!` only records the mapping: it does not prove that the
11//! chosen provider actually implements the capability's provider trait for the
12//! context. That mistake otherwise surfaces deep inside a generic consumer
13//! bound. [`crate::delegate_and_check_components`] and [`crate::check_components`]
14//! move the failure back to the wiring entry, so `add a provider that does not
15//! implement its trait` is rejected where the choice was made.
16//!
17//! # Example
18//!
19//! ```rust,ignore
20//! // Define component marker types and a provider trait with an explicit context.
21//! struct ApprovalComponent;
22//! struct SandboxComponent;
23//! trait ApprovalProvider<Ctx> {}
24//! struct PromptApproval;
25//! impl<C> ApprovalProvider<C> for PromptApproval {}
26//!
27//! // Wire and verify in one table.
28//! delegate_and_check_components!(InteractiveCtx {
29//!     ApprovalComponent => PromptApproval : ApprovalProvider<InteractiveCtx>,
30//!     SandboxComponent  => WorkspaceSandbox : SandboxProvider<InteractiveCtx>,
31//! });
32//!
33//! // Or verify wiring declared elsewhere against the resolved providers.
34//! check_components!(InteractiveCtx {
35//!     ApprovalComponent => ApprovalProvider<InteractiveCtx>,
36//! });
37//! ```
38
39/// Type-level lookup: maps a component **Name** to a concrete **Provider**
40/// type for a given implementor (the "context").
41///
42/// This is the single foundational trait of the CGP substrate. All
43/// composition flows through it.
44pub trait HasComponent<Name> {
45    /// The concrete provider type wired to `Name` for this context.
46    type Provider;
47}
48
49/// The elaborated provider/dictionary selected by `Ctx` for component `Name`.
50pub type ComponentProvider<Ctx, Name> = <Ctx as HasComponent<Name>>::Provider;
51
52/// Wire multiple component names to provider types for a context.
53///
54/// Generates one `HasComponent<Name>` implementation per entry.
55///
56/// ```rust,ignore
57/// delegate_components!(MyCtx {
58///     ApprovalComponent => PromptApproval,
59///     SandboxComponent  => WorkspaceSandbox,
60/// });
61/// ```
62#[macro_export]
63macro_rules! delegate_components {
64    ($ctx:ty { $($name:ty => $provider:ty),* $(,)? }) => {
65        $(
66            impl $crate::cgp::HasComponent<$name> for $ctx {
67                type Provider = $provider;
68            }
69        )*
70    };
71}
72
73/// Wire components and prove each provider implements its capability trait.
74///
75/// Each entry is `Component => Provider : ProviderTrait<Ctx>`. The trait check
76/// is type-checked at the table entry, so a mis-wiring is reported next to the
77/// choice rather than in the consumer bound that later needs the capability.
78///
79/// ```rust,ignore
80/// delegate_and_check_components!(InteractiveCtx {
81///     ApprovalComponent => PromptApproval : ApprovalProvider<InteractiveCtx>,
82///     SandboxComponent  => WorkspaceSandbox : SandboxProvider<InteractiveCtx>,
83/// });
84/// ```
85#[macro_export]
86macro_rules! delegate_and_check_components {
87    ($ctx:ty { $($name:ty => $provider:ty : $provider_trait:path),+ $(,)? }) => {
88        $crate::delegate_components!($ctx { $($name => $provider),+ });
89        $(
90            const _: fn() = || {
91                fn assert_provider<P: $provider_trait>() {}
92                assert_provider::<$provider>();
93            };
94        )+
95    };
96}
97
98/// Verify the providers already wired for a concrete context.
99///
100/// Unlike [`crate::delegate_and_check_components`], this checks the resolved
101/// [`crate::cgp::ComponentProvider`] instead of a provider written at the call
102/// site, so it can validate a context whose wiring lives in another module.
103///
104/// ```rust,ignore
105/// check_components!(InteractiveCtx {
106///     ApprovalComponent => ApprovalProvider<InteractiveCtx>,
107///     SandboxComponent  => SandboxProvider<InteractiveCtx>,
108/// });
109/// ```
110#[macro_export]
111macro_rules! check_components {
112    ($ctx:ty { $($name:ty => $provider_trait:path),+ $(,)? }) => {
113        $(
114            const _: fn() = || {
115                fn assert_provider<P: $provider_trait>() {}
116                assert_provider::<$crate::cgp::ComponentProvider<$ctx, $name>>();
117            };
118        )+
119    };
120}
121
122#[cfg(test)]
123mod tests {
124    use super::*;
125
126    struct Ctx;
127    struct OtherCtx;
128    struct Capability;
129    struct GoodProvider;
130    struct OtherProvider;
131
132    trait CapabilityProvider<C> {}
133
134    impl CapabilityProvider<Ctx> for GoodProvider {}
135    impl CapabilityProvider<OtherCtx> for OtherProvider {}
136
137    delegate_components!(Ctx { Capability => GoodProvider });
138
139    // Checks the provider resolved from the existing wiring.
140    check_components!(Ctx { Capability => CapabilityProvider<Ctx> });
141
142    // Wires and verifies in one table.
143    delegate_and_check_components!(OtherCtx {
144        Capability => OtherProvider : CapabilityProvider<OtherCtx>,
145    });
146
147    #[test]
148    fn checked_wiring_resolves_to_the_selected_provider() {
149        fn assert_provider<P: CapabilityProvider<Ctx>>() {}
150        assert_provider::<ComponentProvider<Ctx, Capability>>();
151    }
152}