zenoh_protocol_core/key_expr/
include.rs

1//
2// Copyright (c) 2022 ZettaScale Technology
3//
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7// which is available at https://www.apache.org/licenses/LICENSE-2.0.
8//
9// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10//
11// Contributors:
12//   ZettaScale Zenoh Team, <zenoh@zettascale.tech>
13//
14use super::{keyexpr, utils::Split, DELIMITER, DOUBLE_WILD, STAR_DSL};
15
16pub const DEFAULT_INCLUDER: LTRIncluder = LTRIncluder;
17
18pub trait Includer<Left, Right> {
19    /// Returns `true` if the set defined by `left` includes the one defined by `right`
20    fn includes(&self, left: Left, right: Right) -> bool;
21}
22
23impl<T: for<'a> Includer<&'a [u8], &'a [u8]>> Includer<&keyexpr, &keyexpr> for T {
24    fn includes(&self, left: &keyexpr, right: &keyexpr) -> bool {
25        let left = left.as_bytes();
26        let right = right.as_bytes();
27        if left == right || left == b"**" {
28            return true;
29        }
30        self.includes(left, right)
31    }
32}
33
34pub struct LTRIncluder;
35impl Includer<&[u8], &[u8]> for LTRIncluder {
36    fn includes(&self, mut left: &[u8], mut right: &[u8]) -> bool {
37        loop {
38            let (lchunk, lrest) = left.split_once(&DELIMITER);
39            let lempty = lrest.is_empty();
40            if lchunk == DOUBLE_WILD {
41                if lempty || self.includes(lrest, right) {
42                    return true;
43                }
44                right = right.split_once(&DELIMITER).1;
45                if right.is_empty() {
46                    return false;
47                }
48            } else {
49                let (rchunk, rrest) = right.split_once(&DELIMITER);
50                if rchunk.is_empty() || !self.non_double_wild_chunk_includes(lchunk, rchunk) {
51                    return false;
52                }
53                let rempty = rrest.is_empty();
54                if lempty {
55                    return rempty;
56                }
57                left = lrest;
58                right = rrest;
59            }
60        }
61    }
62}
63
64impl LTRIncluder {
65    fn non_double_wild_chunk_includes(&self, lchunk: &[u8], rchunk: &[u8]) -> bool {
66        if lchunk == b"*" || lchunk == rchunk {
67            true
68        } else if lchunk.contains(&b'$') {
69            let mut spleft = lchunk.splitter(STAR_DSL);
70            if let Some(rchunk) = rchunk.strip_prefix(spleft.next().unwrap()) {
71                if let Some(mut rchunk) = rchunk.strip_suffix(spleft.next_back().unwrap()) {
72                    for needle in spleft {
73                        let needle_len = needle.len();
74                        if let Some(position) =
75                            rchunk.windows(needle_len).position(|right| right == needle)
76                        {
77                            rchunk = &rchunk[position + needle_len..]
78                        } else {
79                            return false;
80                        }
81                    }
82                    true
83                } else {
84                    false
85                }
86            } else {
87                false
88            }
89        } else {
90            false
91        }
92    }
93}