sieve/compiler/grammar/tests/test_ihave.rs
1/*
2 * Copyright (c) 2020-2023, Stalwart Labs Ltd.
3 *
4 * This file is part of the Stalwart Sieve Interpreter.
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of
9 * the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 * in the LICENSE file at the top-level directory of this distribution.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * You can be released from the requirements of the AGPLv3 license by
20 * purchasing a commercial license. Please contact licensing@stalw.art
21 * for more details.
22*/
23
24use serde::{Deserialize, Serialize};
25
26use crate::compiler::grammar::instruction::{CompilerState, Instruction};
27use crate::compiler::grammar::Capability;
28use crate::compiler::CompileError;
29use crate::compiler::Value;
30
31use crate::compiler::grammar::test::Test;
32
33#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
34pub(crate) struct TestIhave {
35 pub capabilities: Vec<Capability>,
36 pub is_not: bool,
37}
38
39#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
40pub struct Error {
41 pub message: Value,
42}
43
44impl<'x> CompilerState<'x> {
45 pub(crate) fn parse_test_ihave(&mut self) -> Result<Test, CompileError> {
46 Ok(Test::Ihave(TestIhave {
47 capabilities: self
48 .parse_static_strings()?
49 .into_iter()
50 .map(|n| n.into())
51 .collect(),
52 is_not: false,
53 }))
54 }
55
56 pub(crate) fn parse_error(&mut self) -> Result<(), CompileError> {
57 let cmd = Instruction::Error(Error {
58 message: self.parse_string()?,
59 });
60 self.instructions.push(cmd);
61 Ok(())
62 }
63}