Skip to main content

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 lchunk.has_direct_verbatim_non_empty() || rchunk.has_direct_verbatim_non_empty() {
75            false
76        } else if lchunk == b"*" {
77            true
78        } else if lchunk.contains(&b'$') {
79            let mut spleft = lchunk.splitter(STAR_DSL);
80            if let Some(rchunk) = rchunk.strip_prefix(spleft.next().unwrap()) {
81                if let Some(mut rchunk) = rchunk.strip_suffix(spleft.next_back().unwrap()) {
82                    for needle in spleft {
83                        let needle_len = needle.len();
84                        if let Some(position) =
85                            rchunk.windows(needle_len).position(|right| right == needle)
86                        {
87                            rchunk = &rchunk[position + needle_len..]
88                        } else {
89                            return false;
90                        }
91                    }
92                    true
93                } else {
94                    false
95                }
96            } else {
97                false
98            }
99        } else {
100            false
101        }
102    }
103}