zenoh_keyexpr/key_expr/
include.rs

1//
2// Copyright (c) 2023 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::{intersect::MayHaveVerbatim, 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 {
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) = Split::split_once(left, &DELIMITER);
39            let lempty = lrest.is_empty();
40            if lchunk == DOUBLE_WILD {
41                if (lempty && !right.has_verbatim()) || (!lempty && self.includes(lrest, right)) {
42                    return true;
43                }
44                if right.has_direct_verbatim() {
45                    return false;
46                }
47                right = Split::split_once(right, &DELIMITER).1;
48                if right.is_empty() {
49                    return false;
50                }
51            } else {
52                let (rchunk, rrest) = Split::split_once(right, &DELIMITER);
53                if rchunk.is_empty()
54                    || rchunk == DOUBLE_WILD
55                    || !self.non_double_wild_chunk_includes(lchunk, rchunk)
56                {
57                    return false;
58                }
59                let rempty = rrest.is_empty();
60                if lempty {
61                    return rempty;
62                }
63                left = lrest;
64                right = rrest;
65            }
66        }
67    }
68}
69
70impl LTRIncluder {
71    fn non_double_wild_chunk_includes(&self, lchunk: &[u8], rchunk: &[u8]) -> bool {
72        if lchunk == rchunk {
73            true
74        } else if unsafe {
75            lchunk.has_direct_verbatim_non_empty() || rchunk.has_direct_verbatim_non_empty()
76        } {
77            false
78        } else if lchunk == b"*" {
79            true
80        } else if lchunk.contains(&b'$') {
81            let mut spleft = lchunk.splitter(STAR_DSL);
82            if let Some(rchunk) = rchunk.strip_prefix(spleft.next().unwrap()) {
83                if let Some(mut rchunk) = rchunk.strip_suffix(spleft.next_back().unwrap()) {
84                    for needle in spleft {
85                        let needle_len = needle.len();
86                        if let Some(position) =
87                            rchunk.windows(needle_len).position(|right| right == needle)
88                        {
89                            rchunk = &rchunk[position + needle_len..]
90                        } else {
91                            return false;
92                        }
93                    }
94                    true
95                } else {
96                    false
97                }
98            } else {
99                false
100            }
101        } else {
102            false
103        }
104    }
105}