tanzim_parse/closure.rs
1//! Custom parser backed by a closure.
2//!
3//! Use this when a format isn't built-in and you don't want to define a whole type just to
4//! implement [`Parse`]. Wrap a closure of the same shape as
5//! [`Parse::parse`] — see [`BoxedParseFn`] — and the resulting
6//! [`Closure`] *is* a `Parse`, so it plugs straight into the pipeline. Optionally attach a
7//! [`BoxedValidatorFn`] with [`Closure::with_validator`] to take part in format auto-detection.
8//!
9//! For anything with non-trivial state, prefer a real `impl Parse`. Reach for `Closure` for
10//! small, stateless, or one-off parsers.
11//!
12//! # Example
13//!
14//! ```
15//! use tanzim_parse::{closure::Closure, Parse};
16//! use tanzim_source::SourceBuilder;
17//! use tanzim_value::{LocatedValue, Location, Value};
18//!
19//! let parser = Closure::new(
20//! "upper",
21//! "txt",
22//! Box::new(|source, bytes| {
23//! Ok(LocatedValue {
24//! value: Value::String(String::from_utf8_lossy(bytes).to_uppercase()),
25//! location: Location::in_source(source.clone(), None, None, None),
26//! })
27//! }),
28//! );
29//! let source = SourceBuilder::new()
30//! .with_source("file")
31//! .with_resource("test.txt")
32//! .build()
33//! .unwrap();
34//! let value = parser.parse(&source, b"hello").unwrap();
35//! assert_eq!(value.value.as_string().unwrap(), "HELLO");
36//! ```
37
38use crate::{Parse, Source};
39use tanzim_value::{Error, LocatedValue};
40
41/// The parse closure driving a [`Closure`] parser — same contract as
42/// [`Parse::parse`].
43///
44/// Called with the [`Source`] declaration and the raw `&[u8]` bytes. Return a [`LocatedValue`]
45/// tree (ideally with a [`Location`](tanzim_value::Location) on every node), or an [`Error`] on
46/// failure.
47pub type BoxedParseFn = Box<dyn Fn(&Source, &[u8]) -> Result<LocatedValue, Error>>;
48
49/// The optional auto-detection probe for a [`Closure`] parser — same contract as
50/// [`Parse::is_format_supported`].
51///
52/// Given the raw bytes, return `Some(true)` if confident, `Some(false)` if definitely not this
53/// format, or `None` to abstain. The default (when none is set) abstains with `None`.
54pub type BoxedValidatorFn = Box<dyn Fn(&[u8]) -> Option<bool>>;
55
56/// A [`Parse`] implementation whose behaviour is supplied by closures.
57///
58/// Reach for this instead of a full `impl Parse` when the parser is small, stateless, or a
59/// one-off adapter. See the [module docs](self) for a complete example.
60pub struct Closure {
61 name: String,
62 parser: BoxedParseFn,
63 validator: BoxedValidatorFn,
64 supported_format_list: Vec<String>,
65}
66
67impl Closure {
68 /// Build a closure-backed parser.
69 ///
70 /// - `name` — the parser [`name`](crate::Parse::name) used in error messages.
71 /// - `supported_format` — the single format extension this parser handles (widen later with
72 /// [`Closure::with_format_list`]).
73 /// - `parser` — the closure run by [`parse`](crate::Parse::parse).
74 ///
75 /// The auto-detection probe defaults to abstaining (`None`); set one with
76 /// [`Closure::with_validator`].
77 pub fn new<N: AsRef<str>, F: AsRef<str>>(
78 name: N,
79 supported_format: F,
80 parser: BoxedParseFn,
81 ) -> Self {
82 Self {
83 name: name.as_ref().to_string(),
84 parser,
85 validator: Box::new(|_| None),
86 supported_format_list: vec![supported_format.as_ref().to_string()],
87 }
88 }
89
90 /// Attach an auto-detection probe (see [`BoxedValidatorFn`]) used when a payload has no format
91 /// hint.
92 pub fn with_validator(mut self, validator: BoxedValidatorFn) -> Self {
93 self.validator = validator;
94 self
95 }
96
97 /// Replace the list of format extensions this parser handles (e.g. `["yml", "yaml"]`).
98 pub fn with_format_list<N: AsRef<str>>(mut self, format_list: &[N]) -> Self {
99 let mut formats = Vec::new();
100 for format in format_list {
101 formats.push(format.as_ref().to_string());
102 }
103 self.supported_format_list = formats;
104 self
105 }
106}
107
108impl Parse for Closure {
109 fn name(&self) -> &str {
110 self.name.as_str()
111 }
112
113 fn supported_format_list(&self) -> Vec<String> {
114 self.supported_format_list.clone()
115 }
116
117 fn parse(&self, source: &Source, bytes: &[u8]) -> Result<LocatedValue, Error> {
118 (self.parser)(source, bytes)
119 }
120
121 fn is_format_supported(&self, bytes: &[u8]) -> Option<bool> {
122 (self.validator)(bytes)
123 }
124}
125
126#[cfg(test)]
127mod tests {
128 use super::*;
129 use tanzim_source::SourceBuilder;
130 use tanzim_value::{Location, Value};
131
132 #[test]
133 fn closure_parser_delegates_to_function() {
134 let parser = Closure::new(
135 "upper",
136 "txt",
137 Box::new(|source, bytes| {
138 Ok(LocatedValue {
139 value: Value::String(String::from_utf8_lossy(bytes).to_uppercase()),
140 location: Location::in_source(source.clone(), None, None, None),
141 })
142 }),
143 )
144 .with_validator(Box::new(|bytes| Some(!bytes.is_empty())));
145 let source = SourceBuilder::new()
146 .with_source("file")
147 .with_resource("test.txt")
148 .build()
149 .unwrap();
150 let parsed = parser.parse(&source, b"hello").unwrap();
151 assert_eq!(parsed.value.as_string().unwrap(), "HELLO");
152 assert_eq!(parser.is_format_supported(b"x"), Some(true));
153 assert_eq!(parser.is_format_supported(b""), Some(false));
154 }
155
156 #[test]
157 fn closure_parser_with_format_list() {
158 let parser = Closure::new(
159 "yaml",
160 "yml",
161 Box::new(|source, bytes| {
162 Ok(LocatedValue {
163 value: Value::String(String::from_utf8_lossy(bytes).to_string()),
164 location: Location::at(source.source(), source.resource(), None, None, None),
165 })
166 }),
167 )
168 .with_format_list(&["yml", "yaml"]);
169 assert_eq!(
170 parser.supported_format_list(),
171 vec!["yml".to_string(), "yaml".to_string()]
172 );
173 }
174}