Skip to main content

mads_core/
context.rs

1//! Provider-resolution contexts for construction and running applications.
2
3use std::sync::Arc;
4
5use crate::{Config, ProviderRegistry, Result};
6
7/// Borrows the providers and configuration available while constructing an application.
8pub struct ConstructionContext<'a> {
9    registry: &'a ProviderRegistry,
10    config: &'a Config,
11}
12
13impl<'a> ConstructionContext<'a> {
14    /// Creates a context over providers and configuration owned by the application builder.
15    pub fn new(registry: &'a ProviderRegistry, config: &'a Config) -> Self {
16        Self { registry, config }
17    }
18
19    /// Resolves a provider by its concrete type.
20    #[allow(clippy::result_large_err)]
21    pub fn resolve<T>(&self) -> Result<Arc<T>>
22    where
23        T: Send + Sync + 'static,
24    {
25        self.registry.resolve()
26    }
27
28    /// Returns the configuration available during construction.
29    pub const fn config(&self) -> &'a Config {
30        self.config
31    }
32}
33
34/// Owns the immutable providers and configuration of a running application.
35#[derive(Clone)]
36pub struct ApplicationContext {
37    registry: Arc<ProviderRegistry>,
38    config: Arc<Config>,
39}
40
41impl ApplicationContext {
42    /// Creates an immutable application context from completed construction state.
43    pub fn new(registry: ProviderRegistry, config: Config) -> Self {
44        Self {
45            registry: Arc::new(registry),
46            config: Arc::new(config),
47        }
48    }
49
50    /// Resolves an application-scoped provider by its concrete type.
51    #[allow(clippy::result_large_err)]
52    pub fn resolve<T>(&self) -> Result<Arc<T>>
53    where
54        T: Send + Sync + 'static,
55    {
56        self.registry.resolve()
57    }
58
59    /// Returns the immutable application configuration.
60    pub fn config(&self) -> &Config {
61        self.config.as_ref()
62    }
63}