Struct nccl::config::Config

source ·
pub struct Config<'a> { /* private fields */ }
Expand description

A nccl configuration

Indexable with &str.

e.g.

// config.nccl:
// server
//     domain
//         example.com
//         www.example.com
//     port
//         80
//         443
//     root
//         /var/www/html

let content = std::fs::read_to_string("examples/config.nccl").unwrap();
let config = parse_config(&content).unwrap();

// get the value of a single node
assert_eq!(Some("/var/www/html"), config["server"]["root"].value());

// value always returns the value of the first child node
assert_eq!(Some("example.com"), config["server"]["domain"].value());

// get multiple values
assert_eq!(
    vec!["example.com", "www.example.com"],
    config["server"]["domain"].values().collect::<Vec<_>>()
);

// parse multiple values
assert_eq!(
    Ok(vec![80, 443]),
    config["server"]["port"]
        .values()
        .map(|value| value.parse::<u16>())
        .collect::<Result<Vec<_>, _>>()
);

Implementations§

source§

impl<'a> Config<'a>

source

pub fn quoted(&self) -> bool

source

pub fn quote_kind(&self) -> Option<QuoteKind>

source

pub fn has_value(&self, value: &str) -> bool

Check whether the config has the node.

source

pub fn children(&self) -> impl Iterator<Item = &Config<'a>>

Iterator for the children of a node.

source

pub fn child(&self) -> Option<&Config<'a>>

The first child of the node.

// excerpt of long.nccl:
// strings
//    in which case
//       "just\nuse quotes"
let source = std::fs::read_to_string("examples/long.nccl").unwrap();
let config = parse_config(&source).unwrap();
assert_eq!(
    "just\nuse quotes",
    config["strings"]["in which case:"]
        .child()
        .unwrap()
        .parse_quoted()
        .unwrap()
);
source

pub fn key(&self) -> &'a str

The key of the config node.

let source = "key\n value\n";
let config = nccl::parse_config(&source).unwrap();
assert_eq!(config["key"].key(), "key");
source

pub fn span(&self) -> Span

The location in the source of this node.

let source = "key\n value\n";
let config = nccl::parse_config(&source).unwrap();
assert_eq!(config["key"].span().line, 1);
assert_eq!(config["key"].child().unwrap().span().line, 2);
source

pub fn values(&self) -> impl Iterator<Item = &str>

Iterator for the child values of a node.

source

pub fn value(&self) -> Option<&'a str>

The first child value of a node.

source

pub fn parse_quoted(&self) -> Result<String, NcclError>

Parse the string including escape sequences if it’s quoted.

Operates on the first child of the node. See Config::child.

Trait Implementations§

source§

impl<'a> Clone for Config<'a>

source§

fn clone(&self) -> Config<'a>

Returns a copy 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<'a> Debug for Config<'a>

source§

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

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

impl Hash for Config<'_>

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

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

§

type Output = Config<'a>

The returned type after indexing.
source§

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

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

impl PartialEq for Config<'_>

source§

fn eq(&self, rhs: &Config<'_>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl ToString for Config<'_>

source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<'a> Eq for Config<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Config<'a>

§

impl<'a> RefUnwindSafe for Config<'a>

§

impl<'a> Send for Config<'a>

§

impl<'a> Sync for Config<'a>

§

impl<'a> Unpin for Config<'a>

§

impl<'a> UnwindSafe for Config<'a>

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> ToOwned for T
where T: Clone,

§

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>,

§

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>,

§

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.