1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use std::error::{self, Error as StdError};
use std::fmt;
use std::sync::Arc;
use downcast::{AnySync, TypeMismatch};
pub use anyhow::{Result, Error};
#[derive(Debug)]
pub struct InstancerNotFoundError {
pub service_name: String
}
impl InstancerNotFoundError {
pub fn new(service_name: String) -> Self {
Self{ service_name }
}
}
impl fmt::Display for InstancerNotFoundError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "instancer for {} not found", self.service_name)
}
}
impl error::Error for InstancerNotFoundError {}
#[derive(Debug)]
pub struct InstanceCreationError {
pub service_name: String,
pub creation_error: Error,
}
impl InstanceCreationError {
pub fn new(service_name: String, creation_error: Error) -> Self {
Self{ service_name, creation_error }
}
}
impl fmt::Display for InstanceCreationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "instance creation for {} failed: {}", self.service_name, self.creation_error)
}
}
impl error::Error for InstanceCreationError {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
Some(&*self.creation_error)
}
}
#[derive(Debug)]
pub struct InstanceTypeError {
pub service_name: String,
pub type_mismatch: TypeMismatch,
}
impl InstanceTypeError {
pub fn new(service_name: String, type_mismatch: TypeMismatch) -> Self {
Self{ service_name, type_mismatch }
}
}
impl fmt::Display for InstanceTypeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "wrong type for instance of {}: {}", self.service_name, self.type_mismatch)
}
}
impl error::Error for InstanceTypeError {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
Some(&self.type_mismatch)
}
}
pub trait Service: Send + Sync + 'static {
fn service_name() -> String {
std::any::type_name::<Self>()
.replace("dyn ", "")
.replace("::", ".")
}
}
pub type InstanceRef = Arc<dyn AnySync>;
#[allow(type_alias_bounds)]
pub type TypedInstanceRef<S: ?Sized> = Arc<Box<S>>;
pub use TypedInstanceRef as I;