summaryrefslogtreecommitdiffstats
path: root/libimagentryfilter
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2016-02-03 16:18:38 +0100
committerMatthias Beyer <mail@beyermatthias.de>2016-02-03 16:39:05 +0100
commit12443f631bf3ccc6ccb09d60ec59e837cee62400 (patch)
tree41eb862183b1d3d38f6f8ab33128d8d6229c126a /libimagentryfilter
parent34e62aaade1a34ca273e8190c6f5fbaff8591346 (diff)
Add builtin header check: Field is type
Diffstat (limited to 'libimagentryfilter')
-rw-r--r--libimagentryfilter/src/builtin/header/field_istype.rs62
-rw-r--r--libimagentryfilter/src/builtin/header/mod.rs1
2 files changed, 63 insertions, 0 deletions
diff --git a/libimagentryfilter/src/builtin/header/field_istype.rs b/libimagentryfilter/src/builtin/header/field_istype.rs
new file mode 100644
index 00000000..23191611
--- /dev/null
+++ b/libimagentryfilter/src/builtin/header/field_istype.rs
@@ -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)
+ }
+
+}
+
+
diff --git a/libimagentryfilter/src/builtin/header/mod.rs b/libimagentryfilter/src/builtin/header/mod.rs
index c42547df..1dd2cb69 100644
--- a/libimagentryfilter/src/builtin/header/mod.rs
+++ b/libimagentryfilter/src/builtin/header/mod.rs
@@ -1,4 +1,5 @@
pub mod field_eq;
pub mod field_grep;
+pub mod field_istype;
pub mod field_path;