Skip to main content

weirdnet_rules/
model.rs

1//! What a rule is: when it runs, what must hold, and what it does.
2use serde::{Deserialize, Serialize};
3
4/// Spec version written by this crate.
5pub const SPEC_VERSION: u32 = 1;
6
7/// How a condition compares.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
9pub enum Op {
10    #[serde(rename = "=")]
11    Eq,
12    #[serde(rename = "!=")]
13    Ne,
14    #[serde(rename = "~")]
15    Contains,
16    #[serde(rename = "!~")]
17    NotContains,
18    #[serde(rename = ">")]
19    Gt,
20    #[serde(rename = ">=")]
21    Ge,
22    #[serde(rename = "<")]
23    Lt,
24    #[serde(rename = "<=")]
25    Le,
26    #[serde(rename = "?")]
27    Exists,
28    #[serde(rename = "!?")]
29    Missing,
30}
31
32impl Op {
33    /// Longest first, so `>=` is not read as `>`.
34    pub const ALL: [Op; 10] = [Op::Ne, Op::NotContains, Op::Ge, Op::Le, Op::Missing, Op::Eq, Op::Contains, Op::Gt, Op::Lt, Op::Exists];
35    pub fn as_str(self) -> &'static str {
36        match self {
37            Op::Eq => "=",
38            Op::Ne => "!=",
39            Op::Contains => "~",
40            Op::NotContains => "!~",
41            Op::Gt => ">",
42            Op::Ge => ">=",
43            Op::Lt => "<",
44            Op::Le => "<=",
45            Op::Exists => "?",
46            Op::Missing => "!?",
47        }
48    }
49    pub fn unary(self) -> bool {
50        matches!(self, Op::Exists | Op::Missing)
51    }
52}
53
54/// `field op value`, e.g. `inserted > 0`, `format = pdf`, `summary ~ btc`.
55#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
56pub struct Condition {
57    pub field: String,
58    pub op: Op,
59    #[serde(default)]
60    pub value: String,
61}
62
63/// When a scheduled rule runs: every so often (`every 6h`) or at cron times
64/// (`cron "0 3 * * *"`). See [`crate::schedule::next_run`].
65#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
66#[serde(rename_all = "snake_case")]
67pub enum Schedule {
68    /// Milliseconds between runs.
69    Every(i64),
70    Cron(String),
71}
72
73/// A then-action of the application's own type: the engine only needs its verb (for run
74/// results and failure messages).
75pub trait ThenAction {
76    fn verb(&self) -> &'static str;
77}
78
79fn spec_version() -> u32 {
80    SPEC_VERSION
81}
82
83/// When a rule runs and what it does; `T` is the application's then-action.
84#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
85pub struct RuleSpec<T> {
86    #[serde(default = "spec_version")]
87    pub version: u32,
88    /// Event kinds (`import`, `journal.*`, `*`); any one matching is enough.
89    pub on: Vec<String>,
90    /// All must hold.
91    #[serde(default, rename = "if")]
92    pub when: Vec<Condition>,
93    pub then: Vec<T>,
94    /// Only for events about where the rule lives (the host decides what that means).
95    #[serde(default)]
96    pub related: bool,
97    /// Run on a schedule as well.
98    #[serde(default, skip_serializing_if = "Option::is_none")]
99    pub schedule: Option<Schedule>,
100}
101
102impl<T> Default for RuleSpec<T> {
103    fn default() -> Self {
104        Self { version: SPEC_VERSION, on: vec![], when: vec![], then: vec![], related: false, schedule: None }
105    }
106}