rustlogic/operators.rs
1//! Contains all the functionality to adjust operators when parsing
2
3macro_rules! operator_getset {
4 ($setter:ident, $getter:ident) => {
5 /// Setter used to adjust operator within [OperatorSet](struct.OperatorSet.html)
6 pub fn $setter(self: Self, to: &str) -> OperatorSet {
7 let mut operator_clone = self.clone();
8 operator_clone.$getter = String::from(to);
9 operator_clone
10 }
11
12 /// Getter used to fetch operator within [OperatorSet](struct.OperatorSet.html)
13 pub fn $getter(self: &Self) -> &str {
14 &self.$getter[..]
15 }
16 };
17}
18
19/// OperatorSet struct used to specify non-default operator symbols for [custom_parse](../../fn.custom_parse.html) function
20///
21/// # Examples
22/// ## Using the common sets
23/// ```
24/// use rustlogic::operators;
25///
26/// let mathematical_version = operators::common_sets::mathematical();
27///
28/// // We create a parsed logic node
29/// let parse_result = rustlogic::custom_parse("(![A]*[B])+[C]", &mathematical_version);
30/// # assert!(parse_result.is_ok());
31///
32/// // -- snipp
33/// ```
34/// For all the common sets [click here](common_sets/index.html)
35///
36/// ## Defining your own sets
37/// ```
38/// use rustlogic::operators::OperatorSet;
39///
40/// // Create a operator set with words
41/// let worded_version = OperatorSet::new(
42/// " AND ", " OR ", "NOT ", "TRUE", "FALSE", "(", ")", "$[", "]",
43/// );
44///
45/// // Attempt to parse a logical string
46/// let parse_result = rustlogic::custom_parse("(NOT $[A] AND $[B]) OR $[C]", &worded_version);
47/// # assert!(parse_result.is_ok());
48///
49/// // -- snipp
50/// ```
51///
52/// ## Adjusting a pre-existing set
53/// ```
54/// use rustlogic::operators;
55///
56/// // Adjust the group brackets to be curly
57/// let curly_bracket_groups = operators::common_sets::default()
58/// .adjust_group_open("{")
59/// .adjust_group_close("}");
60///
61/// // We create a parsed logic node
62/// let parse_result = rustlogic::custom_parse("{~[A]&[B]}|[C]", &curly_bracket_groups);
63/// # assert!(parse_result.is_ok());
64///
65/// // -- snipp
66/// ```
67#[derive(Clone)]
68pub struct OperatorSet {
69 group_open: String,
70 group_close: String,
71 and: String,
72 or: String,
73 not: String,
74 true_symbol: String,
75 false_symbol: String,
76 variable_open: String,
77 variable_close: String,
78}
79
80impl OperatorSet {
81 pub fn new(
82 and: &str,
83 or: &str,
84 not: &str,
85 true_symbol: &str,
86 false_symbol: &str,
87 group_open: &str,
88 group_close: &str,
89 variable_open: &str,
90 variable_close: &str,
91 ) -> OperatorSet {
92 OperatorSet {
93 group_open: String::from(group_open),
94 group_close: String::from(group_close),
95 and: String::from(and),
96 or: String::from(or),
97 not: String::from(not),
98 true_symbol: String::from(true_symbol),
99 false_symbol: String::from(false_symbol),
100 variable_open: String::from(variable_open),
101 variable_close: String::from(variable_close),
102 }
103 }
104
105 operator_getset!(adjust_and, and);
106 operator_getset!(adjust_or, or);
107 operator_getset!(adjust_not, not);
108 operator_getset!(adjust_true_symbol, true_symbol);
109 operator_getset!(adjust_false_symbol, false_symbol);
110 operator_getset!(adjust_group_open, group_open);
111 operator_getset!(adjust_group_close, group_close);
112 operator_getset!(adjust_variable_open, variable_open);
113 operator_getset!(adjust_variable_close, variable_close);
114}
115
116/// Some common operator sets used
117pub mod common_sets {
118 use super::OperatorSet;
119
120 /// Returns the default set of operators used with the [parse](../../fn.parse.html) function
121 ///
122 /// This includes "&", "|" and "~" as logical functions,
123 /// "1" and "0" as absolute values, "(" and ")" as group opener and closer
124 /// and "[" and "]" as variable opener and closer.
125 pub fn default() -> OperatorSet {
126 OperatorSet::new("&", "|", "~", "1", "0", "(", ")", "[", "]")
127 }
128
129 /// Returns an english worded set of operators
130 ///
131 /// This includes " AND ", " OR " and "NOT " as logical functions,
132 /// "TRUE" and "FALSE" as absolute values, "(" and ")" as group opener and closer
133 /// and "$[" and "]" as variable opener and closer.
134 pub fn worded() -> OperatorSet {
135 OperatorSet::new(
136 " AND ", " OR ", "NOT ", "TRUE", "FALSE", "(", ")", "$[", "]",
137 )
138 }
139
140 /// Returns a mathematical set of operators
141 ///
142 /// This includes "*", "+" and "!" as logical functions,
143 /// "1" and "0" as absolute values, "(" and ")" as group opener and closer
144 /// and "[" and "]" as variable opener and closer.
145 pub fn mathematical() -> OperatorSet {
146 OperatorSet::new("*", "+", "!", "1", "0", "(", ")", "[", "]")
147 }
148
149 /// Returns the display set, this is the default set with some whitespace added
150 ///
151 /// This includes " & ", " | " and "~ " as logical functions,
152 /// "1" and "0" as absolute values, "( " and " )" as group opener and closer
153 /// and "[ " and " ]" as variable opener and closer.
154 pub fn display() -> OperatorSet {
155 OperatorSet::new(" & ", " | ", "~", "1", "0", "( ", " )", "[", "]")
156 }
157}