Add builtin header check: Field is type
This commit is contained in:
parent
34e62aaade
commit
12443f631b
2 changed files with 63 additions and 0 deletions
62
libimagentryfilter/src/builtin/header/field_istype.rs
Normal file
62
libimagentryfilter/src/builtin/header/field_istype.rs
Normal file
|
@ -0,0 +1,62 @@
|
|||
use libimagstore::store::Entry;
|
||||
|
||||
use builtin::header::field_path::FieldPath;
|
||||
use filter::Filter;
|
||||
|
||||
use toml::Value;
|
||||
|
||||
pub enum Type {
|
||||
Array,
|
||||
Boolean,
|
||||
Float,
|
||||
Integer,
|
||||
None,
|
||||
String,
|
||||
Table,
|
||||
}
|
||||
|
||||
impl Type {
|
||||
|
||||
fn matches(&self, v: &Value) -> bool {
|
||||
match (self, v) {
|
||||
(&Type::String, &Value::String(_)) => true,
|
||||
(&Type::Integer, &Value::Integer(_)) => true,
|
||||
(&Type::Float, &Value::Float(_)) => true,
|
||||
(&Type::Boolean, &Value::Boolean(_)) => true,
|
||||
(&Type::Array, &Value::Array(_)) => true,
|
||||
(&Type::Table, &Value::Table(_)) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub struct FieldIsType {
|
||||
header_field_path: FieldPath,
|
||||
expected_type: Type,
|
||||
}
|
||||
|
||||
impl FieldIsType {
|
||||
|
||||
pub fn new(path: FieldPath, expected_type: Type) -> FieldIsType {
|
||||
FieldIsType {
|
||||
header_field_path: path,
|
||||
expected_type: expected_type,
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl Filter for FieldIsType {
|
||||
|
||||
fn filter(&self, e: &Entry) -> bool {
|
||||
let header = e.get_header();
|
||||
self.header_field_path
|
||||
.walk(header)
|
||||
.map(|v| self.expected_type.matches(&v))
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
pub mod field_eq;
|
||||
pub mod field_grep;
|
||||
pub mod field_istype;
|
||||
pub mod field_path;
|
||||
|
||||
|
|
Loading…
Reference in a new issue