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
#![allow(unused_variables)]
mod name;
mod registry;
pub use self::{name::Name, registry::Registry};
use crate::{application::Application, error::FrameworkError, shutdown::Shutdown, Version};
use std::{cmp::Ordering, fmt::Debug, slice::Iter};
pub trait Component<A>: Debug + Send + Sync
where
A: Application,
{
fn name(&self) -> Name;
fn version(&self) -> Version;
fn dependencies(&self) -> Iter<'_, Name> {
[].iter()
}
fn after_config(&mut self, config: &A::Cfg) -> Result<(), FrameworkError> {
Ok(())
}
fn before_shutdown(&self, kind: Shutdown) -> Result<(), FrameworkError> {
Ok(())
}
}
impl<A> PartialEq for Box<dyn Component<A>>
where
A: Application,
{
fn eq(&self, other: &Self) -> bool {
self.name() == other.name()
}
}
impl<A> PartialOrd for Box<dyn Component<A>>
where
A: Application,
{
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
if other.dependencies().any(|dep| *dep == self.name()) {
if self.dependencies().any(|dep| *dep == other.name()) {
None
} else {
Some(Ordering::Greater)
}
} else if self.dependencies().any(|dep| *dep == other.name()) {
Some(Ordering::Less)
} else {
Some(Ordering::Equal)
}
}
}