Merge pull request #222 from Orange-OpenSource/refacto/add-predicate-value

Add PredicateValue public type
This commit is contained in:
Fabrice Reix 2021-06-29 18:41:22 +02:00 committed by GitHub
commit 31dc5a9a1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 650 additions and 1387 deletions

View File

@ -1,7 +1,7 @@
error: Parsing literal
error: Parsing predicate value
--> tests_error_parser/predicate_startwith_value.hurl:4:26
|
4 | header "toto" startsWith 1
| ^ expecting '"'
| ^ invalid predicate value
|

View File

@ -167,7 +167,7 @@ pub mod tests {
source_info: SourceInfo::init(1, 14, 1, 27),
value: PredicateFuncValue::CountEqual {
space0: whitespace.clone(),
value: 3,
value: PredicateValue::Integer(3),
},
},
};

View File

@ -44,6 +44,7 @@ mod log_deserialize;
mod log_serialize;
mod multipart;
mod predicate;
mod predicate_value;
mod query;
mod request;
mod response;

View File

@ -21,11 +21,12 @@ use regex::Regex;
use hurl_core::ast::*;
use super::core::*;
use super::core::{Error, RunnerError};
use super::expr::eval_expr;
use super::core::Error;
use super::template::eval_template;
use super::value::Value;
use crate::runner::core::PredicateResult;
use crate::runner::predicate_value::eval_predicate_value;
use crate::runner::RunnerError;
pub fn eval_predicate(
predicate: Predicate,
@ -131,186 +132,79 @@ fn eval_predicate_func(
}
}
impl Value {
pub fn expected(&self) -> String {
match self {
Value::Unit => "something".to_string(),
Value::Bool(value) => format!("bool <{}>", value),
Value::Integer(value) => format!("integer <{}>", value),
Value::String(value) => format!("string <{}>", value),
Value::List(value) => format!("list of size {}", value.len()),
Value::Object(values) => format!("list of size {}", values.len()),
Value::Nodeset(size) => format!("list of size {}", size),
Value::Bytes(values) => format!("list of size {}", values.len()),
Value::Null => "null".to_string(),
Value::Float(i, d) => format!("float <{}.{}>", i, d),
}
}
}
fn expected(
predicate_func: PredicateFunc,
variables: &HashMap<String, Value>,
) -> Result<String, Error> {
match predicate_func.value {
PredicateFuncValue::EqualInt {
value: expected, ..
PredicateFuncValue::Equal { value, .. } | PredicateFuncValue::NotEqual { value, .. } => {
let value = eval_predicate_value(value, variables)?;
Ok(value.expected())
}
| PredicateFuncValue::NotEqualInt {
value: expected, ..
} => {
let expected = expected.to_string();
Ok(format!("int <{}>", expected))
PredicateFuncValue::GreaterThan { value, .. } => {
let value = eval_predicate_value(value, variables)?;
Ok(format!("greater than <{}>", value.expected()))
}
PredicateFuncValue::EqualFloat {
value:
Float {
int: expected_int,
decimal: expected_dec,
..
},
..
PredicateFuncValue::GreaterThanOrEqual { value, .. } => {
let value = eval_predicate_value(value, variables)?;
Ok(format!("greater than or equals to <{}>", value.expected()))
}
| PredicateFuncValue::NotEqualFloat {
value:
Float {
int: expected_int,
decimal: expected_dec,
..
},
..
} => Ok(format!("float <{}.{}>", expected_int, expected_dec)),
PredicateFuncValue::EqualNull { .. } | PredicateFuncValue::NotEqualNull { .. } => {
Ok("null".to_string())
PredicateFuncValue::LessThan { value, .. } => {
let value = eval_predicate_value(value, variables)?;
Ok(format!("less than <{}>", value.expected()))
}
PredicateFuncValue::EqualBool {
value: expected, ..
PredicateFuncValue::LessThanOrEqual { value, .. } => {
let value = eval_predicate_value(value, variables)?;
Ok(format!("less than or equals to <{}>", value.expected()))
}
| PredicateFuncValue::NotEqualBool {
value: expected, ..
} => {
let expected = expected.to_string();
Ok(format!("bool <{}>", expected))
}
PredicateFuncValue::EqualString {
value: expected, ..
}
| PredicateFuncValue::NotEqualString {
value: expected, ..
} => {
let expected = eval_template(expected, variables)?;
Ok(format!("string <{}>", expected))
}
PredicateFuncValue::EqualHex {
value: expected, ..
}
| PredicateFuncValue::NotEqualHex {
value: expected, ..
} => Ok(format!("bytearray <{}>", expected.to_string())),
PredicateFuncValue::EqualExpression {
value: expected, ..
}
| PredicateFuncValue::NotEqualExpression {
value: expected, ..
} => {
let expected = eval_expr(expected, variables)?;
todo!(">> {:?}", expected)
}
PredicateFuncValue::GreaterThanInt {
value: expected, ..
} => {
let expected = expected.to_string();
Ok(format!("greater than <{}>", expected))
}
PredicateFuncValue::GreaterThanFloat {
value:
Float {
int: expected_int,
decimal: expected_dec,
..
},
..
} => Ok(format!("greater than <{}.{}>", expected_int, expected_dec)),
PredicateFuncValue::GreaterThanOrEqualInt {
value: expected, ..
} => {
let expected = expected.to_string();
Ok(format!("greater than or equals to <{}>", expected))
}
PredicateFuncValue::GreaterThanOrEqualFloat {
value:
Float {
int: expected_int,
decimal: expected_dec,
..
},
..
} => Ok(format!(
"greater than or equals to <{}.{}>",
expected_int, expected_dec
)),
PredicateFuncValue::LessThanInt {
value: expected, ..
} => {
let expected = expected.to_string();
Ok(format!("less than <{}>", expected))
}
PredicateFuncValue::LessThanFloat {
value:
Float {
int: expected_int,
decimal: expected_dec,
..
},
..
} => Ok(format!("less than <{}.{}>", expected_int, expected_dec)),
PredicateFuncValue::LessThanOrEqualInt {
value: expected, ..
} => {
let expected = expected.to_string();
Ok(format!("less than or equals to <{}>", expected))
}
PredicateFuncValue::LessThanOrEqualFloat {
value:
Float {
int: expected_int,
decimal: expected_dec,
..
},
..
} => Ok(format!(
"less than or equals to <{}.{}>",
expected_int, expected_dec
)),
PredicateFuncValue::CountEqual {
value: expected, ..
} => {
let expected = expected.to_string();
Ok(format!("count equals to <{}>", expected))
let expected = if let PredicateValue::Integer(expected) = expected {
expected
} else {
panic!();
};
Ok(format!("count equals to <{}>", expected.to_string()))
}
PredicateFuncValue::StartWith {
value: expected, ..
} => {
let expected = eval_template(expected, variables)?;
let expected = eval_predicate_value_template(expected, variables)?;
Ok(format!("starts with string <{}>", expected))
}
PredicateFuncValue::Contain {
value: expected, ..
} => {
let expected = eval_template(expected, variables)?;
let expected = eval_predicate_value_template(expected, variables)?;
Ok(format!("contains string <{}>", expected))
}
PredicateFuncValue::IncludeString {
value: expected, ..
} => {
let expected = eval_template(expected, variables)?;
Ok(format!("includes string <{}>", expected))
PredicateFuncValue::Include { value, .. } => {
let value = eval_predicate_value(value, variables)?;
Ok(format!("include {}", value.expected()))
}
PredicateFuncValue::IncludeInt {
value: expected, ..
} => Ok(format!("includes int <{}>", expected)),
PredicateFuncValue::IncludeFloat {
value: expected, ..
} => Ok(format!("includes float <{}>", expected)),
PredicateFuncValue::IncludeNull { .. } => Ok("includes null".to_string()),
PredicateFuncValue::IncludeBool {
value: expected, ..
} => Ok(format!("includes bool <{}>", expected)),
PredicateFuncValue::IncludeExpression {
value: _expected, ..
} => todo!(),
PredicateFuncValue::Match {
value: expected, ..
} => {
let expected = eval_template(expected, variables)?;
let expected = eval_predicate_value_template(expected, variables)?;
Ok(format!("matches regex <{}>", expected))
}
PredicateFuncValue::IsInteger {} => Ok("integer".to_string()),
@ -322,132 +216,80 @@ fn expected(
}
}
pub fn eval_predicate_value_template(
predicate_value: PredicateValue,
variables: &HashMap<String, Value>,
) -> Result<String, Error> {
let template = if let PredicateValue::String(template) = predicate_value {
template
} else {
panic!()
};
eval_template(template, variables)
}
fn eval_something(
predicate_func: PredicateFunc,
variables: &HashMap<String, Value>,
value: Value,
) -> Result<AssertResult, Error> {
match predicate_func.value {
PredicateFuncValue::EqualInt {
value: expected, ..
} => Ok(assert_values_equal(value, Value::Integer(expected))),
PredicateFuncValue::EqualNull { .. } => Ok(assert_values_equal(value, Value::Null)),
PredicateFuncValue::EqualBool {
value: expected, ..
} => Ok(assert_values_equal(value, Value::Bool(expected))),
PredicateFuncValue::EqualFloat {
value: Float { int, decimal, .. },
..
} => Ok(assert_values_equal(value, Value::Float(int, decimal))),
PredicateFuncValue::EqualString {
PredicateFuncValue::Equal {
value: expected, ..
} => {
let expected = eval_template(expected, variables)?;
Ok(assert_values_equal(value, Value::String(expected)))
}
PredicateFuncValue::EqualHex {
value: Hex {
value: expected, ..
},
..
} => Ok(assert_values_equal(value, Value::Bytes(expected))),
PredicateFuncValue::EqualExpression {
value: expected, ..
} => {
let expected = eval_expr(expected, variables)?;
let expected = eval_predicate_value(expected, variables)?;
Ok(assert_values_equal(value, expected))
}
PredicateFuncValue::NotEqualInt {
value: expected, ..
} => Ok(assert_values_not_equal(value, Value::Integer(expected))),
PredicateFuncValue::NotEqualNull { .. } => Ok(assert_values_equal(value, Value::Null)),
PredicateFuncValue::NotEqualBool {
value: expected, ..
} => Ok(assert_values_not_equal(value, Value::Bool(expected))),
PredicateFuncValue::NotEqualFloat {
value: Float { int, decimal, .. },
..
} => Ok(assert_values_not_equal(value, Value::Float(int, decimal))),
PredicateFuncValue::NotEqualString {
PredicateFuncValue::NotEqual {
value: expected, ..
} => {
let expected = eval_template(expected, variables)?;
Ok(assert_values_not_equal(value, Value::String(expected)))
}
PredicateFuncValue::NotEqualHex {
value: Hex {
value: expected, ..
},
..
} => Ok(assert_values_not_equal(value, Value::Bytes(expected))),
PredicateFuncValue::NotEqualExpression {
value: expected, ..
} => {
let expected = eval_expr(expected, variables)?;
let expected = eval_predicate_value(expected, variables)?;
Ok(assert_values_not_equal(value, expected))
}
PredicateFuncValue::GreaterThanInt {
PredicateFuncValue::GreaterThan {
value: expected, ..
} => Ok(assert_values_greater(value, Value::Integer(expected))),
PredicateFuncValue::GreaterThanFloat {
value: Float { int, decimal, .. },
..
} => Ok(assert_values_greater(value, Value::Float(int, decimal))),
PredicateFuncValue::GreaterThanOrEqualInt {
} => {
let expected = eval_predicate_value(expected, variables)?;
Ok(assert_values_greater(value, expected))
}
PredicateFuncValue::GreaterThanOrEqual {
value: expected, ..
} => Ok(assert_values_greater_or_equal(
value,
Value::Integer(expected),
)),
PredicateFuncValue::GreaterThanOrEqualFloat {
value: Float { int, decimal, .. },
..
} => Ok(assert_values_greater_or_equal(
value,
Value::Float(int, decimal),
)),
PredicateFuncValue::LessThanInt {
} => {
let expected = eval_predicate_value(expected, variables)?;
Ok(assert_values_greater_or_equal(value, expected))
}
PredicateFuncValue::LessThan {
value: expected, ..
} => Ok(assert_values_less(value, Value::Integer(expected))),
PredicateFuncValue::LessThanFloat {
value: Float { int, decimal, .. },
..
} => Ok(assert_values_less(value, Value::Float(int, decimal))),
PredicateFuncValue::LessThanOrEqualInt {
} => {
let expected = eval_predicate_value(expected, variables)?;
Ok(assert_values_less(value, expected))
}
PredicateFuncValue::LessThanOrEqual {
value: expected, ..
} => Ok(assert_values_less_or_equal(value, Value::Integer(expected))),
PredicateFuncValue::LessThanOrEqualFloat {
value: Float { int, decimal, .. },
..
} => Ok(assert_values_less_or_equal(
value,
Value::Float(int, decimal),
)),
} => {
let expected = eval_predicate_value(expected, variables)?;
Ok(assert_values_less_or_equal(value, expected))
}
// countEquals
PredicateFuncValue::CountEqual {
value: expected_value,
value: PredicateValue::Integer(expected_value),
..
} => match value {
Value::List(values) => Ok(AssertResult {
success: values.len() as u64 == expected_value,
success: values.len() as i64 == expected_value,
actual: values.len().to_string(),
expected: expected_value.to_string(),
type_mismatch: false,
}),
Value::Nodeset(n) => Ok(AssertResult {
success: n as u64 == expected_value,
success: n as i64 == expected_value,
actual: n.to_string(),
expected: expected_value.to_string(),
type_mismatch: false,
}),
Value::Bytes(data) => Ok(AssertResult {
success: data.len() as u64 == expected_value,
success: data.len() as i64 == expected_value,
actual: data.len().to_string(),
expected: expected_value.to_string(),
type_mismatch: false,
@ -464,7 +306,12 @@ fn eval_something(
PredicateFuncValue::StartWith {
value: expected, ..
} => {
let expected = eval_template(expected, variables)?;
let template = if let PredicateValue::String(template) = expected {
template
} else {
panic!("expect a string predicate value")
};
let expected = eval_template(template, variables)?;
match value.clone() {
Value::String(actual) => Ok(AssertResult {
success: actual.as_str().starts_with(expected.as_str()),
@ -485,7 +332,12 @@ fn eval_something(
PredicateFuncValue::Contain {
value: expected, ..
} => {
let expected = eval_template(expected, variables)?;
let template = if let PredicateValue::String(template) = expected {
template
} else {
panic!("expect a string predicate value")
};
let expected = eval_template(template, variables)?;
match value.clone() {
Value::String(actual) => Ok(AssertResult {
success: actual.as_str().contains(expected.as_str()),
@ -502,46 +354,22 @@ fn eval_something(
}
}
// includes String
PredicateFuncValue::IncludeString {
PredicateFuncValue::Include {
value: expected, ..
} => {
let expected = eval_template(expected, variables)?;
Ok(assert_include(value, Value::String(expected)))
}
// includes int
PredicateFuncValue::IncludeInt {
value: expected, ..
} => Ok(assert_include(value, Value::Integer(expected))),
// includes float
PredicateFuncValue::IncludeFloat {
value: Float { int, decimal, .. },
..
} => Ok(assert_include(value, Value::Float(int, decimal))),
// includes bool
PredicateFuncValue::IncludeBool {
value: expected, ..
} => Ok(assert_include(value, Value::Bool(expected))),
// includes null
PredicateFuncValue::IncludeNull { .. } => Ok(assert_include(value, Value::Null)),
// includes expression
PredicateFuncValue::IncludeExpression {
value: expected, ..
} => {
let expected = eval_expr(expected, variables)?;
let expected = eval_predicate_value(expected, variables)?;
Ok(assert_include(value, expected))
}
// match string
PredicateFuncValue::Match {
value: expected, ..
} => {
let expected = eval_template(expected, variables)?;
let template = if let PredicateValue::String(template) = expected {
template
} else {
panic!("expect a string predicate value")
};
let expected = eval_template(template, variables)?;
let regex = match Regex::new(expected.as_str()) {
Ok(re) => re,
Err(_) => {
@ -618,6 +446,7 @@ fn eval_something(
type_mismatch: false,
}),
},
_ => panic!(),
}
}
@ -951,9 +780,9 @@ mod tests {
not: true,
space0: whitespace.clone(),
predicate_func: PredicateFunc {
value: PredicateFuncValue::EqualInt {
value: PredicateFuncValue::Equal {
space0: whitespace,
value: 10,
value: PredicateValue::Integer(10),
operator: false,
},
source_info: SourceInfo::init(1, 11, 1, 12),
@ -987,9 +816,9 @@ mod tests {
};
let assert_result = eval_predicate_func(
PredicateFunc {
value: PredicateFuncValue::EqualInt {
value: PredicateFuncValue::Equal {
space0: whitespace,
value: 10,
value: PredicateValue::Integer(10),
operator: false,
},
source_info: SourceInfo::init(0, 0, 0, 0),
@ -1013,9 +842,9 @@ mod tests {
};
let assert_result = eval_predicate_func(
PredicateFunc {
value: PredicateFuncValue::EqualInt {
value: PredicateFuncValue::Equal {
space0: whitespace,
value: 10,
value: PredicateValue::Integer(10),
operator: false,
},
source_info: SourceInfo::init(0, 0, 0, 0),
@ -1040,9 +869,9 @@ mod tests {
let assert_result = eval_something(
PredicateFunc {
value: PredicateFuncValue::EqualInt {
value: PredicateFuncValue::Equal {
space0: whitespace.clone(),
value: 10,
value: PredicateValue::Integer(10),
operator: false,
},
source_info: SourceInfo::init(0, 0, 0, 0),
@ -1058,9 +887,9 @@ mod tests {
let assert_result = eval_something(
PredicateFunc {
value: PredicateFuncValue::EqualBool {
value: PredicateFuncValue::Equal {
space0: whitespace.clone(),
value: true,
value: PredicateValue::Bool(true),
operator: false,
},
source_info: SourceInfo::init(0, 0, 0, 0),
@ -1076,13 +905,13 @@ mod tests {
let assert_result = eval_something(
PredicateFunc {
value: PredicateFuncValue::EqualFloat {
value: PredicateFuncValue::Equal {
space0: whitespace,
value: Float {
value: PredicateValue::Float(Float {
int: 1,
decimal: 200_000_000_000_000_000,
decimal_digits: 0,
},
}),
operator: false,
},
source_info: SourceInfo::init(0, 0, 0, 0),
@ -1129,9 +958,9 @@ mod tests {
};
let assert_result = eval_something(
PredicateFunc {
value: PredicateFuncValue::EqualInt {
value: PredicateFuncValue::Equal {
space0: whitespace.clone(),
value: 1,
value: PredicateValue::Integer(1),
operator: false,
},
source_info: SourceInfo::init(0, 0, 0, 0),
@ -1147,9 +976,9 @@ mod tests {
let assert_result = eval_something(
PredicateFunc {
value: PredicateFuncValue::EqualBool {
value: PredicateFuncValue::Equal {
space0: whitespace.clone(),
value: false,
value: PredicateValue::Bool(false),
operator: false,
},
source_info: SourceInfo::init(0, 0, 0, 0),
@ -1165,13 +994,13 @@ mod tests {
let assert_result = eval_something(
PredicateFunc {
value: PredicateFuncValue::EqualFloat {
value: PredicateFuncValue::Equal {
space0: whitespace.clone(),
value: Float {
value: PredicateValue::Float(Float {
int: 1,
decimal: 1,
decimal_digits: 1,
},
}),
operator: false,
},
source_info: SourceInfo::init(0, 0, 0, 0),
@ -1188,9 +1017,9 @@ mod tests {
// a float can be equals to an int (but the reverse)
let assert_result = eval_something(
PredicateFunc {
value: PredicateFuncValue::EqualInt {
value: PredicateFuncValue::Equal {
space0: whitespace,
value: 1,
value: PredicateValue::Integer(1),
operator: false,
},
source_info: SourceInfo::init(0, 0, 0, 0),
@ -1214,9 +1043,9 @@ mod tests {
};
let assert_result = eval_something(
PredicateFunc {
value: PredicateFuncValue::NotEqualInt {
value: PredicateFuncValue::NotEqual {
space0: whitespace.clone(),
value: 1,
value: PredicateValue::Integer(1),
operator: false,
},
source_info: SourceInfo::init(0, 0, 0, 0),
@ -1260,9 +1089,9 @@ mod tests {
let error = eval_something(
PredicateFunc {
value: PredicateFuncValue::EqualString {
value: PredicateFuncValue::Equal {
space0: whitespace.clone(),
value: template.clone(),
value: PredicateValue::String(template.clone()),
operator: false,
},
source_info: SourceInfo::init(1, 1, 1, 21),
@ -1286,9 +1115,9 @@ mod tests {
);
let assert_result = eval_something(
PredicateFunc {
value: PredicateFuncValue::EqualString {
value: PredicateFuncValue::Equal {
space0: whitespace,
value: template,
value: PredicateValue::String(template),
operator: false,
},
source_info: SourceInfo::init(0, 0, 0, 0),
@ -1441,7 +1270,7 @@ mod tests {
PredicateFunc {
value: PredicateFuncValue::CountEqual {
space0: whitespace.clone(),
value: 10,
value: PredicateValue::Integer(10),
},
source_info: SourceInfo::init(0, 0, 0, 0),
},
@ -1458,7 +1287,7 @@ mod tests {
PredicateFunc {
value: PredicateFuncValue::CountEqual {
space0: whitespace.clone(),
value: 1,
value: PredicateValue::Integer(1),
},
source_info: SourceInfo::init(0, 0, 0, 0),
},
@ -1476,7 +1305,7 @@ mod tests {
source_info: SourceInfo::init(0, 0, 0, 0),
value: PredicateFuncValue::CountEqual {
space0: whitespace,
value: 1,
value: PredicateValue::Integer(1),
},
},
&variables,
@ -1500,7 +1329,7 @@ mod tests {
PredicateFunc {
value: PredicateFuncValue::CountEqual {
space0: whitespace.clone(),
value: 1,
value: PredicateValue::Integer(1),
},
source_info: SourceInfo::init(0, 0, 0, 0),
},
@ -1517,7 +1346,7 @@ mod tests {
PredicateFunc {
value: PredicateFuncValue::CountEqual {
space0: whitespace,
value: 1,
value: PredicateValue::Integer(1),
},
source_info: SourceInfo::init(0, 0, 0, 0),
},
@ -1571,9 +1400,10 @@ mod tests {
space0: whitespace(),
predicate_func: PredicateFunc {
source_info: SourceInfo::init(0, 0, 0, 0),
value: PredicateFuncValue::EqualNull {
value: PredicateFuncValue::Equal {
space0: whitespace(),
operator: false,
value: PredicateValue::Null {},
},
},
};
@ -1590,14 +1420,14 @@ mod tests {
source_info: SourceInfo::init(0, 0, 0, 0),
value: PredicateFuncValue::StartWith {
space0: whitespace(),
value: Template {
value: PredicateValue::String(Template {
quotes: false,
elements: vec![TemplateElement::String {
value: "toto".to_string(),
encoded: "toto".to_string(),
}],
source_info: SourceInfo::init(0, 0, 0, 0),
},
}),
},
},
};
@ -1621,8 +1451,9 @@ mod tests {
space0: whitespace(),
predicate_func: PredicateFunc {
source_info: SourceInfo::init(0, 0, 0, 0),
value: PredicateFuncValue::EqualNull {
value: PredicateFuncValue::Equal {
space0: whitespace(),
value: PredicateValue::Null {},
operator: false,
},
},
@ -1644,9 +1475,10 @@ mod tests {
space0: whitespace(),
predicate_func: PredicateFunc {
source_info: SourceInfo::init(0, 0, 0, 0),
value: PredicateFuncValue::EqualNull {
value: PredicateFuncValue::Equal {
space0: whitespace(),
operator: false,
value: PredicateValue::Null {},
},
},
};

View File

@ -0,0 +1,46 @@
/*
* hurl (https://hurl.dev)
* Copyright (C) 2020 Orange
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
use std::collections::HashMap;
use hurl_core::ast::*;
use super::core::Error;
use super::expr::eval_expr;
use super::template::eval_template;
use super::value::Value;
pub fn eval_predicate_value(
predicate_value: PredicateValue,
variables: &HashMap<String, Value>,
) -> Result<Value, Error> {
match predicate_value {
PredicateValue::String(template) => {
let s = eval_template(template, variables)?;
Ok(Value::String(s))
}
PredicateValue::Integer(value) => Ok(Value::Integer(value)),
PredicateValue::Float(value) => Ok(Value::Float(value.int, value.decimal)),
PredicateValue::Bool(value) => Ok(Value::Bool(value)),
PredicateValue::Null {} => Ok(Value::Null {}),
PredicateValue::Hex(value) => Ok(Value::Bytes(value.value)),
PredicateValue::Expression(expr) => {
let value = eval_expr(expr, variables)?;
Ok(value)
}
}
}

View File

@ -397,155 +397,70 @@ pub struct PredicateFunc {
pub value: PredicateFuncValue,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[allow(clippy::large_enum_variant)]
pub enum PredicateValue {
String(Template),
Integer(i64),
Float(Float),
Bool(bool),
Null {},
Hex(Hex),
Expression(Expr),
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[allow(clippy::large_enum_variant)]
pub enum PredicateFuncValue {
EqualString {
Equal {
space0: Whitespace,
value: Template,
value: PredicateValue,
operator: bool,
},
EqualInt {
NotEqual {
space0: Whitespace,
value: i64,
value: PredicateValue,
operator: bool,
},
EqualFloat {
GreaterThan {
space0: Whitespace,
value: Float,
value: PredicateValue,
operator: bool,
},
EqualBool {
GreaterThanOrEqual {
space0: Whitespace,
value: bool,
value: PredicateValue,
operator: bool,
},
EqualNull {
LessThan {
space0: Whitespace,
value: PredicateValue,
operator: bool,
},
EqualHex {
LessThanOrEqual {
space0: Whitespace,
value: Hex,
operator: bool,
},
EqualExpression {
space0: Whitespace,
value: Expr,
operator: bool,
},
NotEqualString {
space0: Whitespace,
value: Template,
operator: bool,
},
NotEqualInt {
space0: Whitespace,
value: i64,
operator: bool,
},
NotEqualFloat {
space0: Whitespace,
value: Float,
operator: bool,
},
NotEqualBool {
space0: Whitespace,
value: bool,
operator: bool,
},
NotEqualNull {
space0: Whitespace,
operator: bool,
},
NotEqualHex {
space0: Whitespace,
value: Hex,
operator: bool,
},
NotEqualExpression {
space0: Whitespace,
value: Expr,
operator: bool,
},
GreaterThanInt {
space0: Whitespace,
value: i64,
operator: bool,
},
GreaterThanFloat {
space0: Whitespace,
value: Float,
operator: bool,
},
GreaterThanOrEqualInt {
space0: Whitespace,
value: i64,
operator: bool,
},
GreaterThanOrEqualFloat {
space0: Whitespace,
value: Float,
operator: bool,
},
LessThanInt {
space0: Whitespace,
value: i64,
operator: bool,
},
LessThanFloat {
space0: Whitespace,
value: Float,
operator: bool,
},
LessThanOrEqualInt {
space0: Whitespace,
value: i64,
operator: bool,
},
LessThanOrEqualFloat {
space0: Whitespace,
value: Float,
value: PredicateValue,
operator: bool,
},
CountEqual {
space0: Whitespace,
value: u64,
value: PredicateValue,
},
StartWith {
space0: Whitespace,
value: Template,
value: PredicateValue,
},
Contain {
space0: Whitespace,
value: Template,
value: PredicateValue,
},
IncludeString {
Include {
space0: Whitespace,
value: Template,
},
IncludeInt {
space0: Whitespace,
value: i64,
},
IncludeFloat {
space0: Whitespace,
value: Float,
},
IncludeBool {
space0: Whitespace,
value: bool,
},
IncludeNull {
space0: Whitespace,
},
IncludeExpression {
space0: Whitespace,
value: Expr,
value: PredicateValue,
},
Match {
space0: Whitespace,
value: Template,
value: PredicateValue,
},
IsInteger {},
IsFloat {},

View File

@ -144,58 +144,42 @@ impl fmt::Display for Hex {
impl PredicateFuncValue {
pub fn name(&self) -> String {
match self {
PredicateFuncValue::EqualString { operator, .. }
| PredicateFuncValue::EqualInt { operator, .. }
| PredicateFuncValue::EqualFloat { operator, .. }
| PredicateFuncValue::EqualBool { operator, .. }
| PredicateFuncValue::EqualNull { operator, .. }
| PredicateFuncValue::EqualHex { operator, .. }
| PredicateFuncValue::EqualExpression { operator, .. } => {
PredicateFuncValue::Equal { operator, .. } => {
if *operator {
"==".to_string()
} else {
"equals".to_string()
}
}
PredicateFuncValue::NotEqualString { operator, .. }
| PredicateFuncValue::NotEqualInt { operator, .. }
| PredicateFuncValue::NotEqualFloat { operator, .. }
| PredicateFuncValue::NotEqualBool { operator, .. }
| PredicateFuncValue::NotEqualNull { operator, .. }
| PredicateFuncValue::NotEqualHex { operator, .. }
| PredicateFuncValue::NotEqualExpression { operator, .. } => {
PredicateFuncValue::NotEqual { operator, .. } => {
if *operator {
"!=".to_string()
} else {
"notEquals".to_string()
}
}
PredicateFuncValue::GreaterThanInt { operator, .. }
| PredicateFuncValue::GreaterThanFloat { operator, .. } => {
PredicateFuncValue::GreaterThan { operator, .. } => {
if *operator {
">".to_string()
} else {
"greaterThan".to_string()
}
}
PredicateFuncValue::GreaterThanOrEqualInt { operator, .. }
| PredicateFuncValue::GreaterThanOrEqualFloat { operator, .. } => {
PredicateFuncValue::GreaterThanOrEqual { operator, .. } => {
if *operator {
">=".to_string()
} else {
"greaterThanOrEquals".to_string()
}
}
PredicateFuncValue::LessThanInt { operator, .. }
| PredicateFuncValue::LessThanFloat { operator, .. } => {
PredicateFuncValue::LessThan { operator, .. } => {
if *operator {
"<".to_string()
} else {
"lessThan".to_string()
}
}
PredicateFuncValue::LessThanOrEqualInt { operator, .. }
| PredicateFuncValue::LessThanOrEqualFloat { operator, .. } => {
PredicateFuncValue::LessThanOrEqual { operator, .. } => {
if *operator {
"<=".to_string()
} else {
@ -205,12 +189,7 @@ impl PredicateFuncValue {
PredicateFuncValue::CountEqual { .. } => "countEquals".to_string(),
PredicateFuncValue::StartWith { .. } => "startsWith".to_string(),
PredicateFuncValue::Contain { .. } => "contains".to_string(),
PredicateFuncValue::IncludeString { .. }
| PredicateFuncValue::IncludeInt { .. }
| PredicateFuncValue::IncludeFloat { .. }
| PredicateFuncValue::IncludeBool { .. }
| PredicateFuncValue::IncludeNull { .. }
| PredicateFuncValue::IncludeExpression { .. } => "includes".to_string(),
PredicateFuncValue::Include { .. } => "includes".to_string(),
PredicateFuncValue::Match { .. } => "matches".to_string(),
PredicateFuncValue::IsInteger { .. } => "isInteger".to_string(),
PredicateFuncValue::IsFloat { .. } => "isFloat".to_string(),

View File

@ -472,135 +472,30 @@ impl Htmlable for PredicateFuncValue {
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str(format!("<span class=\"number\">{}</span>", value).as_str());
buffer.push_str(value.to_html().as_str());
}
PredicateFuncValue::EqualString { space0, value, .. } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str(
format!("<span class=\"string\">\"{}\"</span>", value.to_html()).as_str(),
);
}
PredicateFuncValue::EqualInt { space0, value, .. } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str(format!("<span class=\"number\">{}</span>", value).as_str());
}
PredicateFuncValue::EqualFloat { space0, value, .. } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str(
format!("<span class=\"number\">{}</span>", value.to_string()).as_str(),
);
}
PredicateFuncValue::EqualNull { space0, .. } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str("<span class=\"null\">null</span>");
}
PredicateFuncValue::EqualBool { space0, value, .. } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str(format!("<span class=\"boolean\">{}</span>", value).as_str());
}
PredicateFuncValue::EqualHex { space0, value, .. } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer
.push_str(format!("<span class=\"hex\">{}</span>", value.to_string()).as_str());
}
PredicateFuncValue::EqualExpression { space0, value, .. } => {
PredicateFuncValue::Equal { space0, value, .. } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str(value.to_html().as_str());
}
PredicateFuncValue::NotEqualString { space0, value, .. } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str(
format!("<span class=\"string\">\"{}\"</span>", value.to_html()).as_str(),
);
}
PredicateFuncValue::NotEqualInt { space0, value, .. } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str(format!("<span class=\"number\">{}</span>", value).as_str());
}
PredicateFuncValue::NotEqualFloat { space0, value, .. } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str(
format!("<span class=\"number\">{}</span>", value.to_string()).as_str(),
);
}
PredicateFuncValue::NotEqualNull { space0, .. } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str("<span class=\"null\">null</span>");
}
PredicateFuncValue::NotEqualBool { space0, value, .. } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str(format!("<span class=\"boolean\">{}</span>", value).as_str());
}
PredicateFuncValue::NotEqualHex { space0, value, .. } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer
.push_str(format!("<span class=\"hex\">{}</span>", value.to_string()).as_str());
}
PredicateFuncValue::NotEqualExpression { space0, value, .. } => {
PredicateFuncValue::NotEqual { space0, value, .. } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str(value.to_html().as_str());
}
PredicateFuncValue::GreaterThanInt { space0, value, .. } => {
PredicateFuncValue::GreaterThan { space0, value, .. } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str(
format!("<span class=\"number\">{}</span>", value.to_string()).as_str(),
);
buffer.push_str(value.to_html().as_str());
}
PredicateFuncValue::GreaterThanFloat { space0, value, .. } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str(
format!("<span class=\"number\">{}</span>", value.to_string()).as_str(),
);
}
PredicateFuncValue::GreaterThanOrEqualInt {
PredicateFuncValue::GreaterThanOrEqual {
space0,
value,
operator,
@ -617,130 +512,50 @@ impl Htmlable for PredicateFuncValue {
.as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str(
format!("<span class=\"number\">{}</span>", value.to_string()).as_str(),
);
buffer.push_str(value.to_html().as_str());
}
PredicateFuncValue::GreaterThanOrEqualFloat { space0, value, .. } => {
PredicateFuncValue::LessThan { space0, value, .. } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str(
format!("<span class=\"number\">{}</span>", value.to_string()).as_str(),
);
buffer.push_str(value.to_html().as_str());
}
PredicateFuncValue::LessThanInt { space0, value, .. } => {
PredicateFuncValue::LessThanOrEqual { space0, value, .. } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str(
format!("<span class=\"number\">{}</span>", value.to_string()).as_str(),
);
buffer.push_str(value.to_html().as_str());
}
PredicateFuncValue::LessThanFloat { space0, value, .. } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str(
format!("<span class=\"number\">{}</span>", value.to_string()).as_str(),
);
}
PredicateFuncValue::LessThanOrEqualInt { space0, value, .. } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str(
format!("<span class=\"number\">{}</span>", value.to_string()).as_str(),
);
}
PredicateFuncValue::LessThanOrEqualFloat { space0, value, .. } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str(
format!("<span class=\"number\">{}</span>", value.to_string()).as_str(),
);
}
PredicateFuncValue::StartWith { space0, value } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str(
format!("<span class=\"string\">\"{}\"</span>", value.to_html()).as_str(),
);
buffer.push_str(value.to_html().as_str());
}
PredicateFuncValue::Contain { space0, value } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str(
format!("<span class=\"string\">\"{}\"</span>", value.to_html()).as_str(),
);
}
PredicateFuncValue::IncludeString { space0, value } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str(
format!("<span class=\"string\">\"{}\"</span>", value.to_html()).as_str(),
);
}
PredicateFuncValue::IncludeInt { space0, value } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str(format!("<span class=\"number\">{}</span>", value).as_str());
}
PredicateFuncValue::IncludeNull { space0 } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str("<span class=\"null\">null</span>");
}
PredicateFuncValue::IncludeBool { space0, value } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str(format!("<span class=\"boolean\">{}</span>", value).as_str());
}
PredicateFuncValue::IncludeFloat { space0, value } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str(
format!("<span class=\"number\">{}</span>", value.to_string()).as_str(),
);
}
PredicateFuncValue::IncludeExpression { space0, value } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push('"');
buffer.push_str(value.to_html().as_str());
buffer.push('"');
}
PredicateFuncValue::Include { space0, value } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str(value.to_html().as_str());
}
PredicateFuncValue::Match { space0, value } => {
buffer.push_str(
format!("<span class=\"predicate-type\">{}</span>", self.name()).as_str(),
);
buffer.push_str(space0.to_html().as_str());
buffer.push_str(
format!("<span class=\"string\">\"{}\"</span>", value.to_html()).as_str(),
);
buffer.push_str(value.to_html().as_str());
}
PredicateFuncValue::IsInteger {} => {
buffer.push_str(
@ -777,6 +592,26 @@ impl Htmlable for PredicateFuncValue {
}
}
impl Htmlable for PredicateValue {
fn to_html(&self) -> String {
match self {
PredicateValue::String(value) => {
format!("<span class=\"string\">\"{}\"</span>", value.to_html())
}
PredicateValue::Integer(value) => format!("<span class=\"number\">{}</span>", value),
PredicateValue::Float(value) => {
format!("<span class=\"number\">{}</span>", value.to_string())
}
PredicateValue::Bool(value) => format!("<span class=\"boolean\">{}</span>", value),
PredicateValue::Hex(value) => {
format!("<span class=\"hex\">{}</span>", value.to_string())
}
PredicateValue::Expression(value) => value.to_html(),
PredicateValue::Null {} => "<span class=\"null\">null</span>".to_string(),
}
}
}
impl Htmlable for Body {
fn to_html(&self) -> String {
let mut buffer = String::from("");

View File

@ -61,3 +61,20 @@ pub enum ParseError {
OddNumberOfHexDigits,
UrlIllegalCharacter(char),
}
impl Error {
pub fn recoverable(&self) -> Error {
Error {
pos: self.pos.clone(),
recoverable: true,
inner: self.inner.clone(),
}
}
pub fn non_recoverable(&self) -> Error {
Error {
pos: self.pos.clone(),
recoverable: false,
inner: self.inner.clone(),
}
}
}

View File

@ -42,6 +42,7 @@ mod expr;
mod json;
mod parsers;
mod predicate;
mod predicate_value;
mod primitives;
mod query;
mod reader;

View File

@ -20,10 +20,9 @@ use crate::ast::*;
use super::combinators::*;
use super::error::*;
use super::expr;
use super::predicate_value::predicate_value;
use super::primitives::*;
use super::reader::Reader;
use super::string::*;
use super::ParseResult;
pub fn predicate(reader: &mut Reader) -> ParseResult<'static, Predicate> {
@ -104,6 +103,15 @@ fn predicate_func_value(reader: &mut Reader) -> ParseResult<'static, PredicateFu
}
}
impl PredicateValue {
pub fn is_number(&self) -> bool {
matches!(self, PredicateValue::Integer(_) | PredicateValue::Float(_))
}
pub fn is_string(&self) -> bool {
matches!(self, PredicateValue::String(_))
}
}
fn equal_predicate(reader: &mut Reader) -> ParseResult<'static, PredicateFuncValue> {
let operator = try_literals("equals", "==", reader)? == "==";
let space0 = if operator {
@ -111,51 +119,12 @@ fn equal_predicate(reader: &mut Reader) -> ParseResult<'static, PredicateFuncVal
} else {
one_or_more_spaces(reader)?
};
let start = reader.state.clone();
// TODO To be refactored - use idiomatic choice with correct recoverable behaviour
match predicate_value(reader) {
Ok(PredicateValue::Null {}) => Ok(PredicateFuncValue::EqualNull { space0, operator }),
Ok(PredicateValue::Bool { value }) => Ok(PredicateFuncValue::EqualBool {
space0,
value,
operator,
}),
Ok(PredicateValue::Int { value }) => Ok(PredicateFuncValue::EqualInt {
space0,
value,
operator,
}),
Ok(PredicateValue::Float { value }) => Ok(PredicateFuncValue::EqualFloat {
space0,
value,
operator,
}),
Ok(PredicateValue::Hex { value }) => Ok(PredicateFuncValue::EqualHex {
space0,
value,
operator,
}),
Ok(PredicateValue::Expression { value }) => Ok(PredicateFuncValue::EqualExpression {
space0,
value,
operator,
}),
Ok(PredicateValue::Template { value }) => Ok(PredicateFuncValue::EqualString {
space0,
value,
operator,
}),
Err(e) => match e.inner {
ParseError::EscapeChar {} | ParseError::OddNumberOfHexDigits {} => Err(e),
_ => Err(Error {
pos: start.pos,
recoverable: false,
inner: ParseError::PredicateValue {},
}),
},
}
let value = predicate_value(reader)?;
Ok(PredicateFuncValue::Equal {
space0,
value,
operator,
})
}
fn not_equal_predicate(reader: &mut Reader) -> ParseResult<'static, PredicateFuncValue> {
@ -165,49 +134,12 @@ fn not_equal_predicate(reader: &mut Reader) -> ParseResult<'static, PredicateFun
} else {
one_or_more_spaces(reader)?
};
let start = reader.state.clone();
match predicate_value(reader) {
Ok(PredicateValue::Null {}) => Ok(PredicateFuncValue::EqualNull { space0, operator }),
Ok(PredicateValue::Bool { value }) => Ok(PredicateFuncValue::NotEqualBool {
space0,
value,
operator,
}),
Ok(PredicateValue::Int { value }) => Ok(PredicateFuncValue::NotEqualInt {
space0,
value,
operator,
}),
Ok(PredicateValue::Float { value }) => Ok(PredicateFuncValue::NotEqualFloat {
space0,
value,
operator,
}),
Ok(PredicateValue::Hex { value }) => Ok(PredicateFuncValue::NotEqualHex {
space0,
value,
operator,
}),
Ok(PredicateValue::Expression { value }) => Ok(PredicateFuncValue::NotEqualExpression {
space0,
value,
operator,
}),
Ok(PredicateValue::Template { value }) => Ok(PredicateFuncValue::NotEqualString {
space0,
value,
operator,
}),
Err(e) => match e.inner {
ParseError::EscapeChar {} | ParseError::OddNumberOfHexDigits {} => Err(e),
_ => Err(Error {
pos: start.pos,
recoverable: false,
inner: ParseError::PredicateValue {},
}),
},
}
let value = predicate_value(reader)?;
Ok(PredicateFuncValue::NotEqual {
space0,
value,
operator,
})
}
fn greater_predicate(reader: &mut Reader) -> ParseResult<'static, PredicateFuncValue> {
@ -218,31 +150,19 @@ fn greater_predicate(reader: &mut Reader) -> ParseResult<'static, PredicateFuncV
one_or_more_spaces(reader)?
};
let start = reader.state.clone();
match predicate_value(reader) {
Ok(PredicateValue::Int { value }) => Ok(PredicateFuncValue::GreaterThanInt {
space0,
value,
operator,
}),
Ok(PredicateValue::Float { value }) => Ok(PredicateFuncValue::GreaterThanFloat {
space0,
value,
operator,
}),
Ok(_) => Err(Error {
let value = predicate_value(reader)?;
if !value.is_number() {
return Err(Error {
pos: start.pos,
recoverable: false,
inner: ParseError::PredicateValue {},
}),
Err(e) => match e.inner {
ParseError::EscapeChar {} => Err(e),
_ => Err(Error {
pos: start.pos,
recoverable: false,
inner: ParseError::PredicateValue {},
}),
},
});
}
Ok(PredicateFuncValue::GreaterThan {
space0,
value,
operator,
})
}
fn greater_or_equal_predicate(reader: &mut Reader) -> ParseResult<'static, PredicateFuncValue> {
@ -253,31 +173,19 @@ fn greater_or_equal_predicate(reader: &mut Reader) -> ParseResult<'static, Predi
one_or_more_spaces(reader)?
};
let start = reader.state.clone();
match predicate_value(reader) {
Ok(PredicateValue::Int { value }) => Ok(PredicateFuncValue::GreaterThanOrEqualInt {
space0,
value,
operator,
}),
Ok(PredicateValue::Float { value }) => Ok(PredicateFuncValue::GreaterThanOrEqualFloat {
space0,
value,
operator,
}),
Ok(_) => Err(Error {
let value = predicate_value(reader)?;
if !value.is_number() {
return Err(Error {
pos: start.pos,
recoverable: false,
inner: ParseError::PredicateValue {},
}),
Err(e) => match e.inner {
ParseError::EscapeChar {} => Err(e),
_ => Err(Error {
pos: start.pos,
recoverable: false,
inner: ParseError::PredicateValue {},
}),
},
});
}
Ok(PredicateFuncValue::GreaterThanOrEqual {
space0,
value,
operator,
})
}
fn less_predicate(reader: &mut Reader) -> ParseResult<'static, PredicateFuncValue> {
@ -288,31 +196,19 @@ fn less_predicate(reader: &mut Reader) -> ParseResult<'static, PredicateFuncValu
one_or_more_spaces(reader)?
};
let start = reader.state.clone();
match predicate_value(reader) {
Ok(PredicateValue::Int { value }) => Ok(PredicateFuncValue::LessThanInt {
space0,
value,
operator,
}),
Ok(PredicateValue::Float { value }) => Ok(PredicateFuncValue::LessThanFloat {
space0,
value,
operator,
}),
Ok(_) => Err(Error {
let value = predicate_value(reader)?;
if !value.is_number() {
return Err(Error {
pos: start.pos,
recoverable: false,
inner: ParseError::PredicateValue {},
}),
Err(e) => match e.inner {
ParseError::EscapeChar {} => Err(e),
_ => Err(Error {
pos: start.pos,
recoverable: false,
inner: ParseError::PredicateValue {},
}),
},
});
}
Ok(PredicateFuncValue::LessThan {
space0,
value,
operator,
})
}
fn less_or_equal_predicate(reader: &mut Reader) -> ParseResult<'static, PredicateFuncValue> {
@ -323,99 +219,85 @@ fn less_or_equal_predicate(reader: &mut Reader) -> ParseResult<'static, Predicat
one_or_more_spaces(reader)?
};
let start = reader.state.clone();
match predicate_value(reader) {
Ok(PredicateValue::Int { value }) => Ok(PredicateFuncValue::LessThanOrEqualInt {
space0,
value,
operator,
}),
Ok(PredicateValue::Float { value }) => Ok(PredicateFuncValue::LessThanOrEqualFloat {
space0,
value,
operator,
}),
Ok(_) => Err(Error {
let value = predicate_value(reader)?;
if !value.is_number() {
return Err(Error {
pos: start.pos,
recoverable: false,
inner: ParseError::PredicateValue {},
}),
Err(e) => match e.inner {
ParseError::EscapeChar {} => Err(e),
_ => Err(Error {
pos: start.pos,
recoverable: false,
inner: ParseError::PredicateValue {},
}),
},
});
}
Ok(PredicateFuncValue::LessThanOrEqual {
space0,
value,
operator,
})
}
fn count_equal_predicate(reader: &mut Reader) -> ParseResult<'static, PredicateFuncValue> {
try_literal("countEquals", reader)?;
let space0 = one_or_more_spaces(reader)?;
let save = reader.state.clone();
let value = match natural(reader) {
Err(_) => {
return Err(Error {
pos: save.pos,
recoverable: false,
inner: ParseError::PredicateValue {},
});
}
Ok(value) => value,
};
let value = predicate_value(reader)?;
if !matches!(value, PredicateValue::Integer(_)) {
return Err(Error {
pos: save.pos,
recoverable: false,
inner: ParseError::PredicateValue {},
});
}
Ok(PredicateFuncValue::CountEqual { space0, value })
}
fn start_with_predicate(reader: &mut Reader) -> ParseResult<'static, PredicateFuncValue> {
try_literal("startsWith", reader)?;
let space0 = one_or_more_spaces(reader)?;
let value = quoted_template(reader)?;
let save = reader.state.clone();
let value = predicate_value(reader)?;
if !value.is_string() {
return Err(Error {
pos: save.pos,
recoverable: false,
inner: ParseError::PredicateValue {},
});
}
Ok(PredicateFuncValue::StartWith { space0, value })
}
fn contain_predicate(reader: &mut Reader) -> ParseResult<'static, PredicateFuncValue> {
try_literal("contains", reader)?;
let space0 = one_or_more_spaces(reader)?;
let value = quoted_template(reader)?;
let save = reader.state.clone();
let value = predicate_value(reader)?;
if !value.is_string() {
return Err(Error {
pos: save.pos,
recoverable: false,
inner: ParseError::PredicateValue {},
});
}
Ok(PredicateFuncValue::Contain { space0, value })
}
fn include_predicate(reader: &mut Reader) -> ParseResult<'static, PredicateFuncValue> {
try_literal("includes", reader)?;
let space0 = one_or_more_spaces(reader)?;
let start = reader.state.clone();
match predicate_value(reader) {
Ok(PredicateValue::Null {}) => Ok(PredicateFuncValue::IncludeNull { space0 }),
Ok(PredicateValue::Bool { value }) => Ok(PredicateFuncValue::IncludeBool { space0, value }),
Ok(PredicateValue::Int { value }) => Ok(PredicateFuncValue::IncludeInt { space0, value }),
Ok(PredicateValue::Float { value }) => {
Ok(PredicateFuncValue::IncludeFloat { space0, value })
}
Ok(PredicateValue::Template { value }) => {
Ok(PredicateFuncValue::IncludeString { space0, value })
}
Ok(PredicateValue::Hex { value: _ }) => {
todo!()
}
Ok(PredicateValue::Expression { value }) => {
Ok(PredicateFuncValue::IncludeExpression { space0, value })
}
Err(e) => match e.inner {
ParseError::EscapeChar {} => Err(e),
_ => Err(Error {
pos: start.pos,
recoverable: false,
inner: ParseError::PredicateValue {},
}),
},
}
let value = predicate_value(reader)?;
Ok(PredicateFuncValue::Include { space0, value })
}
fn match_predicate(reader: &mut Reader) -> ParseResult<'static, PredicateFuncValue> {
try_literal("matches", reader)?;
let space0 = one_or_more_spaces(reader)?;
let value = quoted_template(reader)?;
let save = reader.state.clone();
let value = predicate_value(reader)?;
if !value.is_string() {
return Err(Error {
pos: save.pos,
recoverable: false,
inner: ParseError::PredicateValue {},
});
}
Ok(PredicateFuncValue::Match { space0, value })
}
@ -449,54 +331,6 @@ fn exist_predicate(reader: &mut Reader) -> ParseResult<'static, PredicateFuncVal
Ok(PredicateFuncValue::Exist {})
}
/* internal to the parser */
#[derive(Clone, Debug, PartialEq, Eq)]
enum PredicateValue {
Null {},
Int { value: i64 },
Float { value: Float },
Bool { value: bool },
Template { value: Template },
Hex { value: Hex },
Expression { value: Expr },
}
fn predicate_value(reader: &mut Reader) -> ParseResult<'static, PredicateValue> {
choice(
vec![
|p1| match null(p1) {
Ok(()) => Ok(PredicateValue::Null {}),
Err(e) => Err(e),
},
|p1| match boolean(p1) {
Ok(value) => Ok(PredicateValue::Bool { value }),
Err(e) => Err(e),
},
|p1| match float(p1) {
Ok(value) => Ok(PredicateValue::Float { value }),
Err(e) => Err(e),
},
|p1| match integer(p1) {
Ok(value) => Ok(PredicateValue::Int { value }),
Err(e) => Err(e),
},
|p1| match hex(p1) {
Ok(value) => Ok(PredicateValue::Hex { value }),
Err(e) => Err(e),
},
|p1| match expr::parse(p1) {
Ok(value) => Ok(PredicateValue::Expression { value }),
Err(e) => Err(e),
},
|p1| match quoted_template(p1) {
Ok(value) => Ok(PredicateValue::Template { value }),
Err(e) => Err(e),
},
],
reader,
)
}
#[cfg(test)]
mod tests {
use crate::ast::Pos;
@ -545,12 +379,12 @@ mod tests {
},
predicate_func: PredicateFunc {
source_info: SourceInfo::init(1, 5, 1, 16),
value: PredicateFuncValue::EqualBool {
value: PredicateFuncValue::Equal {
space0: Whitespace {
value: String::from(" "),
source_info: SourceInfo::init(1, 11, 1, 12),
},
value: true,
value: PredicateValue::Bool(true),
operator: false
},
},
@ -587,8 +421,8 @@ mod tests {
let mut reader = Reader::init("equals true");
assert_eq!(
equal_predicate(&mut reader).unwrap(),
PredicateFuncValue::EqualBool {
value: true,
PredicateFuncValue::Equal {
value: PredicateValue::Bool(true),
space0: Whitespace {
value: String::from(" "),
source_info: SourceInfo::init(1, 7, 1, 9),
@ -601,12 +435,12 @@ mod tests {
let mut reader = Reader::init("equals 1.1");
assert_eq!(
equal_predicate(&mut reader).unwrap(),
PredicateFuncValue::EqualFloat {
value: Float {
PredicateFuncValue::Equal {
value: PredicateValue::Float(Float {
int: 1,
decimal: 100_000_000_000_000_000,
decimal_digits: 1,
},
}),
space0: Whitespace {
value: String::from(" "),
source_info: SourceInfo::init(1, 7, 1, 8),
@ -618,8 +452,8 @@ mod tests {
let mut reader = Reader::init("equals 2");
assert_eq!(
equal_predicate(&mut reader).unwrap(),
PredicateFuncValue::EqualInt {
value: 2,
PredicateFuncValue::Equal {
value: PredicateValue::Integer(2),
space0: Whitespace {
value: String::from(" "),
source_info: SourceInfo::init(1, 7, 1, 8),
@ -631,8 +465,8 @@ mod tests {
let mut reader = Reader::init("== 2");
assert_eq!(
equal_predicate(&mut reader).unwrap(),
PredicateFuncValue::EqualInt {
value: 2,
PredicateFuncValue::Equal {
value: PredicateValue::Integer(2),
space0: Whitespace {
value: String::from(" "),
source_info: SourceInfo::init(1, 3, 1, 4),
@ -644,15 +478,15 @@ mod tests {
let mut reader = Reader::init("equals \"Bob\"");
assert_eq!(
equal_predicate(&mut reader).unwrap(),
PredicateFuncValue::EqualString {
value: Template {
PredicateFuncValue::Equal {
value: PredicateValue::String(Template {
quotes: true,
elements: vec![TemplateElement::String {
value: "Bob".to_string(),
encoded: "Bob".to_string(),
}],
source_info: SourceInfo::init(1, 8, 1, 13),
},
}),
space0: Whitespace {
value: String::from(" "),
source_info: SourceInfo::init(1, 7, 1, 8),
@ -667,8 +501,8 @@ mod tests {
let mut reader = Reader::init("equals {{count}}");
assert_eq!(
equal_predicate(&mut reader).unwrap(),
PredicateFuncValue::EqualExpression {
value: Expr {
PredicateFuncValue::Equal {
value: PredicateValue::Expression(Expr {
space0: Whitespace {
value: String::from(""),
source_info: SourceInfo::init(1, 10, 1, 10),
@ -681,7 +515,7 @@ mod tests {
value: String::from(""),
source_info: SourceInfo::init(1, 15, 1, 15),
},
},
}),
space0: Whitespace {
value: String::from(" "),
source_info: SourceInfo::init(1, 7, 1, 8),
@ -697,7 +531,7 @@ mod tests {
assert_eq!(
count_equal_predicate(&mut reader).unwrap(),
PredicateFuncValue::CountEqual {
value: 2,
value: PredicateValue::Integer(2),
space0: Whitespace {
value: String::from(" "),
source_info: SourceInfo::init(1, 12, 1, 13),
@ -730,38 +564,6 @@ mod tests {
}
);
assert_eq!(error.recoverable, false);
assert_eq!(
error.inner,
ParseError::Expecting {
value: "\"".to_string()
}
);
}
#[test]
fn test_predicate_value() {
let mut reader = Reader::init("true");
assert_eq!(
predicate_value(&mut reader).unwrap(),
PredicateValue::Bool { value: true }
);
let mut reader = Reader::init("1");
assert_eq!(
predicate_value(&mut reader).unwrap(),
PredicateValue::Int { value: 1 }
);
let mut reader = Reader::init("1.1");
assert_eq!(
predicate_value(&mut reader).unwrap(),
PredicateValue::Float {
value: Float {
int: 1,
decimal: 100_000_000_000_000_000,
decimal_digits: 1,
}
}
);
assert_eq!(error.inner, ParseError::PredicateValue {});
}
}

View File

@ -0,0 +1,113 @@
/*
* hurl (https://hurl.dev)
* Copyright (C) 2020 Orange
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
use crate::ast::*;
use super::combinators::*;
use super::expr;
use super::primitives::*;
use super::reader::Reader;
use super::string::*;
use super::ParseResult;
use crate::parser::{Error, ParseError};
pub fn predicate_value(reader: &mut Reader) -> ParseResult<'static, PredicateValue> {
choice(
vec![
|p1| match null(p1) {
Ok(()) => Ok(PredicateValue::Null {}),
Err(e) => Err(e),
},
|p1| match boolean(p1) {
Ok(value) => Ok(PredicateValue::Bool(value)),
Err(e) => Err(e),
},
|p1| match float(p1) {
Ok(value) => Ok(PredicateValue::Float(value)),
Err(e) => Err(e),
},
|p1| match integer(p1) {
Ok(value) => Ok(PredicateValue::Integer(value)),
Err(e) => Err(e),
},
|p1| match hex(p1) {
Ok(value) => Ok(PredicateValue::Hex(value)),
Err(e) => Err(e),
},
|p1| match expr::parse(p1) {
Ok(value) => Ok(PredicateValue::Expression(value)),
Err(e) => Err(e),
},
|p1| match quoted_template(p1) {
Ok(value) => Ok(PredicateValue::String(value)),
Err(e) => Err(e),
},
],
reader,
)
.map_err(|e| Error {
pos: e.pos,
recoverable: false,
inner: if e.recoverable {
ParseError::PredicateValue
} else {
e.inner
},
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::parser::ParseError;
#[test]
fn test_predicate_value() {
let mut reader = Reader::init("true");
assert_eq!(
predicate_value(&mut reader).unwrap(),
PredicateValue::Bool(true)
);
let mut reader = Reader::init("1");
assert_eq!(
predicate_value(&mut reader).unwrap(),
PredicateValue::Integer(1)
);
let mut reader = Reader::init("1.1");
assert_eq!(
predicate_value(&mut reader).unwrap(),
PredicateValue::Float(Float {
int: 1,
decimal: 100_000_000_000_000_000,
decimal_digits: 1,
})
);
}
#[test]
fn test_predicate_value_error() {
let mut reader = Reader::init("xx");
let error = predicate_value(&mut reader).err().unwrap();
assert_eq!(error.pos, Pos { line: 1, column: 1 });
assert_eq!(error.inner, ParseError::PredicateValue {});
assert!(!error.recoverable);
}
}

View File

@ -82,7 +82,7 @@ fn status_query(reader: &mut Reader) -> ParseResult<'static, QueryValue> {
fn header_query(reader: &mut Reader) -> ParseResult<'static, QueryValue> {
try_literal("header", reader)?;
let space0 = one_or_more_spaces(reader)?;
let name = quoted_template(reader)?;
let name = quoted_template(reader).map_err(|e| e.non_recoverable())?;
Ok(QueryValue::Header { space0, name })
}
@ -112,7 +112,7 @@ fn body_query(reader: &mut Reader) -> ParseResult<'static, QueryValue> {
fn xpath_query(reader: &mut Reader) -> ParseResult<'static, QueryValue> {
try_literal("xpath", reader)?;
let space0 = one_or_more_spaces(reader)?;
let expr = quoted_template(reader)?;
let expr = quoted_template(reader).map_err(|e| e.non_recoverable())?;
Ok(QueryValue::Xpath { space0, expr })
}
@ -121,7 +121,7 @@ fn jsonpath_query(reader: &mut Reader) -> ParseResult<'static, QueryValue> {
let space0 = one_or_more_spaces(reader)?;
//let expr = jsonpath_expr(reader)?;
// let start = reader.state.pos.clone();
let expr = quoted_template(reader)?;
let expr = quoted_template(reader).map_err(|e| e.non_recoverable())?;
// let end = reader.state.pos.clone();
// let expr = Template {
// elements: template.elements.iter().map(|e| match e {
@ -140,14 +140,14 @@ fn jsonpath_query(reader: &mut Reader) -> ParseResult<'static, QueryValue> {
fn regex_query(reader: &mut Reader) -> ParseResult<'static, QueryValue> {
try_literal("regex", reader)?;
let space0 = one_or_more_spaces(reader)?;
let expr = quoted_template(reader)?;
let expr = quoted_template(reader).map_err(|e| e.non_recoverable())?;
Ok(QueryValue::Regex { space0, expr })
}
fn variable_query(reader: &mut Reader) -> ParseResult<'static, QueryValue> {
try_literal("variable", reader)?;
let space0 = one_or_more_spaces(reader)?;
let name = quoted_template(reader)?;
let name = quoted_template(reader).map_err(|e| e.non_recoverable())?;
Ok(QueryValue::Variable { space0, name })
}

View File

@ -402,7 +402,7 @@ mod tests {
source_info: SourceInfo::init(2, 8, 2, 18),
},
},
subquery: None
subquery: None,
},
space1: Whitespace {
value: String::from(" "),
@ -416,20 +416,20 @@ mod tests {
},
predicate_func: PredicateFunc {
source_info: SourceInfo::init(2, 19, 2, 45),
value: PredicateFuncValue::EqualString {
value: PredicateFuncValue::Equal {
space0: Whitespace {
value: String::from(" "),
source_info: SourceInfo::init(2, 25, 2, 26),
},
value: Template {
value: PredicateValue::String(Template {
quotes: true,
elements: vec![TemplateElement::String {
value: "https://google.fr".to_string(),
encoded: "https://google.fr".to_string(),
}],
source_info: SourceInfo::init(2, 26, 2, 45),
},
operator: false
}),
operator: false,
},
},
},
@ -623,7 +623,7 @@ mod tests {
source_info: SourceInfo::init(1, 13, 1, 23),
},
},
subquery: None
subquery: None,
}
);
}
@ -655,26 +655,26 @@ mod tests {
subquery: Some((
Whitespace {
value: " ".to_string(),
source_info: SourceInfo::init(1, 25, 1, 26)
source_info: SourceInfo::init(1, 25, 1, 26),
},
Subquery {
source_info: SourceInfo::init(1, 26, 1, 44),
value: SubqueryValue::Regex {
space0: Whitespace {
value: " ".to_string(),
source_info: SourceInfo::init(1, 31, 1, 32)
source_info: SourceInfo::init(1, 31, 1, 32),
},
expr: Template {
quotes: true,
elements: vec![TemplateElement::String {
value: "token=(.*)".to_string(),
encoded: "token=(.*)".to_string()
encoded: "token=(.*)".to_string(),
}],
source_info: SourceInfo::init(1, 32, 1, 44)
}
}
source_info: SourceInfo::init(1, 32, 1, 44),
},
},
}
))
)),
}
);
assert_eq!(reader.state.cursor, 43);
@ -688,7 +688,7 @@ mod tests {
error.pos,
Pos {
line: 1,
column: 32,
column: 32
}
);
assert_eq!(
@ -740,7 +740,7 @@ mod tests {
source_info: SourceInfo::init(1, 8, 1, 18),
},
},
subquery: None
subquery: None,
}
);
}
@ -759,13 +759,13 @@ mod tests {
},
predicate_func: PredicateFunc {
source_info: SourceInfo::init(1, 21, 1, 29),
value: PredicateFuncValue::EqualInt {
value: PredicateFuncValue::Equal {
space0: Whitespace {
value: String::from(" "),
source_info: SourceInfo::init(1, 27, 1, 28),
},
value: 5,
operator: false
value: PredicateValue::Integer(5),
operator: false,
},
},
}

View File

@ -148,7 +148,7 @@ pub fn quoted_template(reader: &mut Reader) -> ParseResult<'static, Template> {
let quotes = true;
let start = reader.state.clone().pos;
let mut end = start.clone();
literal("\"", reader)?;
try_literal("\"", reader)?;
let mut chars = vec![];
loop {
let pos = reader.state.pos.clone();

View File

@ -41,7 +41,7 @@ fn subquery_value(reader: &mut Reader) -> ParseResult<'static, SubqueryValue> {
fn regex_subquery(reader: &mut Reader) -> ParseResult<'static, SubqueryValue> {
try_literal("regex", reader)?;
let space0 = one_or_more_spaces(reader)?;
let expr = quoted_template(reader)?;
let expr = quoted_template(reader).map_err(|e| e.non_recoverable())?;
Ok(SubqueryValue::Regex { space0, expr })
}

View File

@ -332,166 +332,55 @@ impl ToJson for Predicate {
attributes.push(("not".to_string(), JValue::Boolean(true)))
}
match self.predicate_func.value.clone() {
PredicateFuncValue::EqualInt { value, .. } => {
PredicateFuncValue::Equal { value, .. } => {
attributes.push(("type".to_string(), JValue::String("equal".to_string())));
attributes.push(("value".to_string(), JValue::Number(value.to_string())));
attributes.push(("value".to_string(), value.to_json()));
}
PredicateFuncValue::EqualBool { value, .. } => {
attributes.push(("type".to_string(), JValue::String("equal".to_string())));
attributes.push(("value".to_string(), JValue::Boolean(value)));
}
PredicateFuncValue::EqualString { value, .. } => {
attributes.push(("type".to_string(), JValue::String("equal".to_string())));
attributes.push(("value".to_string(), JValue::String(value.to_string())));
}
PredicateFuncValue::EqualFloat { value, .. } => {
attributes.push(("type".to_string(), JValue::String("equal".to_string())));
attributes.push(("value".to_string(), JValue::Number(value.to_string())));
}
PredicateFuncValue::EqualNull { .. } => {
attributes.push(("type".to_string(), JValue::String("equal".to_string())));
attributes.push(("value".to_string(), JValue::Null));
}
PredicateFuncValue::EqualHex { value, .. } => {
attributes.push(("type".to_string(), JValue::String("equal".to_string())));
let value = JValue::Object(vec![
(
"value".to_string(),
JValue::String(base64::encode(&value.value)),
),
("encoding".to_string(), JValue::String("base64".to_string())),
]);
attributes.push(("value".to_string(), value));
}
PredicateFuncValue::EqualExpression { value, .. } => {
attributes.push((
"type".to_string(),
JValue::String(
"\
equal"
.to_string(),
),
));
attributes.push(("value".to_string(), JValue::String(value.to_string())));
}
PredicateFuncValue::NotEqualInt { value, .. } => {
PredicateFuncValue::NotEqual { value, .. } => {
attributes.push(("type".to_string(), JValue::String("not-equal".to_string())));
attributes.push(("value".to_string(), JValue::Number(value.to_string())));
attributes.push(("value".to_string(), value.to_json()));
}
PredicateFuncValue::NotEqualBool { value, .. } => {
attributes.push(("type".to_string(), JValue::String("not-equal".to_string())));
attributes.push(("value".to_string(), JValue::Boolean(value)));
}
PredicateFuncValue::NotEqualString { value, .. } => {
attributes.push(("type".to_string(), JValue::String("not-equal".to_string())));
attributes.push(("value".to_string(), JValue::String(value.to_string())));
}
PredicateFuncValue::NotEqualFloat { value, .. } => {
attributes.push(("type".to_string(), JValue::String("not-equal".to_string())));
attributes.push(("value".to_string(), JValue::Number(value.to_string())));
}
PredicateFuncValue::NotEqualNull { .. } => {
attributes.push(("type".to_string(), JValue::String("not-equal".to_string())));
attributes.push(("value".to_string(), JValue::Null));
}
PredicateFuncValue::NotEqualHex { value, .. } => {
attributes.push(("type".to_string(), JValue::String("not-equal".to_string())));
let value = JValue::Object(vec![
(
"value".to_string(),
JValue::String(base64::encode(&value.value)),
),
("encoding".to_string(), JValue::String("base64".to_string())),
]);
attributes.push(("value".to_string(), value));
}
PredicateFuncValue::NotEqualExpression { value, .. } => {
attributes.push(("type".to_string(), JValue::String("not-equal".to_string())));
attributes.push(("value".to_string(), JValue::String(value.to_string())));
}
PredicateFuncValue::GreaterThanInt { value, .. } => {
PredicateFuncValue::GreaterThan { value, .. } => {
attributes.push(("type".to_string(), JValue::String("greater".to_string())));
attributes.push(("value".to_string(), JValue::Number(value.to_string())));
attributes.push(("value".to_string(), value.to_json()));
}
PredicateFuncValue::GreaterThanFloat { value, .. } => {
attributes.push(("type".to_string(), JValue::String("greater".to_string())));
attributes.push(("value".to_string(), JValue::Number(value.to_string())));
}
PredicateFuncValue::GreaterThanOrEqualInt { value, .. } => {
PredicateFuncValue::GreaterThanOrEqual { value, .. } => {
attributes.push((
"type".to_string(),
JValue::String("greater-or-equal".to_string()),
));
attributes.push(("value".to_string(), JValue::Number(value.to_string())));
attributes.push(("value".to_string(), value.to_json()));
}
PredicateFuncValue::GreaterThanOrEqualFloat { value, .. } => {
attributes.push((
"type".to_string(),
JValue::String("greater-or-equal".to_string()),
));
attributes.push(("value".to_string(), JValue::Number(value.to_string())));
}
PredicateFuncValue::LessThanInt { value, .. } => {
PredicateFuncValue::LessThan { value, .. } => {
attributes.push(("type".to_string(), JValue::String("less".to_string())));
attributes.push(("value".to_string(), JValue::Number(value.to_string())));
attributes.push(("value".to_string(), value.to_json()));
}
PredicateFuncValue::LessThanFloat { value, .. } => {
attributes.push(("type".to_string(), JValue::String("less".to_string())));
attributes.push(("value".to_string(), JValue::Number(value.to_string())));
}
PredicateFuncValue::LessThanOrEqualInt { value, .. } => {
PredicateFuncValue::LessThanOrEqual { value, .. } => {
attributes.push((
"type".to_string(),
JValue::String("less-or-equal".to_string()),
));
attributes.push(("value".to_string(), JValue::Number(value.to_string())));
}
PredicateFuncValue::LessThanOrEqualFloat { value, .. } => {
attributes.push((
"type".to_string(),
JValue::String("less-or-equal".to_string()),
));
attributes.push(("value".to_string(), JValue::Number(value.to_string())));
attributes.push(("value".to_string(), value.to_json()));
}
PredicateFuncValue::CountEqual { value, .. } => {
attributes.push(("type".to_string(), JValue::String("count".to_string())));
attributes.push(("value".to_string(), JValue::Number(value.to_string())));
attributes.push(("value".to_string(), value.to_json()));
}
PredicateFuncValue::StartWith { value, .. } => {
attributes.push(("type".to_string(), JValue::String("start-with".to_string())));
attributes.push(("value".to_string(), JValue::String(value.to_string())));
attributes.push(("value".to_string(), value.to_json()));
}
PredicateFuncValue::Contain { value, .. } => {
attributes.push(("type".to_string(), JValue::String("contain".to_string())));
attributes.push(("value".to_string(), JValue::String(value.to_string())));
attributes.push(("value".to_string(), value.to_json()));
}
PredicateFuncValue::IncludeString { value, .. } => {
PredicateFuncValue::Include { value, .. } => {
attributes.push(("type".to_string(), JValue::String("include".to_string())));
attributes.push(("value".to_string(), JValue::String(value.to_string())));
}
PredicateFuncValue::IncludeInt { value, .. } => {
attributes.push(("type".to_string(), JValue::String("include".to_string())));
attributes.push(("value".to_string(), JValue::Number(value.to_string())));
}
PredicateFuncValue::IncludeFloat { value, .. } => {
attributes.push(("type".to_string(), JValue::String("include".to_string())));
attributes.push(("value".to_string(), JValue::Number(value.to_string())));
}
PredicateFuncValue::IncludeBool { value, .. } => {
attributes.push(("type".to_string(), JValue::String("include".to_string())));
attributes.push(("value".to_string(), JValue::Boolean(value)));
}
PredicateFuncValue::IncludeNull { .. } => {
attributes.push(("type".to_string(), JValue::String("include".to_string())));
attributes.push(("value".to_string(), JValue::Null));
}
PredicateFuncValue::IncludeExpression { value, .. } => {
attributes.push(("type".to_string(), JValue::String("include".to_string())));
attributes.push(("value".to_string(), JValue::String(value.to_string())));
attributes.push(("value".to_string(), value.to_json()));
}
PredicateFuncValue::Match { value, .. } => {
attributes.push(("type".to_string(), JValue::String("match".to_string())));
attributes.push(("value".to_string(), JValue::String(value.to_string())));
attributes.push(("value".to_string(), value.to_json()));
}
PredicateFuncValue::IsInteger {} => {
attributes.push(("type".to_string(), JValue::String("isInteger".to_string())));
@ -519,6 +408,26 @@ impl ToJson for Predicate {
}
}
impl ToJson for PredicateValue {
fn to_json(&self) -> JValue {
match self {
PredicateValue::String(value) => JValue::String(value.to_string()),
PredicateValue::Integer(value) => JValue::Number(value.to_string()),
PredicateValue::Float(value) => JValue::Number(value.to_string()),
PredicateValue::Bool(value) => JValue::Boolean(*value),
PredicateValue::Null {} => JValue::Null,
PredicateValue::Hex(value) => JValue::Object(vec![
(
"value".to_string(),
JValue::String(base64::encode(&value.value)),
),
("encoding".to_string(), JValue::String("base64".to_string())),
]),
PredicateValue::Expression(value) => JValue::String(value.to_string()),
}
}
}
impl ToJson for hurl_core::ast::JsonValue {
fn to_json(&self) -> JValue {
match self {
@ -731,9 +640,9 @@ pub mod tests {
space0: whitespace(),
predicate_func: PredicateFunc {
source_info: SourceInfo::init(0, 0, 0, 0),
value: PredicateFuncValue::EqualInt {
value: PredicateFuncValue::Equal {
space0: whitespace(),
value,
value: PredicateValue::Integer(value),
operator: false,
},
},

View File

@ -538,120 +538,40 @@ impl Tokenizable for PredicateFuncValue {
fn tokenize(&self) -> Vec<Token> {
let mut tokens: Vec<Token> = vec![];
match self {
PredicateFuncValue::EqualNull { space0, .. } => {
tokens.push(Token::PredicateType(self.name()));
add_tokens(&mut tokens, space0.tokenize());
tokens.push(Token::Keyword("null".to_string()));
}
PredicateFuncValue::EqualBool { space0, value, .. } => {
tokens.push(Token::PredicateType(self.name()));
add_tokens(&mut tokens, space0.tokenize());
tokens.push(Token::Boolean(value.to_string()));
}
PredicateFuncValue::EqualString { space0, value, .. } => {
tokens.push(Token::PredicateType(self.name()));
add_tokens(&mut tokens, space0.tokenize());
add_tokens(&mut tokens, value.tokenize());
}
PredicateFuncValue::EqualInt { space0, value, .. } => {
tokens.push(Token::PredicateType(self.name()));
add_tokens(&mut tokens, space0.tokenize());
tokens.push(Token::Number(value.to_string()));
}
PredicateFuncValue::EqualFloat { space0, value, .. } => {
tokens.push(Token::PredicateType(self.name()));
add_tokens(&mut tokens, space0.tokenize());
tokens.push(Token::Number(value.to_string()));
}
PredicateFuncValue::EqualHex { space0, value, .. } => {
tokens.push(Token::PredicateType(self.name()));
add_tokens(&mut tokens, space0.tokenize());
tokens.push(Token::String(value.to_string()));
}
PredicateFuncValue::EqualExpression { space0, value, .. } => {
PredicateFuncValue::Equal { space0, value, .. } => {
tokens.push(Token::PredicateType(self.name()));
add_tokens(&mut tokens, space0.tokenize());
tokens.append(&mut value.tokenize());
}
PredicateFuncValue::NotEqualNull { space0, .. } => {
tokens.push(Token::PredicateType(self.name()));
add_tokens(&mut tokens, space0.tokenize());
tokens.push(Token::Keyword("null".to_string()));
}
PredicateFuncValue::NotEqualBool { space0, value, .. } => {
tokens.push(Token::PredicateType(self.name()));
add_tokens(&mut tokens, space0.tokenize());
tokens.push(Token::Boolean(value.to_string()));
}
PredicateFuncValue::NotEqualString { space0, value, .. } => {
tokens.push(Token::PredicateType(self.name()));
add_tokens(&mut tokens, space0.tokenize());
add_tokens(&mut tokens, value.tokenize());
}
PredicateFuncValue::NotEqualInt { space0, value, .. } => {
tokens.push(Token::PredicateType(self.name()));
add_tokens(&mut tokens, space0.tokenize());
tokens.push(Token::Number(value.to_string()));
}
PredicateFuncValue::NotEqualFloat { space0, value, .. } => {
tokens.push(Token::PredicateType(self.name()));
add_tokens(&mut tokens, space0.tokenize());
tokens.push(Token::Number(value.to_string()));
}
PredicateFuncValue::NotEqualHex { space0, value, .. } => {
tokens.push(Token::PredicateType(self.name()));
add_tokens(&mut tokens, space0.tokenize());
tokens.push(Token::String(value.to_string()));
}
PredicateFuncValue::NotEqualExpression { space0, value, .. } => {
PredicateFuncValue::NotEqual { space0, value, .. } => {
tokens.push(Token::PredicateType(self.name()));
add_tokens(&mut tokens, space0.tokenize());
tokens.append(&mut value.tokenize());
}
PredicateFuncValue::GreaterThanInt { space0, value, .. } => {
PredicateFuncValue::GreaterThan { space0, value, .. } => {
tokens.push(Token::PredicateType(self.name()));
add_tokens(&mut tokens, space0.tokenize());
tokens.push(Token::Number(value.to_string()));
tokens.append(&mut value.tokenize());
}
PredicateFuncValue::GreaterThanFloat { space0, value, .. } => {
PredicateFuncValue::GreaterThanOrEqual { space0, value, .. } => {
tokens.push(Token::PredicateType(self.name()));
add_tokens(&mut tokens, space0.tokenize());
tokens.push(Token::Number(value.to_string()));
tokens.append(&mut value.tokenize());
}
PredicateFuncValue::GreaterThanOrEqualInt { space0, value, .. } => {
PredicateFuncValue::LessThan { space0, value, .. } => {
tokens.push(Token::PredicateType(self.name()));
add_tokens(&mut tokens, space0.tokenize());
tokens.push(Token::Number(value.to_string()));
tokens.append(&mut value.tokenize());
}
PredicateFuncValue::GreaterThanOrEqualFloat { space0, value, .. } => {
PredicateFuncValue::LessThanOrEqual { space0, value, .. } => {
tokens.push(Token::PredicateType(self.name()));
add_tokens(&mut tokens, space0.tokenize());
tokens.push(Token::Number(value.to_string()));
}
PredicateFuncValue::LessThanInt { space0, value, .. } => {
tokens.push(Token::PredicateType(self.name()));
add_tokens(&mut tokens, space0.tokenize());
tokens.push(Token::Number(value.to_string()));
}
PredicateFuncValue::LessThanFloat { space0, value, .. } => {
tokens.push(Token::PredicateType(self.name()));
add_tokens(&mut tokens, space0.tokenize());
tokens.push(Token::Number(value.to_string()));
}
PredicateFuncValue::LessThanOrEqualInt { space0, value, .. } => {
tokens.push(Token::PredicateType(self.name()));
add_tokens(&mut tokens, space0.tokenize());
tokens.push(Token::Number(value.to_string()));
}
PredicateFuncValue::LessThanOrEqualFloat { space0, value, .. } => {
tokens.push(Token::PredicateType(self.name()));
add_tokens(&mut tokens, space0.tokenize());
tokens.push(Token::Number(value.to_string()));
tokens.append(&mut value.tokenize());
}
PredicateFuncValue::CountEqual { space0, value } => {
tokens.push(Token::PredicateType(self.name()));
add_tokens(&mut tokens, space0.tokenize());
tokens.push(Token::Boolean(value.to_string()));
tokens.append(&mut value.tokenize());
}
PredicateFuncValue::StartWith { space0, value } => {
tokens.push(Token::PredicateType(self.name()));
@ -663,32 +583,7 @@ impl Tokenizable for PredicateFuncValue {
add_tokens(&mut tokens, space0.tokenize());
add_tokens(&mut tokens, value.tokenize());
}
PredicateFuncValue::IncludeString { space0, value } => {
tokens.push(Token::PredicateType(self.name()));
add_tokens(&mut tokens, space0.tokenize());
add_tokens(&mut tokens, value.tokenize());
}
PredicateFuncValue::IncludeInt { space0, value } => {
tokens.push(Token::PredicateType(self.name()));
add_tokens(&mut tokens, space0.tokenize());
tokens.push(Token::Number(value.to_string()));
}
PredicateFuncValue::IncludeFloat { space0, value } => {
tokens.push(Token::PredicateType(self.name()));
add_tokens(&mut tokens, space0.tokenize());
tokens.push(Token::Number(value.to_string()));
}
PredicateFuncValue::IncludeNull { space0 } => {
tokens.push(Token::PredicateType(self.name()));
add_tokens(&mut tokens, space0.tokenize());
tokens.push(Token::Keyword("null".to_string()));
}
PredicateFuncValue::IncludeBool { space0, value } => {
tokens.push(Token::PredicateType(self.name()));
add_tokens(&mut tokens, space0.tokenize());
tokens.push(Token::Boolean(value.to_string()));
}
PredicateFuncValue::IncludeExpression { space0, value } => {
PredicateFuncValue::Include { space0, value } => {
tokens.push(Token::PredicateType(self.name()));
add_tokens(&mut tokens, space0.tokenize());
tokens.append(&mut value.tokenize());
@ -722,6 +617,20 @@ impl Tokenizable for PredicateFuncValue {
}
}
impl Tokenizable for PredicateValue {
fn tokenize(&self) -> Vec<Token> {
match self {
PredicateValue::String(value) => value.tokenize(),
PredicateValue::Integer(value) => vec![Token::Number(value.to_string())],
PredicateValue::Float(value) => vec![Token::Number(value.to_string())],
PredicateValue::Bool(value) => vec![Token::Boolean(value.to_string())],
PredicateValue::Null {} => vec![Token::Keyword("null".to_string())],
PredicateValue::Hex(value) => vec![Token::String(value.to_string())],
PredicateValue::Expression(value) => value.tokenize(),
}
}
}
impl Tokenizable for EncodedString {
fn tokenize(&self) -> Vec<Token> {
let mut tokens: Vec<Token> = vec![];

View File

@ -418,131 +418,37 @@ impl Lintable<PredicateFuncValue> for PredicateFuncValue {
#[allow(clippy::clone_on_copy)]
fn lint(&self) -> PredicateFuncValue {
match self {
PredicateFuncValue::EqualString { value, .. } => PredicateFuncValue::EqualString {
space0: one_whitespace(),
value: value.clone().lint(),
operator: true,
},
PredicateFuncValue::EqualInt { value, .. } => PredicateFuncValue::EqualInt {
space0: one_whitespace(),
value: value.clone(),
operator: true,
},
PredicateFuncValue::EqualBool { value, .. } => PredicateFuncValue::EqualBool {
space0: one_whitespace(),
value: value.clone(),
operator: true,
},
PredicateFuncValue::EqualNull { .. } => PredicateFuncValue::EqualNull {
space0: one_whitespace(),
operator: true,
},
PredicateFuncValue::EqualFloat { value, .. } => PredicateFuncValue::EqualFloat {
space0: one_whitespace(),
value: value.clone(),
operator: true,
},
PredicateFuncValue::EqualHex { value, .. } => PredicateFuncValue::EqualHex {
PredicateFuncValue::Equal { value, .. } => PredicateFuncValue::Equal {
space0: one_whitespace(),
value: value.lint(),
operator: true,
},
PredicateFuncValue::EqualExpression { value, .. } => {
PredicateFuncValue::EqualExpression {
space0: one_whitespace(),
value: value.clone(),
operator: true,
}
}
PredicateFuncValue::NotEqualString { value, .. } => {
PredicateFuncValue::NotEqualString {
space0: one_whitespace(),
value: value.clone().lint(),
operator: true,
}
}
PredicateFuncValue::NotEqualInt { value, .. } => PredicateFuncValue::NotEqualInt {
space0: one_whitespace(),
value: value.clone(),
operator: true,
},
PredicateFuncValue::NotEqualBool { value, .. } => PredicateFuncValue::NotEqualBool {
space0: one_whitespace(),
value: value.clone(),
operator: true,
},
PredicateFuncValue::NotEqualNull { .. } => PredicateFuncValue::NotEqualNull {
space0: one_whitespace(),
operator: true,
},
PredicateFuncValue::NotEqualFloat { value, .. } => PredicateFuncValue::NotEqualFloat {
space0: one_whitespace(),
value: value.clone(),
operator: true,
},
PredicateFuncValue::NotEqualHex { value, .. } => PredicateFuncValue::NotEqualHex {
PredicateFuncValue::NotEqual { value, .. } => PredicateFuncValue::NotEqual {
space0: one_whitespace(),
value: value.lint(),
operator: true,
},
PredicateFuncValue::NotEqualExpression { value, .. } => {
PredicateFuncValue::NotEqualExpression {
space0: one_whitespace(),
value: value.clone(),
operator: true,
}
}
PredicateFuncValue::GreaterThanInt { value, .. } => {
PredicateFuncValue::GreaterThanInt {
space0: one_whitespace(),
value: value.clone(),
operator: true,
}
}
PredicateFuncValue::GreaterThanFloat { value, .. } => {
PredicateFuncValue::GreaterThanFloat {
space0: one_whitespace(),
value: value.clone(),
operator: true,
}
}
PredicateFuncValue::GreaterThanOrEqualInt { value, .. } => {
PredicateFuncValue::GreaterThanOrEqualInt {
space0: one_whitespace(),
value: value.clone(),
operator: true,
}
}
PredicateFuncValue::GreaterThanOrEqualFloat { value, .. } => {
PredicateFuncValue::GreaterThanOrEqualFloat {
space0: one_whitespace(),
value: value.clone(),
operator: true,
}
}
PredicateFuncValue::LessThanInt { value, .. } => PredicateFuncValue::GreaterThanInt {
PredicateFuncValue::GreaterThan { value, .. } => PredicateFuncValue::GreaterThan {
space0: one_whitespace(),
value: value.clone(),
value: value.lint(),
operator: true,
},
PredicateFuncValue::LessThanFloat { value, .. } => {
PredicateFuncValue::GreaterThanFloat {
PredicateFuncValue::GreaterThanOrEqual { value, .. } => {
PredicateFuncValue::GreaterThanOrEqual {
space0: one_whitespace(),
value: value.clone(),
value: value.lint(),
operator: true,
}
}
PredicateFuncValue::LessThanOrEqualInt { value, .. } => {
PredicateFuncValue::GreaterThanOrEqualInt {
PredicateFuncValue::LessThan { value, .. } => PredicateFuncValue::GreaterThan {
space0: one_whitespace(),
value: value.lint(),
operator: true,
},
PredicateFuncValue::LessThanOrEqual { value, .. } => {
PredicateFuncValue::GreaterThanOrEqual {
space0: one_whitespace(),
value: value.clone(),
operator: true,
}
}
PredicateFuncValue::LessThanOrEqualFloat { value, .. } => {
PredicateFuncValue::GreaterThanOrEqualFloat {
space0: one_whitespace(),
value: value.clone(),
value: value.lint(),
operator: true,
}
}
@ -551,31 +457,10 @@ impl Lintable<PredicateFuncValue> for PredicateFuncValue {
value: value.clone().lint(),
},
PredicateFuncValue::IncludeString { value, .. } => PredicateFuncValue::IncludeString {
PredicateFuncValue::Include { value, .. } => PredicateFuncValue::Include {
space0: one_whitespace(),
value: value.clone().lint(),
value: value.lint(),
},
PredicateFuncValue::IncludeInt { value, .. } => PredicateFuncValue::IncludeInt {
space0: one_whitespace(),
value: value.clone(),
},
PredicateFuncValue::IncludeFloat { value, .. } => PredicateFuncValue::IncludeFloat {
space0: one_whitespace(),
value: value.clone(),
},
PredicateFuncValue::IncludeBool { value, .. } => PredicateFuncValue::IncludeBool {
space0: one_whitespace(),
value: value.clone(),
},
PredicateFuncValue::IncludeNull { .. } => PredicateFuncValue::IncludeNull {
space0: one_whitespace(),
},
PredicateFuncValue::IncludeExpression { value, .. } => {
PredicateFuncValue::IncludeExpression {
space0: one_whitespace(),
value: value.clone(),
}
}
PredicateFuncValue::Match { value, .. } => PredicateFuncValue::Match {
space0: one_whitespace(),
@ -599,6 +484,25 @@ impl Lintable<PredicateFuncValue> for PredicateFuncValue {
}
}
impl Lintable<PredicateValue> for PredicateValue {
fn errors(&self) -> Vec<Error> {
let errors = vec![];
errors
}
fn lint(&self) -> PredicateValue {
match self {
PredicateValue::String(value) => PredicateValue::String(value.lint()),
PredicateValue::Integer(value) => PredicateValue::Integer(*value),
PredicateValue::Float(value) => PredicateValue::Float(value.clone()),
PredicateValue::Bool(value) => PredicateValue::Bool(*value),
PredicateValue::Null {} => PredicateValue::Null {},
PredicateValue::Hex(value) => PredicateValue::Hex(value.lint()),
PredicateValue::Expression(value) => PredicateValue::Expression(value.clone()),
}
}
}
impl Lintable<Cookie> for Cookie {
fn errors(&self) -> Vec<Error> {
let errors = vec![];