Skip to main content

Rules

Struct Rules 

Source
pub struct Rules {
    pub rules: Vec<Rule>,
}
Expand description

A set of rules, as a file a fleet shares.

{ "$schema": "https://pamoja.molex.cloud/schema/rules-1.json", "rules": [ ... ] }

The $schema is optional; it names the format the file is written in, and an editor reads it to check the file as it is typed. A field the format does not have is refused, with the nearest one it does.

Fields§

§rules: Vec<Rule>

The rules, in the order they are checked against each message.

Implementations§

Source§

impl Rules

Source

pub const SCHEMA: &'static str = "https://pamoja.molex.cloud/schema/rules-1.json"

The address of the published JSON Schema for the rule file format this build writes, which to_json names as the file’s $schema.

Source

pub fn new() -> Self

Starts with no rules.

§Returns

An empty set.

Source

pub fn with(self, rule: Rule) -> Self

Adds a rule.

§Arguments
  • rule - the rule to add.
§Returns

The set, for chaining.

Source

pub fn from_json(text: &str) -> Result<Self>

Loads rules from their JSON file.

§Arguments
  • text - the file’s contents.
§Returns

The rules.

§Errors

Returns [Error::Codec] if the text is not a rule file, is written in a format this build does not read, carries a field the format does not have, or holds a rule check refuses. A misspelled field is reported with the name it was probably meant to be.

§Examples
use pamoja_profile::Rules;

let file = r#"{
  "$schema": "https://pamoja.molex.cloud/schema/rules-1.json",
  "rules": [
    { "name": "vent-when-hot",
      "when": { "topic": "greenhouse/air", "above": 30.0, "hysteresis": 3.0 },
      "then": [ { "drive": "roof-vent", "on": true } ],
      "otherwise": [ { "drive": "roof-vent", "on": false } ] }
  ]
}"#;
let rules = Rules::from_json(file)?;
assert_eq!(rules.rules[0].when.threshold, 30.0);

let typo = file.replace("\"hysteresis\"", "\"hysterisis\"");
let refused = Rules::from_json(&typo).unwrap_err().to_string();
assert!(refused.contains("did you mean `hysteresis`?"), "{refused}");
Source

pub fn to_json(&self) -> Result<String>

Writes the rules as the JSON file a fleet shares.

§Returns

The pretty-printed JSON text.

§Errors

Returns [Error::Codec] if the rules cannot be serialized.

Source

pub fn check(&self) -> Result<()>

Checks the rules for what a file can say that no engine could run.

§Returns

Nothing when every rule is usable.

§Errors

Returns [Error::Codec] naming the first problem and the rule it is in: a rule without a name or with a name another rule has, a condition without a topic or watching a filter with + or #, a threshold that is not a finite number, a hysteresis that is not a finite number of zero or more, a rule with nothing to do, or an action naming no actuator, no topic, or a filter to publish to.

§Examples
use pamoja_profile::{Action, Condition, Rule, Rules};

// A rule on a filter would subscribe to every bed and match none of them, since a
// rule compares each message's topic with its own exactly.
let every_bed = Rules::new().with(
    Rule::new("water-when-dry", Condition::below("garden/+/moisture", 30.0))
        .then(Action::Drive { actuator: "bed-valve".into(), on: true }),
);
let refused = every_bed.check().unwrap_err().to_string();
assert!(refused.contains("a rule watches one topic exactly"));

Trait Implementations§

Source§

impl Clone for Rules

Source§

fn clone(&self) -> Rules

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Rules

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Rules

Source§

fn default() -> Rules

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Rules

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for Rules

Source§

fn eq(&self, other: &Rules) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for Rules

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Rules

Auto Trait Implementations§

§

impl Freeze for Rules

§

impl RefUnwindSafe for Rules

§

impl Send for Rules

§

impl Sync for Rules

§

impl Unpin for Rules

§

impl UnsafeUnpin for Rules

§

impl UnwindSafe for Rules

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.