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>
impl<'a> Config<'a>
pub fn quoted(&self) -> bool
pub fn quote_kind(&self) -> Option<QuoteKind>
sourcepub fn children(&self) -> impl Iterator<Item = &Config<'a>>
pub fn children(&self) -> impl Iterator<Item = &Config<'a>>
Iterator for the children of a node.
sourcepub fn child(&self) -> Option<&Config<'a>>
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()
);
sourcepub fn key(&self) -> &'a str
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");
sourcepub fn span(&self) -> Span
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);
sourcepub fn parse_quoted(&self) -> Result<String, NcclError>
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 PartialEq for Config<'_>
impl PartialEq for Config<'_>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.