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