use std::path::PathBuf;
use super::TriggerKindLabel;
#[cfg(unix)]
use super::command::CommandConfig;
use super::post::PostConfig;
use super::teams::TeamsConfig;
use super::webhook::WebhookConfig;
#[derive(Clone)]
pub(super) enum TriggerKind {
Echo {
label: Option<String>,
},
Log {
path: PathBuf,
},
#[cfg(unix)]
Command(Box<CommandConfig>),
Webhook(Box<WebhookConfig>),
Teams(Box<TeamsConfig>),
Post(Box<PostConfig>),
#[cfg(test)]
TestFailing {
failures_remaining: std::sync::Arc<std::sync::atomic::AtomicU32>,
eventual: TestEventual,
},
#[cfg(test)]
TestFailOnCall {
calls: std::sync::Arc<std::sync::atomic::AtomicU32>,
fail_on_call: u32,
},
}
#[cfg(test)]
#[derive(Clone, Debug)]
pub(super) enum TestEventual {
Succeed,
Fail,
}
impl std::fmt::Debug for TriggerKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Echo { label } => f.debug_struct("Echo").field("label", label).finish(),
Self::Log { path } => f.debug_struct("Log").field("path", path).finish(),
#[cfg(unix)]
Self::Command(cfg) => f.debug_tuple("Command").field(&**cfg).finish(),
Self::Webhook(cfg) => f.debug_tuple("Webhook").field(&**cfg).finish(),
Self::Teams(cfg) => f.debug_tuple("Teams").field(&**cfg).finish(),
Self::Post(cfg) => f.debug_tuple("Post").field(&**cfg).finish(),
#[cfg(test)]
Self::TestFailing {
failures_remaining,
eventual,
} => f
.debug_struct("TestFailing")
.field("failures_remaining", failures_remaining)
.field("eventual", eventual)
.finish(),
#[cfg(test)]
Self::TestFailOnCall {
calls,
fail_on_call,
} => f
.debug_struct("TestFailOnCall")
.field("calls", calls)
.field("fail_on_call", fail_on_call)
.finish(),
}
}
}
pub(super) fn trigger_kind_label(kind: &TriggerKind) -> TriggerKindLabel {
match kind {
TriggerKind::Echo { .. } => TriggerKindLabel::Echo,
TriggerKind::Log { path } => TriggerKindLabel::Log { path: path.clone() },
#[cfg(unix)]
TriggerKind::Command(_) => TriggerKindLabel::Command,
TriggerKind::Webhook(_) => TriggerKindLabel::Webhook,
TriggerKind::Teams(_) => TriggerKindLabel::Teams,
TriggerKind::Post(_) => TriggerKindLabel::Post,
#[cfg(test)]
TriggerKind::TestFailing { .. } => TriggerKindLabel::Echo,
#[cfg(test)]
TriggerKind::TestFailOnCall { .. } => TriggerKindLabel::Echo,
}
}
#[cfg(test)]
#[allow(
clippy::panic,
reason = "test code: panic on unexpected variant is the standard test diagnostic"
)]
mod tests {
use std::path::PathBuf;
use super::TriggerKind;
#[test]
fn trigger_kind_debug_includes_log_path() {
let echo_dbg = format!("{:?}", TriggerKind::Echo { label: None });
assert!(echo_dbg.contains("Echo"));
let log_dbg = format!(
"{:?}",
TriggerKind::Log {
path: PathBuf::from("/tmp/x.log")
}
);
assert!(log_dbg.contains("Log"), "got: {log_dbg}");
assert!(log_dbg.contains("/tmp/x.log"), "got: {log_dbg}");
}
}