Skip to content

Commit

Permalink
Use Arc instead of references
Browse files Browse the repository at this point in the history
  • Loading branch information
mdecimus committed Oct 14, 2023
1 parent bbb2657 commit 0875287
Show file tree
Hide file tree
Showing 43 changed files with 556 additions and 693 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ name = "sieve"
mail-parser = { version = "0.9", git = "https://github.com/stalwartlabs/mail-parser", features = ["ludicrous_mode", "full_encoding", "serde_support"] }
mail-builder = { version = "0.3", git = "https://github.com/stalwartlabs/mail-builder", features = ["ludicrous_mode"] }
phf = { version = "0.11", features = ["macros"] }
serde = { version = "1.0", features = ["derive"] }
serde = { version = "1.0", features = ["derive", "rc"] }
bincode = "1.3.3"
ahash = { version = "0.8.0" }
fancy-regex = "0.11.0"
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/grammar/actions/action_editheader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl<'x> CompilerState<'x> {
let string = self.parse_string_token(token_info)?;
if field_name.is_none() {
if let Value::Text(header_name) = &string {
if HeaderName::parse(header_name).is_none() {
if HeaderName::parse(header_name.as_ref()).is_none() {
return Err(self
.tokens
.unwrap_next()?
Expand Down Expand Up @@ -175,7 +175,7 @@ impl<'x> CompilerState<'x> {
_ => {
field_name = self.parse_string_token(token_info)?;
if let Value::Text(header_name) = &field_name {
if HeaderName::parse(header_name).is_none() {
if HeaderName::parse(header_name.as_ref()).is_none() {
return Err(self
.tokens
.unwrap_next()?
Expand Down
6 changes: 4 additions & 2 deletions src/compiler/grammar/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* for more details.
*/

use std::sync::Arc;

use serde::{Deserialize, Serialize};

use crate::compiler::{Number, VariableType};
Expand All @@ -44,7 +46,7 @@ pub(crate) enum Expression {
pub(crate) enum Constant {
Integer(i64),
Float(f64),
String(String),
String(Arc<String>),
}

impl Eq for Constant {}
Expand All @@ -60,7 +62,7 @@ impl From<Number> for Constant {

impl From<String> for Constant {
fn from(value: String) -> Self {
Constant::String(value)
Constant::String(value.into())
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/compiler/grammar/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ impl Compiler {
#[cfg(test)]
Token::Unknown(instruction) if instruction.contains("test") => {
let has_arguments = instruction != "test";
let mut arguments = vec![Value::Text(instruction)];
let mut arguments = vec![Value::Text(instruction.into())];

if !has_arguments {
arguments.push(state.parse_string()?);
Expand All @@ -779,9 +779,9 @@ impl Compiler {
Token::Number(n) => {
Value::Number(crate::compiler::Number::Integer(n as i64))
}
Token::Identifier(s) => Value::Text(s.to_string()),
Token::Tag(s) => Value::Text(format!(":{s}")),
Token::Unknown(s) => Value::Text(s),
Token::Identifier(s) => Value::Text(s.to_string().into()),
Token::Tag(s) => Value::Text(format!(":{s}").into()),
Token::Unknown(s) => Value::Text(s.into()),
Token::Semicolon => break,
other => panic!("Invalid test param {other:?}"),
});
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/grammar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ impl<'x> CompilerState<'x> {
Ok(regex) => {
*key = Value::Regex(Regex {
regex,
expr: std::mem::take(expr),
expr: expr.to_string(),
});
}
Err(err) => {
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/grammar/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ impl<'x> CompilerState<'x> {
use crate::compiler::Value;

let mut arguments = Vec::new();
arguments.push(Value::Text(name));
arguments.push(Value::Text(name.into()));
while !matches!(
self.tokens.peek().map(|r| r.map(|t| &t.token)),
Some(Ok(Token::Comma
Expand All @@ -538,9 +538,9 @@ impl<'x> CompilerState<'x> {
Token::Number(n) => {
Value::Number(crate::compiler::Number::Integer(n as i64))
}
Token::Identifier(s) => Value::Text(s.to_string()),
Token::Tag(s) => Value::Text(format!(":{s}")),
Token::Unknown(s) => Value::Text(s),
Token::Identifier(s) => Value::Text(s.to_string().into()),
Token::Tag(s) => Value::Text(format!(":{s}").into()),
Token::Unknown(s) => Value::Text(s.into()),
other => panic!("Invalid test param {other:?}"),
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/grammar/tests/test_date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl<'x> CompilerState<'x> {
if header_name.is_none() {
let header = self.parse_string_token(token_info)?;
if let Value::Text(header_name) = &header {
if HeaderName::parse(header_name).is_none() {
if HeaderName::parse(header_name.as_ref()).is_none() {
return Err(self
.tokens
.unwrap_next()?
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/grammar/tests/test_duplicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl<'x> CompilerState<'x> {
self.tokens.next();
let header = self.parse_string()?;
if let Value::Text(header_name) = &header {
if HeaderName::parse(header_name).is_none() {
if HeaderName::parse(header_name.as_ref()).is_none() {
return Err(self
.tokens
.unwrap_next()?
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/grammar/tests/test_exists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl<'x> CompilerState<'x> {
let headers = self.parse_strings_token(token_info)?;
for header in &headers {
if let Value::Text(header_name) = &header {
if HeaderName::parse(header_name).is_none() {
if HeaderName::parse(header_name.as_ref()).is_none() {
return Err(self
.tokens
.unwrap_next()?
Expand Down
7 changes: 3 additions & 4 deletions src/compiler/grammar/tests/test_hasflag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,12 @@ impl<'x> CompilerState<'x> {
match variable {
Value::Text(var_name) => {
variable_list.push(
self.register_variable(var_name, is_local).map_err(
|error_type| CompileError {
self.register_variable(var_name.to_string(), is_local)
.map_err(|error_type| CompileError {
line_num,
line_pos,
error_type,
},
)?,
})?,
);
}
_ => {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/grammar/tests/test_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl<'x> CompilerState<'x> {
let headers = self.parse_strings_token(token_info)?;
for header in &headers {
if let Value::Text(header_name) = &header {
if HeaderName::parse(header_name).is_none() {
if HeaderName::parse(header_name.as_ref()).is_none() {
return Err(self
.tokens
.unwrap_next()?
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl StringConstant {
impl From<StringConstant> for Value {
fn from(value: StringConstant) -> Self {
match value {
StringConstant::String(s) => Value::Text(s),
StringConstant::String(s) => Value::Text(s.into()),
StringConstant::Number(n) => Value::Number(n),
}
}
Expand Down
68 changes: 37 additions & 31 deletions src/compiler/lexer/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ impl<'x> CompilerState<'x> {

Ok(match items.len() {
1 => items.pop().unwrap(),
0 => Value::Text(String::new()),
0 => Value::Text(String::new().into()),
_ => Value::List(items),
})
}
Expand Down Expand Up @@ -525,19 +525,19 @@ impl<'x> CompilerState<'x> {
.and_then(|v| (v, v.parse::<f64>().ok()?).into())
{
Some((v, n)) if n.to_string() == v => Value::Number(Number::Float(n)),
_ => Value::Text(buf.to_vec().into_string()),
_ => Value::Text(buf.to_vec().into_string().into()),
}
} else {
match std::str::from_utf8(buf)
.ok()
.and_then(|v| (v, v.parse::<i64>().ok()?).into())
{
Some((v, n)) if n.to_string() == v => Value::Number(Number::Integer(n)),
_ => Value::Text(buf.to_vec().into_string()),
_ => Value::Text(buf.to_vec().into_string().into()),
}
}
} else {
Value::Text(buf.to_vec().into_string())
Value::Text(buf.to_vec().into_string().into())
});
} else {
match self.tokenize_string(buf, false)? {
Expand Down Expand Up @@ -777,80 +777,86 @@ mod tests {
};

for (input, expected_result) in [
("$${hex:24 24}", Value::Text("$$$".to_string())),
("$${hex:40}", Value::Text("$@".to_string())),
("${hex: 40 }", Value::Text("@".to_string())),
("${HEX: 40}", Value::Text("@".to_string())),
("${hex:40", Value::Text("${hex:40".to_string())),
("${hex:400}", Value::Text("${hex:400}".to_string())),
("${hex:4${hex:30}}", Value::Text("${hex:40}".to_string())),
("${unicode:40}", Value::Text("@".to_string())),
("${ unicode:40}", Value::Text("${ unicode:40}".to_string())),
("${UNICODE:40}", Value::Text("@".to_string())),
("${UnICoDE:0000040}", Value::Text("@".to_string())),
("${Unicode:40}", Value::Text("@".to_string())),
("$${hex:24 24}", Value::Text("$$$".to_string().into())),
("$${hex:40}", Value::Text("$@".to_string().into())),
("${hex: 40 }", Value::Text("@".to_string().into())),
("${HEX: 40}", Value::Text("@".to_string().into())),
("${hex:40", Value::Text("${hex:40".to_string().into())),
("${hex:400}", Value::Text("${hex:400}".to_string().into())),
(
"${hex:4${hex:30}}",
Value::Text("${hex:40}".to_string().into()),
),
("${unicode:40}", Value::Text("@".to_string().into())),
(
"${ unicode:40}",
Value::Text("${ unicode:40}".to_string().into()),
),
("${UNICODE:40}", Value::Text("@".to_string().into())),
("${UnICoDE:0000040}", Value::Text("@".to_string().into())),
("${Unicode:40}", Value::Text("@".to_string().into())),
(
"${Unicode:40 40 ",
Value::Text("${Unicode:40 40 ".to_string()),
Value::Text("${Unicode:40 40 ".to_string().into()),
),
(
"${Unicode:Cool}",
Value::Text("${Unicode:Cool}".to_string()),
Value::Text("${Unicode:Cool}".to_string().into()),
),
("", Value::Text("".to_string())),
("", Value::Text("".to_string().into())),
(
"${global.full}",
Value::Variable(VariableType::Global("full".to_string())),
),
(
"${BAD${global.Company}",
Value::List(vec![
Value::Text("${BAD".to_string()),
Value::Text("${BAD".to_string().into()),
Value::Variable(VariableType::Global("company".to_string())),
]),
),
(
"${President, ${global.Company} Inc.}",
Value::List(vec![
Value::Text("${President, ".to_string()),
Value::Text("${President, ".to_string().into()),
Value::Variable(VariableType::Global("company".to_string())),
Value::Text(" Inc.}".to_string()),
Value::Text(" Inc.}".to_string().into()),
]),
),
(
"dear${hex:20 24 7b}global.Name}",
Value::List(vec![
Value::Text("dear ".to_string()),
Value::Text("dear ".to_string().into()),
Value::Variable(VariableType::Global("name".to_string())),
]),
),
(
"INBOX.lists.${2}",
Value::List(vec![
Value::Text("INBOX.lists.".to_string()),
Value::Text("INBOX.lists.".to_string().into()),
Value::Variable(VariableType::Match(2)),
]),
),
(
"Ein unerh${unicode:00F6}rt gro${unicode:00DF}er Test",
Value::Text("Ein unerhört großer Test".to_string()),
Value::Text("Ein unerhört großer Test".to_string().into()),
),
("&%${}!", Value::Text("&%${}!".to_string())),
("${doh!}", Value::Text("${doh!}".to_string())),
("&%${}!", Value::Text("&%${}!".to_string().into())),
("${doh!}", Value::Text("${doh!}".to_string().into())),
(
"${hex: 20 }${global.hi}${hex: 20 }",
Value::List(vec![
Value::Text(" ".to_string()),
Value::Text(" ".to_string().into()),
Value::Variable(VariableType::Global("hi".to_string())),
Value::Text(" ".to_string()),
Value::Text(" ".to_string().into()),
]),
),
(
"${hex:20 24 7b z}${global.hi}${unicode:}${unicode: }${hex:20}",
Value::List(vec![
Value::Text("${hex:20 24 7b z}".to_string()),
Value::Text("${hex:20 24 7b z}".to_string().into()),
Value::Variable(VariableType::Global("hi".to_string())),
Value::Text("${unicode:}${unicode: } ".to_string()),
Value::Text("${unicode:}${unicode: } ".to_string().into()),
]),
),
(
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* for more details.
*/

use std::{borrow::Cow, fmt::Display};
use std::{borrow::Cow, fmt::Display, sync::Arc};

use ahash::AHashMap;
use mail_parser::HeaderName;
Expand Down Expand Up @@ -96,7 +96,7 @@ impl Default for Compiler {

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) enum Value {
Text(String),
Text(Arc<String>),
Number(Number),
Variable(VariableType),
Regex(Regex),
Expand Down
Loading

0 comments on commit 0875287

Please sign in to comment.