use std::fmt;
use tokio::sync::watch;
use crate::dependency_info::DependencyInfo;
use crate::dependency_state::DependencyState;
#[derive(Clone)]
pub struct Dependency {
module: &'static str,
name: String,
receiver: watch::Receiver<DependencyInfo>,
}
impl Dependency {
pub fn new(
module: &'static str,
name: impl Into<String>,
receiver: watch::Receiver<DependencyInfo>,
) -> Self {
Self {
module,
name: name.into(),
receiver,
}
}
pub fn module(&self) -> &'static str {
self.module
}
pub fn name(&self) -> &str {
&self.name
}
pub fn info(&self) -> DependencyInfo {
self.receiver.borrow().clone()
}
pub fn state(&self) -> DependencyState {
self.receiver.borrow().state()
}
pub fn subscribe(&self) -> watch::Receiver<DependencyInfo> {
self.receiver.clone()
}
pub fn is_same(&self, other: &Self) -> bool {
self.module == other.module && self.name == other.name
}
}
impl fmt::Display for Dependency {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}/{}", self.module, self.name)
}
}
impl fmt::Debug for Dependency {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Dependency")
.field("module", &self.module)
.field("name", &self.name)
.field("info", &self.info())
.finish()
}
}