Expand description
Custom parser backed by a closure.
Use this when a format isn’t built-in and you don’t want to define a whole type just to
implement Parse. Wrap a closure of the same shape as
Parse::parse — see BoxedParseFn — and the resulting
Closure is a Parse, so it plugs straight into the pipeline. Optionally attach a
BoxedValidatorFn with Closure::with_validator to take part in format auto-detection.
For anything with non-trivial state, prefer a real impl Parse. Reach for Closure for
small, stateless, or one-off parsers.
§Example
use tanzim_parse::{closure::Closure, Parse};
use tanzim_source::SourceBuilder;
use tanzim_value::{LocatedValue, Location, Value};
let parser = Closure::new(
"upper",
"txt",
Box::new(|source, bytes| {
Ok(LocatedValue::new(
Value::String(String::from_utf8_lossy(bytes).to_uppercase()),
Location::in_source(source.clone(), None, None, None),
))
}),
);
let source = SourceBuilder::new()
.with_source("file")
.with_resource("test.txt")
.build()
.unwrap();
let value = parser.parse(&source, b"hello").unwrap();
assert_eq!(value.value().as_string().unwrap(), "HELLO");Structs§
Type Aliases§
- Boxed
Parse Fn - The parse closure driving a
Closureparser — same contract asParse::parse. - Boxed
Validator Fn - The optional auto-detection probe for a
Closureparser — same contract asParse::is_format_supported.