saluki_core/components/
build_context.rs1use std::fmt;
4
5use super::{ComponentContext, ComponentType};
6use crate::{
7 runtime::state::{AcquireError, ResourceLease, ResourceRegistry, ResourceSpecification},
8 support::SubsystemIdentifier,
9 topology::ComponentId,
10};
11
12#[derive(Clone)]
17pub struct BuildContext {
18 component_context: ComponentContext,
19 resource_registry: ResourceRegistry,
20}
21
22impl BuildContext {
23 pub fn new(component_context: ComponentContext, resource_registry: ResourceRegistry) -> Self {
25 Self {
26 component_context,
27 resource_registry,
28 }
29 }
30
31 pub fn component_context(&self) -> &ComponentContext {
33 &self.component_context
34 }
35
36 pub fn component_id(&self) -> &ComponentId {
41 self.component_context.component_id()
42 }
43
44 pub fn component_type(&self) -> ComponentType {
46 self.component_context.component_type()
47 }
48
49 pub fn identity(&self) -> SubsystemIdentifier {
54 self.component_context.identity()
55 }
56
57 pub async fn acquire_resource<S: ResourceSpecification>(
63 &self, spec: S,
64 ) -> Result<ResourceLease<S::Resource>, AcquireError> {
65 self.resource_registry.acquire(&self.identity(), spec).await
66 }
67}
68
69impl fmt::Display for BuildContext {
70 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
71 write!(f, "{}", self.component_context)
72 }
73}
74
75macro_rules! test_build_contexts {
76 ($($name:ident => $component_context:ident, $doc:literal;)*) => {
77 $(
78 #[doc = $doc]
79 #[cfg(any(test, feature = "test-util"))]
83 pub fn $name<S: AsRef<str>>(component_id: S) -> Self {
84 Self::new(ComponentContext::$component_context(component_id), ResourceRegistry::new())
85 }
86 )*
87 };
88}
89
90impl BuildContext {
91 test_build_contexts! {
92 test_source => test_source, "Creates a new `BuildContext` for a source component with the given identifier, in a test topology.";
93 test_relay => test_relay, "Creates a new `BuildContext` for a relay component with the given identifier, in a test topology.";
94 test_decoder => test_decoder, "Creates a new `BuildContext` for a decoder component with the given identifier, in a test topology.";
95 test_transform => test_transform, "Creates a new `BuildContext` for a transform component with the given identifier, in a test topology.";
96 test_encoder => test_encoder, "Creates a new `BuildContext` for an encoder component with the given identifier, in a test topology.";
97 test_forwarder => test_forwarder, "Creates a new `BuildContext` for a forwarder component with the given identifier, in a test topology.";
98 test_destination => test_destination, "Creates a new `BuildContext` for a destination component with the given identifier, in a test topology.";
99 }
100}