use {
crate::{DatastarEvent, consts},
core::time::Duration,
};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct MergeSignals {
pub id: Option<String>,
pub retry: Duration,
pub signals: String,
pub only_if_missing: bool,
}
impl MergeSignals {
pub fn new(signals: impl Into<String>) -> Self {
Self {
id: None,
retry: Duration::from_millis(consts::DEFAULT_SSE_RETRY_DURATION),
signals: signals.into(),
only_if_missing: consts::DEFAULT_MERGE_SIGNALS_ONLY_IF_MISSING,
}
}
pub fn id(mut self, id: impl Into<String>) -> Self {
self.id = Some(id.into());
self
}
pub fn retry(mut self, retry: Duration) -> Self {
self.retry = retry;
self
}
pub fn only_if_missing(mut self, only_if_missing: bool) -> Self {
self.only_if_missing = only_if_missing;
self
}
#[inline]
pub fn into_event(self) -> DatastarEvent {
self.into()
}
}
impl From<MergeSignals> for DatastarEvent {
fn from(val: MergeSignals) -> Self {
let mut data: Vec<String> = Vec::new();
if val.only_if_missing != consts::DEFAULT_MERGE_SIGNALS_ONLY_IF_MISSING {
data.push(format!(
"{} {}",
consts::ONLY_IF_MISSING_DATALINE_LITERAL,
val.only_if_missing
));
}
data.push(format!(
"{} {}",
consts::SIGNALS_DATALINE_LITERAL,
val.signals
));
Self {
event: consts::EventType::MergeSignals,
id: val.id,
retry: val.retry,
data,
}
}
}