Skip to main content

ParserBuilder

Struct ParserBuilder 

Source
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

Source

pub fn new() -> Self

A builder with the settings of Parser::new.

Source

pub fn with_flags(self, flags: ParserFlags) -> Self

Replaces the parser flags (Parser::set_flags).

Source

pub fn with_priority(self, priority: u8) -> Self

Sets the priority of the document’s values (Parser::set_priority, spec §8.3).

Source

pub fn with_strategy(self, strategy: DuplicateStrategy) -> Self

Sets how repeated keys are resolved (Parser::set_strategy, spec §8.4).

Source

pub fn with_variable( self, name: impl Into<String>, value: impl Into<String>, ) -> Self

Registers a variable (Parser::register_variable, spec §7.1).

Source

pub fn with_variables<I, K, V>(self, variables: I) -> Self
where I: IntoIterator<Item = (K, V)>, K: Into<String>, V: Into<String>,

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).

Source

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.

Source

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).

Source

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).

Source

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.

Source

pub fn with_base_dir(self, dir: impl Into<PathBuf>) -> Self

Sets the directory that relative paths resolve against (Parser::set_base_dir).

Source

pub fn with_search_path<I, S>(self, dirs: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

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).

Source

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.

Source

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.

Source

pub fn build(self) -> Parser

The parser.

Trait Implementations§

Source§

impl Debug for ParserBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ParserBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.