Expand description
Dissect is a library which is loosely based on logstash’s dissect. It extracts data from strings.
use dissect::{Pattern, Error};
use simd_json::{borrowed::Value, prelude::*};
let filter = Pattern::compile("%{a} %{b}")?;
let input = "John Doe";
let output = filter.run(input);
let mut expected = Value::object();
expected.try_insert("a", Value::from("John"));
expected.try_insert("b", Value::from("Doe"));
assert_eq!(output.as_ref(), expected.as_object());
§Categories
- Simple:
Named fields can be extracted using the syntax %{
use dissect::{Pattern, Error};
use simd_json::{borrowed::Value, prelude::*};
let output = Pattern::compile("%{name}, %{age}")?;
let output = output.run("John Doe, 22");
let mut expected = Value::object();
expected.try_insert("name", Value::from("John Doe"));
expected.try_insert("age", Value::from("22"));
assert_eq!(output.as_ref(), expected.as_object());
- Append (+)
The append operator will append the value to another value creating an array.
use dissect::{Pattern, Error};
use simd_json::{borrowed::Value, prelude::*};
let output = Pattern::compile( "%{+name} %{+name}, %{age}")?;
let output = output.run("John Doe, 22");
let mut expected = Value::object();
expected.try_insert("name", Value::from("John Doe"));
expected.try_insert("age", Value::from("22"));
assert_eq!(output.as_ref(), expected.as_object());
Append works only on strings and doesn’t support other types. Using append with any non-string type will result in an error.
- Named keys (&)
The named operator will return a key value pair of the field. It takes the key from the
previous matched field. Given the rule, %{?name}, %{&name}
and input "John Doe, 22"
,
the "%{?name}"
will match "John Doe"
but the ?
will prevent this from being stored
in the output.
The seperator ,
is skipped and %{&name}
matches "22"
. Since the &
is used, name
doesn’t become the key but the previous value found for name "John Doe"
even so isn’t stored
in the output, will become the key for "22"
.
use dissect::{Pattern, Error};
use simd_json::{borrowed::Value, prelude::*};
let output = Pattern::compile("%{?name}, %{&name}")?;
let output = output.run( "John Doe, 22");
let mut expected = Value::object();
expected.try_insert("John Doe", Value::from("22"));
assert_eq!(output.as_ref(), expected.as_object());
- Empty fields
Fields will return an empty value if no data is present in the input.
use dissect::{Pattern, Error};
use simd_json::{borrowed::Value, prelude::*};
let output = Pattern::compile("%{name}, %{age}")?;
let output = output.run(", 22");
let mut expected = Value::object();
expected.try_insert("name", Value::from(""));
expected.try_insert("age", Value::from("22"));
assert_eq!(output.as_ref(), expected.as_object());
- Skipped fields (?)
The operator will prevent the value from being stored in the output, effectively skipping it.
use dissect::{Pattern, Error};
use simd_json::{borrowed::Value, prelude::*};
let output = Pattern::compile("%{?first_name} %{last_name}, %{age}")?;
let output = output.run("John Doe, 22");
let mut expected = Value::object();
expected.try_insert("last_name", "Doe");
expected.try_insert("age", "22");
assert_eq!(output.as_ref(), expected.as_object());
- Types
We can convert the fields in the output to a different type by mentioning it in the field
definition. The types supported are: int, float, string. The type is specified with the
field : type
syntax.
use dissect::{Pattern, Error};
use simd_json::{borrowed::Value, prelude::*};
let output = Pattern::compile("%{name}, %{age:int}")?;
let output = output.run( "John Doe, 22");
let mut expected = Value::object();
expected.try_insert("name", "John Doe");
expected.try_insert("age",22);
assert_eq!(output.as_ref(), expected.as_object());
- Padding (_)
The operator will remove padding when storing the field in the output. You can specify the
skipped character as a parameter to _
. It will use
by default.
use dissect::{Pattern, Error};
use simd_json::{borrowed::Value, prelude::*};
let output = Pattern::compile("%{name}, %{_}%{age}")?;
let output = output.run("John Doe, 22");
let mut expected = Value::object();
expected.try_insert("name", "John Doe");
expected.try_insert("age", "22");
assert_eq!(output.as_ref(), expected.as_object());
use dissect::{Pattern, Error};
use simd_json::{borrowed::Value, prelude::*};
let output = Pattern::compile("%{name}, %{_(-)}%{age}")?;
let output = output.run("John Doe, -----------------------22");
let mut expected = Value::object();
expected.try_insert("name","John Doe");
expected.try_insert("age", "22");
assert_eq!(output.as_ref(), expected.as_object());