Struct hocon::HoconLoader
source · [−]pub struct HoconLoader { /* private fields */ }
Expand description
Helper to load an HOCON file. This is used to set up the HOCON loader’s option, like strict mode, disabling system environment, and to buffer several documents.
Strict mode
If strict mode is enabled with strict()
,
loading a document will return the first error encountered. Otherwise, most errors
will be wrapped in a Hocon::BadValue
.
Usage
let mut loader = HoconLoader::new() // Creating new loader with default configuration
.no_system() // Disable substituting from system environment
.no_url_include(); // Disable including files from URLs
let default_values = r#"{ a = 7 }"#;
loader = loader.load_str(default_values)? // Load values from a string
.load_file("tests/data/basic.conf")? // Load first file
.load_file("tests/data/test01.conf")?; // Load another file
let hocon = loader.hocon()?; // Create the Hocon document from the loaded sources
Implementations
sourceimpl HoconLoader
impl HoconLoader
sourcepub fn no_system(&self) -> Self
pub fn no_system(&self) -> Self
Disable System environment substitutions
Example HOCON document
"system" : {
"home" : ${HOME},
"pwd" : ${PWD},
"shell" : ${SHELL},
"lang" : ${LANG},
}
with system:
assert_eq!(
HoconLoader::new().load_str(example)?.hocon()?["system"]["shell"],
Hocon::String(String::from("/bin/bash"))
);
without system:
assert_eq!(
HoconLoader::new().no_system().load_str(example)?.hocon()?["system"]["shell"],
Hocon::BadValue(Error::KeyNotFound { key: String::from("SHELL") })
);
sourcepub fn no_url_include(&self) -> Self
pub fn no_url_include(&self) -> Self
Disable loading included files from external urls.
Example HOCON document
include url("https://raw.githubusercontent.com/mockersf/hocon.rs/master/tests/data/basic.conf")
with url include:
assert_eq!(
HoconLoader::new().load_file("tests/data/include_url.conf")?.hocon()?["d"],
Hocon::Boolean(true)
);
without url include:
assert_eq!(
HoconLoader::new().no_url_include().load_file("tests/data/include_url.conf")?.hocon()?["d"],
Hocon::BadValue(Error::MissingKey)
);
Feature
This method depends on feature url-support
sourcepub fn strict(&self) -> Self
pub fn strict(&self) -> Self
Sets the HOCON loader to return the first Error
encoutered instead
of wrapping it in a Hocon::BadValue
and
continuing parsing
Example HOCON document
{
a = ${b}
}
in permissive mode:
assert_eq!(
HoconLoader::new().load_str(example)?.hocon()?["a"],
Hocon::BadValue(Error::KeyNotFound { key: String::from("b") })
);
in strict mode:
assert_eq!(
HoconLoader::new().strict().load_str(example)?.hocon(),
Err(Error::KeyNotFound { key: String::from("b") })
);
sourcepub fn max_include_depth(&self, new_max_depth: u8) -> Self
pub fn max_include_depth(&self, new_max_depth: u8) -> Self
Set a new maximum include depth, by default 10
sourcepub fn load_str(self, s: &str) -> Result<Self, Error>
pub fn load_str(self, s: &str) -> Result<Self, Error>
Load a string containing an Hocon
document. Includes are not supported when
loading from a string
Errors
Error::Parse
if the document is invalid
Additional errors in strict mode
Error::IncludeNotAllowedFromStr
if there is an include in the string
sourcepub fn load_file<P: AsRef<Path>>(&self, path: P) -> Result<Self, Error>
pub fn load_file<P: AsRef<Path>>(&self, path: P) -> Result<Self, Error>
Load the HOCON configuration file containing an Hocon
document
Errors
Error::File
if there was an error reading the file contentError::Parse
if the document is invalid
Additional errors in strict mode
Error::TooManyIncludes
if there are too many included files within included files. The limit can be changed withmax_include_depth
sourcepub fn hocon(self) -> Result<Hocon, Error>
pub fn hocon(self) -> Result<Hocon, Error>
Load the documents as HOCON
Errors in strict mode
Error::Include
if there was an issue with an included fileError::KeyNotFound
if there is a substitution with a key that is not present in the documentError::DisabledExternalUrl
if crate was built without featureurl-support
and aninclude url("...")
was found
sourcepub fn resolve<'de, T>(self) -> Result<T, Error> where
T: Deserialize<'de>,
pub fn resolve<'de, T>(self) -> Result<T, Error> where
T: Deserialize<'de>,
Deserialize the loaded documents to the target type
Errors
Error::Deserialization
if there was a serde error during deserialization (missing required field, type issue, …)
Additional errors in strict mode
Error::Include
if there was an issue with an included fileError::KeyNotFound
if there is a substitution with a key that is not present in the documentError::DisabledExternalUrl
if crate was built without featureurl-support
and aninclude url("...")
was found
Trait Implementations
sourceimpl Clone for HoconLoader
impl Clone for HoconLoader
sourcefn clone(&self) -> HoconLoader
fn clone(&self) -> HoconLoader
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for HoconLoader
impl Debug for HoconLoader
Auto Trait Implementations
impl RefUnwindSafe for HoconLoader
impl Send for HoconLoader
impl Sync for HoconLoader
impl Unpin for HoconLoader
impl UnwindSafe for HoconLoader
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more