Enum Hocon

Source
pub enum Hocon {
    Real(f64),
    Integer(i64),
    String(String),
    Boolean(bool),
    Array(Vec<Hocon>),
    Hash(LinkedHashMap<String, Hocon>),
    Null,
    BadValue(Error),
}
Expand description

An HOCON document

Values can be retrieved as a basic type, with basic cast between some of the value types: Automatic type conversions. If the value if not of the expected type, a None will be returned.

If the value is a Hocon::Hash, its values can be accessed by indexing with a str, as in hash[key]. If the value is an Hocon::Array, its values can be accessed by indexing with a usize. An Hocon::Hash whose keys can be converted to numeric values ("0", "1", …) can be indexed with a usize following the rules described in Conversion of numerically-indexed objects to arrays.

Indexing a Hocon value with a wrong key type, or a type of value that can’t be indexed will return a Hocon::BadValue with an error of type crate::Error::InvalidKey.

Indexing a Hocon value with a key that is not present will return a Hocon::BadValue with an error of type crate::Error::MissingKey.

Values can also be accessed as a Duration or a size following the rules described in Units format.

§Usage

// Accessing a value of the expected type
assert_eq!(
    HoconLoader::new().load_str(r#"{ a: 7 }"#)?.hocon()?["a"].as_i64(),
    Some(7)
);

// Accessing a value with automatic conversion
assert_eq!(
    HoconLoader::new().load_str(r#"{ a: off }"#)?.hocon()?["a"].as_bool(),
    Some(false)
);

// Accessing an Array
assert_eq!(
    HoconLoader::new().load_str(r#"{ a: [ first, second ] }"#)?.hocon()?["a"][0].as_string(),
    Some(String::from("first"))
);

// Accessing an Hash with a missing key
assert_eq!(
    HoconLoader::new().load_str(r#"{ a: 7 }"#)?.hocon()?["b"],
    Hocon::BadValue(Error::MissingKey)
);

// Accessing an Hash as if it was an Array
assert_eq!(
    HoconLoader::new().load_str(r#"{ a: 7 }"#)?.hocon()?[0],
    Hocon::BadValue(Error::InvalidKey)
);

Variants§

§

Real(f64)

A floating value

§

Integer(i64)

An integer value

§

String(String)

A string

§

Boolean(bool)

A boolean

§

Array(Vec<Hocon>)

An array of Hocon values

§

Hash(LinkedHashMap<String, Hocon>)

An HashMap of Hocon values with keys

§

Null

A null value

§

BadValue(Error)

A BadValue, marking an error in parsing or a missing value

Implementations§

Source§

impl Hocon

Source

pub fn as_f64(&self) -> Option<f64>

Try to cast a value as a f64 value

Source

pub fn as_i64(&self) -> Option<i64>

Try to cast a value as a i64 value

Source

pub fn as_string(&self) -> Option<String>

Try to cast a value as a String value

Source

pub fn as_bool(&self) -> Option<bool>

Try to cast a value as a bool value

Source§

impl Hocon

Source

pub fn as_bytes(&self) -> Option<f64>

Try to return a value as a size in bytes according to size in bytes format.

Bare numbers are taken to be in bytes already, while strings are parsed as a number plus an optional unit string.

§Example
assert_eq!(
    HoconLoader::new().load_str(r#"{ size = 1.5KiB }"#)?.hocon()?["size"].as_bytes(),
    Some(1536.0)
);
Source

pub fn as_milliseconds(&self) -> Option<f64>

Try to return a value as a duration in milliseconds according to duration format.

Bare numbers are taken to be in bytes already, while strings are parsed as a number plus an optional unit string.

§Example
assert_eq!(
    HoconLoader::new().load_str(r#"{ duration = 1.5 hour  }"#)?
        .hocon()?["duration"].as_milliseconds(),
    Some(5400000.0)
);
Source

pub fn as_nanoseconds(&self) -> Option<f64>

Try to return a value as a duration in nanoseconds according to duration format.

Bare numbers are taken to be in bytes already, while strings are parsed as a number plus an optional unit string.

§Example
assert_eq!(
    HoconLoader::new().load_str(r#"{ duration = 1.5 hour  }"#)?
        .hocon()?["duration"].as_nanoseconds(),
    Some(5400000000000.0)
);
Source

pub fn as_microseconds(&self) -> Option<f64>

Try to return a value as a duration in microseconds according to duration format.

Bare numbers are taken to be in bytes already, while strings are parsed as a number plus an optional unit string.

§Example
assert_eq!(
    HoconLoader::new().load_str(r#"{ duration = 1.5 hour  }"#)?
        .hocon()?["duration"].as_microseconds(),
    Some(5400000000.0)
);
Source

pub fn as_seconds(&self) -> Option<f64>

Try to return a value as a duration in seconds according to duration format.

Bare numbers are taken to be in bytes already, while strings are parsed as a number plus an optional unit string.

§Example
assert_eq!(
    HoconLoader::new().load_str(r#"{ duration = 1.5 hour  }"#)?
        .hocon()?["duration"].as_seconds(),
    Some(5400.0)
);
Source

pub fn as_minutes(&self) -> Option<f64>

Try to return a value as a duration in minutes according to duration format.

Bare numbers are taken to be in bytes already, while strings are parsed as a number plus an optional unit string.

§Example
assert_eq!(
    HoconLoader::new().load_str(r#"{ duration = 1.5 hour  }"#)?
        .hocon()?["duration"].as_minutes(),
    Some(90.0)
);
Source

pub fn as_hours(&self) -> Option<f64>

Try to return a value as a duration in hours according to duration format.

Bare numbers are taken to be in bytes already, while strings are parsed as a number plus an optional unit string.

§Example
assert_eq!(
    HoconLoader::new().load_str(r#"{ duration = 1.5 hour  }"#)?
        .hocon()?["duration"].as_hours(),
    Some(1.5)
);
Source

pub fn as_days(&self) -> Option<f64>

Try to return a value as a duration in days according to duration format.

Bare numbers are taken to be in bytes already, while strings are parsed as a number plus an optional unit string.

§Example
assert_eq!(
    HoconLoader::new().load_str(r#"{ duration = 1.5 hour  }"#)?
        .hocon()?["duration"].as_days(),
    Some(0.0625)
);
Source

pub fn as_weeks(&self) -> Option<f64>

Try to return a value as a duration in weeks according to duration format.

Bare numbers are taken to be in bytes already, while strings are parsed as a number plus an optional unit string.

§Example
assert_eq!(
    HoconLoader::new().load_str(r#"{ duration = 1.5 days  }"#)?
        .hocon()?["duration"].as_weeks(),
    Some(0.21428571428571427)
);
Source

pub fn as_months(&self) -> Option<f64>

Try to return a value as a duration in months according to duration format.

Bare numbers are taken to be in bytes already, while strings are parsed as a number plus an optional unit string.

§Example
assert_eq!(
    HoconLoader::new().load_str(r#"{ duration = 1.5 days  }"#)?
        .hocon()?["duration"].as_months(),
    Some(0.05)
);
Source

pub fn as_years(&self) -> Option<f64>

Try to return a value as a duration in years according to duration format.

Bare numbers are taken to be in bytes already, while strings are parsed as a number plus an optional unit string.

§Example
assert_eq!(
    HoconLoader::new().load_str(r#"{ duration = 1.5 days  }"#)?
        .hocon()?["duration"].as_years(),
    Some(0.00410958904109589)
);
Source

pub fn as_duration(&self) -> Option<Duration>

Try to return a value as a duration according to duration format.

Bare numbers are taken to be in bytes already, while strings are parsed as a number plus an optional unit string.

§Example
assert_eq!(
    HoconLoader::new().load_str(r#"{ duration = 1.5 hours  }"#)?
        .hocon()?["duration"].as_duration(),
    Some(std::time::Duration::from_secs(5400))
);
Source§

impl Hocon

Source

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

Trait Implementations§

Source§

impl Clone for Hocon

Source§

fn clone(&self) -> Hocon

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Hocon

Source§

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

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

impl<'a> Index<&'a str> for Hocon

Source§

type Output = Hocon

The returned type after indexing.
Source§

fn index(&self, idx: &'a str) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl Index<usize> for Hocon

Source§

type Output = Hocon

The returned type after indexing.
Source§

fn index(&self, idx: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl PartialEq for Hocon

Source§

fn eq(&self, other: &Hocon) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Hocon

Auto Trait Implementations§

§

impl Freeze for Hocon

§

impl RefUnwindSafe for Hocon

§

impl Send for Hocon

§

impl Sync for Hocon

§

impl Unpin for Hocon

§

impl UnwindSafe for Hocon

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more