use std::sync::Arc; use crank_adapter_rest::{OutboundHttpPolicy, RestAdapter, RestAdapterError}; use crank_core::{ AdapterRegistry, CoordinationStateStore, MeteringSink, NoopMeteringSink, ResponseCacheStore, SharedMeteringSink, SharedProtocolAdapter, }; use crate::{RuntimeExecutor, RuntimeLimits}; pub struct RuntimeExecutorBuilder { limits: RuntimeLimits, adapters: AdapterRegistry, response_cache: Option>, coordination_store: Option>, metering_sink: SharedMeteringSink, } impl RuntimeExecutorBuilder { pub fn new() -> Self { Self { limits: RuntimeLimits::default(), adapters: AdapterRegistry::empty(), response_cache: None, coordination_store: None, metering_sink: Arc::new(NoopMeteringSink) as Arc, } } pub fn with_limits(mut self, limits: RuntimeLimits) -> Self { self.limits = limits; self } pub fn register_adapter(mut self, adapter: SharedProtocolAdapter) -> Self { self.adapters = self.adapters.register(adapter); self } pub fn with_response_cache(mut self, store: Arc) -> Self { self.response_cache = Some(store); self } pub fn with_coordination_store(mut self, store: Arc) -> Self { self.coordination_store = Some(store); self } pub fn with_metering_sink(mut self, sink: SharedMeteringSink) -> Self { self.metering_sink = sink; self } pub fn build(self) -> RuntimeExecutor { RuntimeExecutor::from_builder_parts( self.limits, self.adapters, self.response_cache, self.coordination_store, self.metering_sink, ) } } impl Default for RuntimeExecutorBuilder { fn default() -> Self { Self::new() } } pub fn community_default() -> RuntimeExecutorBuilder { RuntimeExecutorBuilder::new() .register_adapter(Arc::new(RestAdapter::new()) as SharedProtocolAdapter) } pub fn community_from_env() -> Result { Ok(RuntimeExecutorBuilder::new() .register_adapter(Arc::new(RestAdapter::from_env()?) as SharedProtocolAdapter)) } pub fn community_with_outbound_policy(policy: OutboundHttpPolicy) -> RuntimeExecutorBuilder { RuntimeExecutorBuilder::new() .register_adapter(Arc::new(RestAdapter::with_policy(policy)) as SharedProtocolAdapter) }