1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// OPCUA for Rust
// SPDX-License-Identifier: MPL-2.0
// Copyright (C) 2017-2022 Adam Lock

use std::convert::TryFrom;

use crate::{
    attribute::AttributeId,
    node_ids::ObjectId,
    service_types::{
        AttributeOperand, ContentFilter, ContentFilterElement, ElementOperand, FilterOperator,
        LiteralOperand, SimpleAttributeOperand,
    },
    status_code::StatusCode,
    DecodingOptions, ExtensionObject, NodeId, QualifiedName, UAString, Variant,
};

#[derive(PartialEq)]
pub enum OperandType {
    ElementOperand,
    LiteralOperand,
    AttributeOperand,
    SimpleAttributeOperand,
}

pub enum Operand {
    ElementOperand(ElementOperand),
    LiteralOperand(LiteralOperand),
    AttributeOperand(AttributeOperand),
    SimpleAttributeOperand(SimpleAttributeOperand),
}

impl From<i8> for LiteralOperand {
    fn from(v: i8) -> Self {
        Self::from(Variant::from(v))
    }
}

impl From<u8> for LiteralOperand {
    fn from(v: u8) -> Self {
        Self::from(Variant::from(v))
    }
}

impl From<i16> for LiteralOperand {
    fn from(v: i16) -> Self {
        Self::from(Variant::from(v))
    }
}

impl From<u16> for LiteralOperand {
    fn from(v: u16) -> Self {
        Self::from(Variant::from(v))
    }
}

impl From<i32> for LiteralOperand {
    fn from(v: i32) -> Self {
        Self::from(Variant::from(v))
    }
}

impl From<u32> for LiteralOperand {
    fn from(v: u32) -> Self {
        Self::from(Variant::from(v))
    }
}

impl From<f32> for LiteralOperand {
    fn from(v: f32) -> Self {
        Self::from(Variant::from(v))
    }
}

impl From<f64> for LiteralOperand {
    fn from(v: f64) -> Self {
        Self::from(Variant::from(v))
    }
}

impl From<bool> for LiteralOperand {
    fn from(v: bool) -> Self {
        Self::from(Variant::from(v))
    }
}

impl From<&str> for LiteralOperand {
    fn from(v: &str) -> Self {
        Self::from(Variant::from(v))
    }
}

impl From<()> for LiteralOperand {
    fn from(_v: ()) -> Self {
        Self::from(Variant::from(()))
    }
}

impl From<Variant> for LiteralOperand {
    fn from(v: Variant) -> Self {
        LiteralOperand { value: v }
    }
}

impl TryFrom<&ExtensionObject> for Operand {
    type Error = StatusCode;

    fn try_from(v: &ExtensionObject) -> Result<Self, Self::Error> {
        let object_id = v
            .object_id()
            .map_err(|_| StatusCode::BadFilterOperandInvalid)?;
        let decoding_options = DecodingOptions::default();
        let operand = match object_id {
            ObjectId::ElementOperand_Encoding_DefaultBinary => {
                Operand::ElementOperand(v.decode_inner::<ElementOperand>(&decoding_options)?)
            }
            ObjectId::LiteralOperand_Encoding_DefaultBinary => {
                Operand::LiteralOperand(v.decode_inner::<LiteralOperand>(&decoding_options)?)
            }
            ObjectId::AttributeOperand_Encoding_DefaultBinary => {
                Operand::AttributeOperand(v.decode_inner::<AttributeOperand>(&decoding_options)?)
            }
            ObjectId::SimpleAttributeOperand_Encoding_DefaultBinary => {
                Operand::SimpleAttributeOperand(
                    v.decode_inner::<SimpleAttributeOperand>(&decoding_options)?,
                )
            }
            _ => {
                return Err(StatusCode::BadFilterOperandInvalid);
            }
        };
        Ok(operand)
    }
}

impl From<&Operand> for ExtensionObject {
    fn from(v: &Operand) -> Self {
        match v {
            Operand::ElementOperand(ref op) => {
                ExtensionObject::from_encodable(ObjectId::ElementOperand_Encoding_DefaultBinary, op)
            }
            Operand::LiteralOperand(ref op) => {
                ExtensionObject::from_encodable(ObjectId::LiteralOperand_Encoding_DefaultBinary, op)
            }
            Operand::AttributeOperand(ref op) => ExtensionObject::from_encodable(
                ObjectId::AttributeOperand_Encoding_DefaultBinary,
                op,
            ),
            Operand::SimpleAttributeOperand(ref op) => ExtensionObject::from_encodable(
                ObjectId::SimpleAttributeOperand_Encoding_DefaultBinary,
                op,
            ),
        }
    }
}

