Crates.io | confindent |
lib.rs | confindent |
version | 2.2.0 |
source | src |
created_at | 2019-01-29 12:04:35.70843 |
updated_at | 2022-07-01 21:24:26.435027 |
description | ⚙️ A configuration format based on indentation |
homepage | |
repository | https://github.com/gennyble/confindent |
max_upload_size | |
id | 111369 |
size | 31,769 |
Configuration by indentation. Read the spec inspired by
the format of the ssh client configuration commonly found on Linux machines
at ~/.ssh/config
.
use confindent::Confindent;
fn main() {
let conf: Confindent = "Pet Dog\n\tName Brady\n\tAge 10".parse().unwrap();
let pet = conf.child("Pet").unwrap();
let name = pet.child_value("Name").unwrap();
let age: usize = pet.child_parse("Age").unwrap();
let word = match pet.value() {
Some("Dog") => "pupper",
Some("Cat") => "kitty",
_ => panic!(),
};
if age > 9 {
println!("{}! {} is an old {}.", age, name, word);
} else {
println!("Only {}! {} is a good, young {}.", age, name, word);
}
}
It's a kind of tree, key-value thing. Lines are key-value pairs, the value starting at the first space after the indent. You can add a child to a value by indenting it with spaces or tabs. Indent the same amount to add another child to that same value. Indent more than you did initially to add a grandchild. Don't mix spaces and tabs. Like this!
Root this is the root
Child I'm a child!
Child You can have multiple children with the same keys!
Grandchild I'm a grandchild!
Open and parse a file with Confindent::from_file
. Pass it a path. It returns
a Result<Confindent, ParseError>
.
Get a direct child with the child(key)
function. Key needs to be able
to turn into a &str
. This returns an Option<&Value>
. Value
is the main data-storing
struct. You can get multiple Value of the same name with children(key)
, which
returns a Vec<&Value>
.
You can get a Value
's value with value()
. It returns an Option<&str>
. Get an owned,
Option<String>
with value_owned()
. If you want
to check that a Value
has a direct child but don't care about the value, use
has_child(key)
. It returns bool
for whether or not a child was found with that key.
Want to parse a possible value into a different type, T
? Instead of value()
use
parse()
. It returns Result<T, ValueParseError<T>>
. That type
may look weird and that's because it is. ValueParseError
is an enum
that can be NoValue
or ParseError(error)
where error
is the error part of the
Result that T::FromStr
returns.
Don't want to call child(key)
and then value()
or parse()
? You can use
child_value(key)
and child_parse(key)
to do both of those
at once. Both of these functions return what value()
and parse()
normally return,
respectively. There's also child_owned()
which is like value_owned()
wherein
it returns an Option<String>
of a child's value.