@@ -96,29 +96,56 @@ impl Display for ParseError<'_> {
9696 }
9797}
9898
99+ /// Parser settings.
100+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
101+ pub struct ParserSettings {
102+ /// Approximate size of the cache used by the DFA of a regex.
103+ /// Default: 10MB
104+ pub regex_dfa_size_limit : usize ,
105+ /// Approximate size limit of the compiled regular expression.
106+ /// Default: 2MB
107+ pub regex_compiled_size_limit : usize ,
108+ /// Maximum number of star metacharacters allowed in a wildcard.
109+ /// Default: unlimited
110+ pub wildcard_star_limit : usize ,
111+ }
112+
113+ impl Default for ParserSettings {
114+ #[ inline]
115+ fn default ( ) -> Self {
116+ Self {
117+ // Default value extracted from the regex crate.
118+ regex_compiled_size_limit : 10 * ( 1 << 20 ) ,
119+ // Default value extracted from the regex crate.
120+ regex_dfa_size_limit : 2 * ( 1 << 20 ) ,
121+ wildcard_star_limit : usize:: MAX ,
122+ }
123+ }
124+ }
125+
99126/// A structure used to drive parsing of an expression into a [`FilterAst`].
100127#[ derive( Clone , Debug , PartialEq , Eq ) ]
101128pub struct FilterParser < ' s > {
102129 pub ( crate ) scheme : & ' s Scheme ,
103- pub ( crate ) regex_dfa_size_limit : usize ,
104- pub ( crate ) regex_compiled_size_limit : usize ,
105- pub ( crate ) wildcard_star_limit : usize ,
130+ pub ( crate ) settings : ParserSettings ,
106131}
107132
108133impl < ' s > FilterParser < ' s > {
109- /// Creates a new parser with default configuration .
134+ /// Creates a new parser with default settings .
110135 #[ inline]
111136 pub fn new ( scheme : & ' s Scheme ) -> Self {
112137 Self {
113138 scheme,
114- // Default value extracted from the regex crate.
115- regex_compiled_size_limit : 10 * ( 1 << 20 ) ,
116- // Default value extracted from the regex crate.
117- regex_dfa_size_limit : 2 * ( 1 << 20 ) ,
118- wildcard_star_limit : usize:: MAX ,
139+ settings : ParserSettings :: default ( ) ,
119140 }
120141 }
121142
143+ /// Creates a new parser with the specified settings.
144+ #[ inline]
145+ pub fn with_settings ( scheme : & ' s Scheme , settings : ParserSettings ) -> Self {
146+ Self { scheme, settings }
147+ }
148+
122149 /// Returns the [`Scheme`](struct@Scheme) for which this parser has been constructor for.
123150 #[ inline]
124151 pub fn scheme ( & self ) -> & ' s Scheme {
@@ -143,39 +170,45 @@ impl<'s> FilterParser<'s> {
143170 complete ( self . lex_as ( input. trim ( ) ) ) . map_err ( |err| ParseError :: new ( input, err) )
144171 }
145172
173+ /// Retrieve parser settings.
174+ #[ inline]
175+ pub fn settings ( & self ) -> & ParserSettings {
176+ & self . settings
177+ }
178+
146179 /// Set the approximate size limit of the compiled regular expression.
147180 #[ inline]
148181 pub fn regex_set_compiled_size_limit ( & mut self , regex_compiled_size_limit : usize ) {
149- self . regex_compiled_size_limit = regex_compiled_size_limit;
182+ self . settings . regex_compiled_size_limit = regex_compiled_size_limit;
150183 }
151184
152185 /// Get the approximate size limit of the compiled regular expression.
153186 #[ inline]
154187 pub fn regex_get_compiled_size_limit ( & self ) -> usize {
155- self . regex_compiled_size_limit
188+ self . settings . regex_compiled_size_limit
156189 }
157190
158191 /// Set the approximate size of the cache used by the DFA of a regex.
159192 #[ inline]
160193 pub fn regex_set_dfa_size_limit ( & mut self , regex_dfa_size_limit : usize ) {
161- self . regex_dfa_size_limit = regex_dfa_size_limit;
194+ self . settings . regex_dfa_size_limit = regex_dfa_size_limit;
162195 }
163196
164197 /// Get the approximate size of the cache used by the DFA of a regex.
165198 #[ inline]
166199 pub fn regex_get_dfa_size_limit ( & self ) -> usize {
167- self . regex_dfa_size_limit
200+ self . settings . regex_dfa_size_limit
168201 }
169202
170203 /// Set the maximum number of star metacharacters allowed in a wildcard.
171204 #[ inline]
172205 pub fn wildcard_set_star_limit ( & mut self , wildcard_star_limit : usize ) {
173- self . wildcard_star_limit = wildcard_star_limit;
206+ self . settings . wildcard_star_limit = wildcard_star_limit;
174207 }
175208
176209 /// Get the maximum number of star metacharacters allowed in a wildcard.
177210 #[ inline]
178211 pub fn wildcard_get_star_limit ( & self ) -> usize {
179- self . wildcard_star_limit
212+ self . settings . wildcard_star_limit
180213 }
181214}
0 commit comments