pub struct ParserBuilder { /* private fields */ }Expand description
Builds a Parser from settings given by value, one method call each.
Every method sets what the Parser setter of the same name sets; ParserBuilder::build
returns the parser. A builder starts from Parser::new: no flags, priority 0, the append
strategy, no registered variables, no variable handler, no base directory, no input limit,
the .inherit depth limit 1024, and a loader that holds no files. ParserBuilder::with_loader opts into file access, for example with
FsLoader for the filesystem.
use serde::Deserialize;
use serde_ucl::parse::{MemoryLoader, ParserBuilder};
use serde_ucl::{ParserFlags, from_value};
#[derive(Deserialize)]
struct Config {
url: String,
home: String,
port: u16,
}
let mut files = MemoryLoader::new();
files.add_file("/etc/app/port.conf", "port = 8443\n");
let mut parser = ParserBuilder::new()
.with_flags(ParserFlags::KEY_LOWERCASE)
.with_variable("HOST", "example.org")
.with_variable_handler(|name| (name == "HOME").then(|| "/home/app".to_string()))
.with_loader(files)
.with_base_dir("/etc/app")
.build();
let input = b"URL = \"https://$HOST/\"\nhome = \"${HOME}\"\n.include \"port.conf\"";
let value = parser.parse(input)?;
let config: Config = from_value(value)?;
assert_eq!(config.url, "https://example.org/");
assert_eq!(config.home, "/home/app");
assert_eq!(config.port, 8443);Implementations§
Source§impl ParserBuilder
impl ParserBuilder
Sourcepub fn new() -> Self
pub fn new() -> Self
A builder with the settings of Parser::new.
Sourcepub fn with_flags(self, flags: ParserFlags) -> Self
pub fn with_flags(self, flags: ParserFlags) -> Self
Replaces the parser flags (Parser::set_flags).
Sourcepub fn with_priority(self, priority: u8) -> Self
pub fn with_priority(self, priority: u8) -> Self
Sets the priority of the document’s values (Parser::set_priority, spec §8.3).
Sourcepub fn with_strategy(self, strategy: DuplicateStrategy) -> Self
pub fn with_strategy(self, strategy: DuplicateStrategy) -> Self
Sets how repeated keys are resolved (Parser::set_strategy, spec §8.4).
Sourcepub fn with_variable(
self,
name: impl Into<String>,
value: impl Into<String>,
) -> Self
pub fn with_variable( self, name: impl Into<String>, value: impl Into<String>, ) -> Self
Registers a variable (Parser::register_variable, spec §7.1).
Sourcepub fn with_variables<I, K, V>(self, variables: I) -> Self
pub fn with_variables<I, K, V>(self, variables: I) -> Self
Registers each variable of variables, in iteration order (Parser::register_variable).
The order decides between names that are prefixes of one another in unbraced references
(spec §7.4).
Sourcepub fn with_variable_handler(
self,
handler: impl FnMut(&str) -> Option<String> + 'static,
) -> Self
pub fn with_variable_handler( self, handler: impl FnMut(&str) -> Option<String> + 'static, ) -> Self
Installs a handler for braced references ${NAME} to names that are not registered
(Parser::set_variable_handler, spec §7.7). It is never asked for unbraced references.
Sourcepub fn with_macro(
self,
name: impl Into<String>,
handler: impl Fn(&mut MacroCall<'_>) -> Result<(), MacroError> + 'static,
) -> Self
pub fn with_macro( self, name: impl Into<String>, handler: impl Fn(&mut MacroCall<'_>) -> Result<(), MacroError> + 'static, ) -> Self
Registers a macro under name (Parser::register_macro, spec §13.2).
Sourcepub fn with_context_macro(
self,
name: impl Into<String>,
handler: impl Fn(&mut MacroCall<'_>) -> Result<(), MacroError> + 'static,
) -> Self
pub fn with_context_macro( self, name: impl Into<String>, handler: impl Fn(&mut MacroCall<'_>) -> Result<(), MacroError> + 'static, ) -> Self
Registers a context macro under name, whose handler also gets the root as built so far
(Parser::register_context_macro, spec §13.2).
Sourcepub fn with_loader(self, loader: impl Loader + 'static) -> Self
pub fn with_loader(self, loader: impl Loader + 'static) -> Self
Sets where Parser::parse_file, the include macros and .load read files from
(Parser::set_loader). The default loader holds no files.
Sourcepub fn with_base_dir(self, dir: impl Into<PathBuf>) -> Self
pub fn with_base_dir(self, dir: impl Into<PathBuf>) -> Self
Sets the directory that relative paths resolve against (Parser::set_base_dir).
Sourcepub fn with_search_path<I, S>(self, dirs: I) -> Self
pub fn with_search_path<I, S>(self, dirs: I) -> Self
Sets the search directories of the include macros, in effect from the start of every
parse as a path list would be (Parser::set_search_path, spec §9.4).
Sourcepub fn with_max_input_bytes(self, limit: u64) -> Self
pub fn with_max_input_bytes(self, limit: u64) -> Self
Sets the most bytes one parse reads, the document and the files its macros read together
(Parser::set_max_input_bytes). The default is no limit.
Sourcepub fn with_inherit_depth_limit(self, limit: usize) -> Self
pub fn with_inherit_depth_limit(self, limit: usize) -> Self
Sets how deep a copy made by .inherit may nest a value
(Parser::set_inherit_depth_limit). The default is 1024.
§Panics
If limit is greater than MAX_INHERIT_DEPTH_LIMIT.