impl From<Operand> for ExtensionObject {
    fn from(v: Operand) -> Self {
        Self::from(&v)
    }
}

impl From<(FilterOperator, Vec<Operand>)> for ContentFilterElement {
    fn from(v: (FilterOperator, Vec<Operand>)) -> ContentFilterElement {
        ContentFilterElement {
            filter_operator: v.0,
            filter_operands: Some(v.1.iter().map(|op| op.into()).collect()),
        }
    }
}

impl From<ElementOperand> for Operand {
    fn from(v: ElementOperand) -> Operand {
        Operand::ElementOperand(v)
    }
}

impl From<LiteralOperand> for Operand {
    fn from(v: LiteralOperand) -> Self {
        Operand::LiteralOperand(v)
    }
}

impl From<SimpleAttributeOperand> for Operand {
    fn from(v: SimpleAttributeOperand) -> Self {
        Operand::SimpleAttributeOperand(v)
    }
}

impl Operand {
    pub fn element(index: u32) -> Operand {
        ElementOperand { index }.into()
    }

    pub fn literal<T>(literal: T) -> Operand
    where
        T: Into<LiteralOperand>,
    {
        Operand::LiteralOperand(literal.into())
    }

    /// Creates a simple attribute operand. The browse path is the browse name using / as a separator.
    pub fn simple_attribute<T>(
        type_definition_id: T,
        browse_path: &str,
        attribute_id: AttributeId,
        index_range: UAString,
    ) -> Operand
    where
        T: Into<NodeId>,
    {
        SimpleAttributeOperand::new(type_definition_id, browse_path, attribute_id, index_range)
            .into()
    }

    pub fn operand_type(&self) -> OperandType {
        match self {
            Operand::ElementOperand(_) => OperandType::ElementOperand,
            Operand::LiteralOperand(_) => OperandType::LiteralOperand,
            Operand::AttributeOperand(_) => OperandType::AttributeOperand,
            Operand::SimpleAttributeOperand(_) => OperandType::SimpleAttributeOperand,
        }
    }

    pub fn is_element(&self) -> bool {
        self.operand_type() == OperandType::ElementOperand
    }

    pub fn is_literal(&self) -> bool {
        self.operand_type() == OperandType::LiteralOperand
    }

    pub fn is_attribute(&self) -> bool {
        self.operand_type() == OperandType::AttributeOperand
    }

    pub fn is_simple_attribute(&self) -> bool {
        self.operand_type() == OperandType::SimpleAttributeOperand
    }
}

/// This is a convenience for building [`ContentFilter`] using operands as building blocks
/// This builder does not check to see that the content filter is valid, i.e. if you
/// reference an element by index that doesn't exist, or introduce a loop then you will
/// not get an error until you feed it to a server and the server rejects it or breaks.
///
/// The builder takes generic types to make it easier to work with. Operands are converted to
/// extension objects.
pub struct ContentFilterBuilder {
    elements: Vec<ContentFilterElement>,
}

impl Default for ContentFilterBuilder {
    fn default() -> Self {
        ContentFilterBuilder {
            elements: Vec::with_capacity(20),
        }
    }
}

