redup_abstractions/
dependency.rs1use std::fmt;
4
5use tokio::sync::watch;
6
7use crate::dependency_info::DependencyInfo;
8use crate::dependency_state::DependencyState;
9
10#[derive(Clone)]
13pub struct Dependency {
14 module: &'static str,
15 name: String,
16 receiver: watch::Receiver<DependencyInfo>,
17}
18
19impl Dependency {
20 pub fn new(
22 module: &'static str,
23 name: impl Into<String>,
24 receiver: watch::Receiver<DependencyInfo>,
25 ) -> Self {
26 Self {
27 module,
28 name: name.into(),
29 receiver,
30 }
31 }
32
33 pub fn module(&self) -> &'static str {
35 self.module
36 }
37
38 pub fn name(&self) -> &str {
40 &self.name
41 }
42
43 pub fn info(&self) -> DependencyInfo {
45 self.receiver.borrow().clone()
46 }
47
48 pub fn state(&self) -> DependencyState {
50 self.receiver.borrow().state()
51 }
52
53 pub fn subscribe(&self) -> watch::Receiver<DependencyInfo> {
55 self.receiver.clone()
56 }
57
58 pub fn is_same(&self, other: &Self) -> bool {
60 self.module == other.module && self.name == other.name
61 }
62}
63
64impl fmt::Display for Dependency {
65 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66 write!(f, "{}/{}", self.module, self.name)
67 }
68}
69
70impl fmt::Debug for Dependency {
71 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72 f.debug_struct("Dependency")
73 .field("module", &self.module)
74 .field("name", &self.name)
75 .field("info", &self.info())
76 .finish()
77 }
78}