Skip to main content

rust_runes/
rule.rs

1use crate::ast::Expression;
2use serde::{Deserialize, Serialize};
3
4/// A rule: when its condition holds, its actions run.
5#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
6pub struct Rule {
7    /// Unique name, used by `Retract` and in errors.
8    pub name: String,
9    /// Optional description.
10    pub description: Option<String>,
11    /// Priority: when several rules match, the highest salience fires first.
12    pub salience: i32,
13    /// The condition, which must evaluate to a boolean.
14    pub when_condition: Expression,
15    /// The actions: assignments and function or method calls.
16    pub then_actions: Vec<Expression>,
17}
18
19impl Rule {
20    /// A rule with no description.
21    pub fn new(
22        name: String,
23        salience: i32,
24        when_condition: Expression,
25        then_actions: Vec<Expression>,
26    ) -> Self {
27        Self {
28            name,
29            description: None,
30            salience,
31            when_condition,
32            then_actions,
33        }
34    }
35
36    /// Sets the description.
37    pub fn with_description(mut self, description: String) -> Self {
38        self.description = Some(description);
39        self
40    }
41}