firebase_rs_sdk/component/
types.rs1use serde_json::Value;
2use std::any::Any;
3use std::fmt;
4use std::sync::Arc;
5
6use crate::component::container::ComponentContainer;
7
8pub type DynService = Arc<dyn Any + Send + Sync>;
9
10#[derive(Clone, Copy, Debug, PartialEq, Eq)]
11pub enum InstantiationMode {
12 Lazy,
13 Eager,
14 Explicit,
15}
16
17#[derive(Clone, Copy, Debug, PartialEq, Eq)]
18pub enum ComponentType {
19 Public,
20 Private,
21 Version,
22}
23
24#[derive(Debug, Clone, Default)]
25pub struct InstanceFactoryOptions {
26 pub instance_identifier: Option<String>,
27 pub options: Value,
28}
29
30impl InstanceFactoryOptions {
31 pub fn new(instance_identifier: Option<String>, options: Value) -> Self {
32 Self {
33 instance_identifier,
34 options,
35 }
36 }
37}
38
39pub type InstanceFactory = Arc<
40 dyn Fn(&ComponentContainer, InstanceFactoryOptions) -> Result<DynService, ComponentError>
41 + Send
42 + Sync,
43>;
44pub type OnInstanceCreatedCallback =
45 Arc<dyn Fn(&ComponentContainer, &str, &DynService) + Send + Sync>;
46
47#[derive(Debug)]
48pub enum ComponentError {
49 MismatchingComponent { expected: String, found: String },
50 ComponentAlreadyProvided { name: String },
51 ComponentNotRegistered { name: String },
52 InstanceAlreadyInitialized { name: String, identifier: String },
53 InitializationFailed { name: String, reason: String },
54 InstanceUnavailable { name: String },
55}
56
57impl fmt::Display for ComponentError {
58 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59 match self {
60 ComponentError::MismatchingComponent { expected, found } => {
61 write!(
62 f,
63 "Component {found} cannot satisfy provider for {expected}"
64 )
65 }
66 ComponentError::ComponentAlreadyProvided { name } => {
67 write!(f, "Component {name} has already been registered")
68 }
69 ComponentError::ComponentNotRegistered { name } => {
70 write!(f, "Component {name} has not been registered yet")
71 }
72 ComponentError::InstanceAlreadyInitialized { name, identifier } => {
73 write!(f, "{name}({identifier}) has already been initialized")
74 }
75 ComponentError::InitializationFailed { name, reason } => {
76 write!(f, "Component {name} failed to initialize: {reason}")
77 }
78 ComponentError::InstanceUnavailable { name } => {
79 write!(f, "Service {name} is not available")
80 }
81 }
82 }
83}
84
85impl std::error::Error for ComponentError {}