xio_instructionset/
lib.rs

1#![deny(missing_docs)]
2
3//! The data structures representing a XIO instruction set.
4
5#[macro_use]
6extern crate serde_derive;
7
8extern crate indexmap;
9extern crate ommui_file_loading;
10extern crate ommui_string_patterns;
11extern crate serde;
12extern crate xio_base_datatypes;
13
14use indexmap::IndexMap;
15use ommui_string_patterns::{
16    freetextstring, idstring, idstring_maybe_empty,
17};
18use xio_base_datatypes as base;
19
20mod hexnumber;
21
22/// Functionality for loading a XIO instruction set from files.
23pub mod filesystem;
24
25/// A map containing instructions indexed by their identifier.
26pub type InstructionMap = IndexMap<String, Instruction>;
27
28/// Description of an instruction type.
29#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
30#[serde(rename_all = "camelCase", deny_unknown_fields)]
31pub struct Instruction {
32    /// The instruction code.
33    #[serde(with = "hexnumber")]
34    pub code: u16,
35
36    /// The description of the instruction.
37    #[serde(with = "freetextstring")]
38    pub description: String,
39
40    /// The format string in Qt format syntax.
41    ///
42    /// The Qt string formatting replaces %1, %2, …, %99 in the provided
43    /// order. See http://doc.qt.io/qt-5/qstring.html#arg
44    #[serde(
45        with = "freetextstring",
46        default,
47        skip_serializing_if = "String::is_empty"
48    )]
49    pub formatstring: String,
50
51    /// The parameters which are required for the instruction.
52    #[serde(default, skip_serializing_if = "Vec::is_empty")]
53    pub parameters: Vec<InstructionParameter>,
54}
55
56/// Instruction categories.
57pub enum InstructionCategory {
58    /// Commands without a time extent (e.g. setValue).
59    CommandWithoutTimeExtent,
60
61    /// Commands with time extent (e.g. wait);
62    CommandWithTimeExtent,
63
64    /// Conditions for commands with time extent.
65    Condition,
66
67    /// Invalid category.
68    Invalid,
69}
70
71impl Instruction {
72    /// Get the category of an instruction.
73    pub fn category(&self) -> InstructionCategory {
74        use std::ops::Shr;
75        use InstructionCategory::*;
76        match self.code.shr(8) & 0b1111u16 {
77            0b0000u16 => CommandWithoutTimeExtent,
78            0b0100u16 => CommandWithTimeExtent,
79            0b1000u16 => Condition,
80            _ => Invalid,
81        }
82    }
83}
84
85/// Description of an instruction parameter type.
86#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
87#[serde(rename_all = "camelCase", deny_unknown_fields)]
88pub struct InstructionParameter {
89    /// The id string of the parameter.
90    #[serde(with = "idstring")]
91    pub id: String,
92
93    /// The group to which the parameter belongs.
94    ///
95    /// If empty, the parameter implicitly has it's own group where
96    /// it is the only member.
97    #[serde(
98        with = "idstring_maybe_empty",
99        default,
100        skip_serializing_if = "String::is_empty"
101    )]
102    pub group: String,
103
104    /// The allowed types of parameters.
105    ///
106    /// Each parameter in a group must have the same type.
107    #[serde(rename = "type")]
108    pub parameter_type: Vec<base::DataType>,
109
110    /// The permitted storage type for the parameter.
111    pub storage: base::StorageTypeWithMixed,
112
113    /// A human-readable description of the parameter.
114    #[serde(with = "freetextstring")]
115    pub description: String,
116}