1 unstable release
0.1.0 | Jun 10, 2019 |
---|
#4 in #automaat
Used in automaat-server
25KB
277 lines
Automaat Processor: String Regex
🚧 Work In Progress 🚧
lib.rs
:
An Automaat processor to match and replace strings using regex patterns.
This processor allows you to match against strings in an Automaat workflow, and either show an error if the pattern doesn't match, or replace the pattern with a replacement string.
It is great for transforming the output of the previous processor into
something that is more readable for the user, before printing it to the
screen using the PrintOutput
processor.
Examples
Replace input based on regex pattern
One common example of this processor is to use it after another processor ran, which provided some output that needs to be rewritten before it is used by the next processor (or presented to the user).
In this example, we get a string Failure #233 - email does not exist
. We
want to rewrite this output to show error: email does not exist
.
use automaat_core::{Context, Processor};
use automaat_processor_string_regex::StringRegex;
let context = Context::new()?;
let processor = StringRegex {
input: "Failure #233 - email does not exist".to_owned(),
regex: r"\A[^-]+ - (.*)\z".to_owned(),
mismatch_error: None,
replace: Some("error: $1".to_owned())
};
let output = processor.run(&context)?;
assert_eq!(output, Some("error: email does not exist".to_owned()));
Return error on regex mismatch
Another common use-case is to match against some input, and return an error if the pattern does not match.
In this case, we want the string to be a valid UUIDv4 format, and return an understandable error to the user if it does not match.
use automaat_core::{Context, Processor};
use automaat_processor_string_regex::StringRegex;
let context = Context::new()?;
let processor = StringRegex {
input: "This is not a valid UUID".to_owned(),
regex: r"\A([a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12})\z".to_owned(),
mismatch_error: Some("provided value is not in a valid UUIDv4 format".to_owned()),
replace: None
};
let error = processor.run(&context).unwrap_err();
assert_eq!(error.to_string(), "provided value is not in a valid UUIDv4 format".to_owned());
Package Features
juniper
– creates a set of objects to be used in GraphQL-based requests/responses.
Dependencies
~4–15MB
~209K SLoC