impl ContentFilterBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    fn add_element(
        mut self,
        filter_operator: FilterOperator,
        filter_operands: Vec<Operand>,
    ) -> Self {
        let filter_operands = filter_operands.iter().map(ExtensionObject::from).collect();
        self.elements.push(ContentFilterElement {
            filter_operator,
            filter_operands: Some(filter_operands),
        });
        self
    }

    pub fn eq<T, S>(self, o1: T, o2: S) -> Self
    where
        T: Into<Operand>,
        S: Into<Operand>,
    {
        self.add_element(FilterOperator::Equals, vec![o1.into(), o2.into()])
    }

    pub fn null<T>(self, o1: T) -> Self
    where
        T: Into<Operand>,
    {
        self.add_element(FilterOperator::IsNull, vec![o1.into()])
    }

    pub fn gt<T, S>(self, o1: T, o2: S) -> Self
    where
        T: Into<Operand>,
        S: Into<Operand>,
    {
        self.add_element(FilterOperator::GreaterThan, vec![o1.into(), o2.into()])
    }

    pub fn lt<T, S>(self, o1: T, o2: S) -> Self
    where
        T: Into<Operand>,
        S: Into<Operand>,
    {
        self.add_element(FilterOperator::LessThan, vec![o1.into(), o2.into()])
    }

    pub fn gte<T, S>(self, o1: T, o2: S) -> Self
    where
        T: Into<Operand>,
        S: Into<Operand>,
    {
        self.add_element(
            FilterOperator::GreaterThanOrEqual,
            vec![o1.into(), o2.into()],
        )
    }

    pub fn lte<T, S>(self, o1: T, o2: S) -> Self
    where
        T: Into<Operand>,
        S: Into<Operand>,
    {
        self.add_element(FilterOperator::LessThanOrEqual, vec![o1.into(), o2.into()])
    }

    pub fn like<T, S>(self, o1: T, o2: S) -> Self
    where
        T: Into<Operand>,
        S: Into<Operand>,
    {
        self.add_element(FilterOperator::Like, vec![o1.into(), o2.into()])
    }

    pub fn not<T>(self, o1: T) -> Self
    where
        T: Into<Operand>,
    {
        self.add_element(FilterOperator::Not, vec![o1.into()])
    }

    pub fn between<T, S, U>(self, o1: T, o2: S, o3: U) -> Self
    where
        T: Into<Operand>,
        S: Into<Operand>,
        U: Into<Operand>,
    {
        self.add_element(
            FilterOperator::Between,
            vec![o1.into(), o2.into(), o3.into()],
        )
    }

    pub fn in_list<T, S>(self, o1: T, list_items: Vec<S>) -> Self
    where
        T: Into<Operand>,
        S: Into<Operand>,
    {
        // Make a list from the operand and then the items
        let mut filter_operands = Vec::with_capacity(list_items.len() + 1);
        filter_operands.push(o1.into());
        list_items.into_iter().for_each(|list_item| {
            filter_operands.push(list_item.into());
        });
        self.add_element(FilterOperator::InList, filter_operands)
    }

    pub fn and<T, S>(self, o1: T, o2: S) -> Self
    where
        T: Into<Operand>,
        S: Into<Operand>,
    {
        self.add_element(FilterOperator::And, vec![o1.into(), o2.into()])
    }

    pub fn or<T, S>(self, o1: T, o2: S) -> Self
    where
        T: Into<Operand>,
        S: Into<Operand>,
    {
        self.add_element(FilterOperator::Or, vec![o1.into(), o2.into()])
    }

    pub fn cast<T, S>(self, o1: T, o2: S) -> Self
    where
        T: Into<Operand>,
        S: Into<Operand>,
    {
        self.add_element(FilterOperator::Cast, vec![o1.into(), o2.into()])
    }

    pub fn bitwise_and<T, S>(self, o1: T, o2: S) -> Self
    where
        T: Into<Operand>,
        S: Into<Operand>,
    {
        self.add_element(FilterOperator::BitwiseAnd, vec![o1.into(), o2.into()])
    }

    pub fn bitwise_or<T, S>(self, o1: T, o2: S) -> Self
    where
        T: Into<Operand>,
        S: Into<Operand>,
    {
        self.add_element(FilterOperator::BitwiseOr, vec![o1.into(), o2.into()])
    }

    pub fn build(self) -> ContentFilter {
        ContentFilter {
            elements: Some(self.elements),
        }
    }
}

impl SimpleAttributeOperand {
    pub fn new<T>(
        type_definition_id: T,
        browse_path: &str,
        attribute_id: AttributeId,
        index_range: UAString,
    ) -> Self
    where
        T: Into<NodeId>,
    {
        // An improbable string to replace escaped forward slashes.
        const ESCAPE_PATTERN: &str = "###!!!###@@@$$$$";
        // Any escaped forward slashes will be replaced temporarily to allow split to work.
        let browse_path = browse_path.replace(r"\/", ESCAPE_PATTERN);
        // If we had a regex with look around support then we could split a pattern such as `r"(?<!\\)/"` where it
        // matches only if the pattern `/` isn't preceded by a backslash. Unfortunately the regex crate doesn't offer
        // this so an escaped forward slash is replaced with an improbable string instead.
        let browse_path = browse_path
            .split('/')
            .map(|s| QualifiedName::new(0, s.replace(ESCAPE_PATTERN, "/")))
            .collect();
        SimpleAttributeOperand {
            type_definition_id: type_definition_id.into(),
            browse_path: Some(browse_path),
            attribute_id: attribute_id as u32,
            index_range,
        }
    }
}