use std::fmt;
use std::time::Duration;
pub const DEFAULT_DEADLINE: Duration = Duration::from_secs(1);
pub const PLUGIN_DEADLINE: Duration = Duration::from_secs(5);
pub const LAST: &str = "*";
pub const FAILED_FACT: &str = "hook.failed";
pub trait Point: 'static {
const ID: &'static str;
type Input: Send + Sync + 'static;
type Value: Send + 'static;
const KIND: PointKind = PointKind::Alter;
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum PointKind {
Request,
Event,
#[default]
Alter,
Collect,
}
impl PointKind {
pub fn as_str(self) -> &'static str {
match self {
PointKind::Request => "request",
PointKind::Event => "event",
PointKind::Alter => "alter",
PointKind::Collect => "collect",
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct PointSpec {
pub description: String,
pub deadline: Option<Duration>,
}
impl PointSpec {
pub fn new(description: impl Into<String>) -> PointSpec {
PointSpec {
description: description.into(),
deadline: None,
}
}
pub fn deadline(mut self, deadline: Duration) -> PointSpec {
self.deadline = Some(deadline);
self
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ListenOpts {
pub after: Vec<String>,
pub before: Vec<String>,
pub priority: i32,
pub deadline: Option<Duration>,
pub owner: Option<String>,
pub threaded: bool,
}
impl ListenOpts {
pub fn new() -> ListenOpts {
ListenOpts::default()
}
pub fn after(mut self, id: impl Into<String>) -> ListenOpts {
self.after.push(id.into());
self
}
pub fn before(mut self, id: impl Into<String>) -> ListenOpts {
self.before.push(id.into());
self
}
pub fn last(mut self) -> ListenOpts {
if !self.after.iter().any(|a| a == LAST) {
self.after.push(LAST.to_owned());
}
self
}
pub fn is_last(&self) -> bool {
self.after.iter().any(|a| a == LAST)
}
pub fn priority(mut self, priority: i32) -> ListenOpts {
self.priority = priority;
self
}
pub fn deadline(mut self, deadline: Duration) -> ListenOpts {
self.deadline = Some(deadline);
self
}
pub fn owner(mut self, owner: impl Into<String>) -> ListenOpts {
self.owner = Some(owner.into());
self
}
pub fn threaded(mut self) -> ListenOpts {
self.threaded = true;
self
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HookError(pub String);
impl fmt::Display for HookError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl std::error::Error for HookError {}
impl From<String> for HookError {
fn from(message: String) -> HookError {
HookError(message)
}
}
impl From<&str> for HookError {
fn from(message: &str) -> HookError {
HookError(message.to_owned())
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FailureKind {
Error(String),
Panic(String),
Late {
deadline: Duration,
took: Option<Duration>,
},
MissingTarget(String),
TypeMismatch,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Failure {
pub point: &'static str,
pub listener: String,
pub owner: Option<String>,
pub kind: FailureKind,
}
impl Failure {
pub fn dropped(&self) -> bool {
!matches!(self.kind, FailureKind::MissingTarget(_))
}
}
impl fmt::Display for Failure {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "hook `{}`, listener `{}`: ", self.point, self.listener)?;
match &self.kind {
FailureKind::Error(m) => write!(f, "failed: {m}"),
FailureKind::Panic(m) => write!(f, "panicked: {m}"),
FailureKind::Late {
deadline,
took: Some(t),
} => write!(f, "took {t:?}, past its deadline of {deadline:?}"),
FailureKind::Late { deadline, .. } => {
write!(f, "no answer within its deadline of {deadline:?}")
}
FailureKind::MissingTarget(t) => {
write!(f, "orders itself against `{t}`, which is not there")
}
FailureKind::TypeMismatch => write!(f, "the point is registered with another type"),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Refused {
Duplicate {
point: &'static str,
listener: String,
},
Cycle {
point: &'static str,
path: Vec<String>,
},
TypeMismatch {
point: &'static str,
},
AlreadyDeclared {
point: &'static str,
},
Threaded {
point: &'static str,
listener: String,
},
}
impl fmt::Display for Refused {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Refused::Duplicate { point, listener } => {
write!(f, "hook `{point}` already has a listener `{listener}`")
}
Refused::Cycle { point, path } => {
write!(
f,
"hook `{point}`: listeners order each other in a cycle: {}",
path.join(" -> ")
)
}
Refused::TypeMismatch { point } => {
write!(f, "hook `{point}` is registered with another type")
}
Refused::AlreadyDeclared { point } => write!(f, "hook `{point}` is already declared"),
Refused::Threaded { point, listener } => write!(
f,
"hook `{point}`: the local listener `{listener}` runs on its own thread, not a new one"
),
}
}
}
impl std::error::Error for Refused {}
#[derive(Debug)]
pub struct Altered<V> {
pub value: V,
pub failures: Vec<Failure>,
pub deferred: Vec<String>,
}
#[derive(Debug)]
pub struct Collected<V> {
pub parts: Vec<(String, V)>,
pub failures: Vec<Failure>,
pub deferred: Vec<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PendingRun {
pub point: &'static str,
pub ran: Vec<String>,
pub failures: Vec<Failure>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ListenerInfo {
pub id: String,
pub owner: Option<String>,
pub after: Vec<String>,
pub before: Vec<String>,
pub priority: i32,
pub deadline: Duration,
pub threaded: bool,
pub local: bool,
pub missing: Vec<String>,
pub runs: u64,
pub failures: u64,
pub last_failure: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PointInfo {
pub id: &'static str,
pub kind: PointKind,
pub description: String,
pub declared: bool,
pub deadline: Duration,
pub listeners: Vec<ListenerInfo>,
}