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 [`delegate_components!`]
6//! macro generates bulk implementations.
7//!
8//! # Example
9//!
10//! ```rust,ignore
11//! // Define component marker types
12//! struct ApprovalComponent;
13//! struct SandboxComponent;
14//!
15//! // Wire them for a context
16//! delegate_components!(InteractiveCtx {
17//!     ApprovalComponent => PromptApproval,
18//!     SandboxComponent  => WorkspaceSandbox,
19//! });
20//!
21//! // Access the wired provider type
22//! let provider: ComponentProvider<InteractiveCtx, ApprovalComponent> = ...;
23//! ```
24
25/// Type-level lookup: maps a component **Name** to a concrete **Provider**
26/// type for a given implementor (the "context").
27///
28/// This is the single foundational trait of the CGP substrate. All
29/// composition flows through it.
30pub trait HasComponent<Name> {
31    /// The concrete provider type wired to `Name` for this context.
32    type Provider;
33}
34
35/// The elaborated provider/dictionary selected by `Ctx` for component `Name`.
36pub type ComponentProvider<Ctx, Name> = <Ctx as HasComponent<Name>>::Provider;
37
38/// Wire multiple component names to provider types for a context.
39///
40/// Generates one `HasComponent<Name>` implementation per entry.
41///
42/// ```rust,ignore
43/// delegate_components!(MyCtx {
44///     ApprovalComponent => PromptApproval,
45///     SandboxComponent  => WorkspaceSandbox,
46/// });
47/// ```
48#[macro_export]
49macro_rules! delegate_components {
50    ($ctx:ty { $($name:ty => $provider:ty),* $(,)? }) => {
51        $(
52            impl $crate::cgp::HasComponent<$name> for $ctx {
53                type Provider = $provider;
54            }
55        )*
56    };
57}