sieve/compiler/grammar/tests/
test_specialuse.rs

1/*
2 * SPDX-FileCopyrightText: 2020 Stalwart Labs Ltd <hello@stalw.art>
3 *
4 * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL
5 */
6
7
8
9use crate::compiler::{grammar::instruction::CompilerState, lexer::Token, CompileError, Value};
10
11use crate::compiler::grammar::test::Test;
12
13/*
14   Usage:  specialuse_exists [<mailbox: string>]
15                             <special-use-attrs: string-list>
16*/
17
18#[derive(Debug, Clone, PartialEq, Eq)]
19#[cfg_attr(
20    any(test, feature = "serde"),
21    derive(serde::Serialize, serde::Deserialize)
22)]
23#[cfg_attr(
24    feature = "rkyv",
25    derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)
26)]
27pub(crate) struct TestSpecialUseExists {
28    pub mailbox: Option<Value>,
29    pub attributes: Vec<Value>,
30    pub is_not: bool,
31}
32
33impl CompilerState<'_> {
34    pub(crate) fn parse_test_specialuseexists(&mut self) -> Result<Test, CompileError> {
35        let mut maybe_attributes = self.parse_strings(false)?;
36
37        match self.tokens.peek().map(|r| r.map(|t| &t.token)) {
38            Some(Ok(Token::StringConstant(_) | Token::StringVariable(_) | Token::BracketOpen)) => {
39                if maybe_attributes.len() == 1 {
40                    Ok(Test::SpecialUseExists(TestSpecialUseExists {
41                        mailbox: maybe_attributes.pop(),
42                        attributes: self.parse_strings(false)?,
43                        is_not: false,
44                    }))
45                } else {
46                    Err(self.tokens.unwrap_next()?.expected("string"))
47                }
48            }
49            _ => Ok(Test::SpecialUseExists(TestSpecialUseExists {
50                mailbox: None,
51                attributes: maybe_attributes,
52                is_not: false,
53            })),
54        }
55    }
56}