GithubHelp home page GithubHelp logo

remimimimimi / confindent Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gennyble/confindent

0.0 1.0 0.0 88 KB

Configuration format based on indentation; Rust crate

License: ISC License

Rust 100.00%

confindent's Introduction

Confindent

Crates Docs Downloads GitHub workflows badge

Configuration by indentation. Like ~/.ssh/config.

Example, short and sweet

use confindent::Confindent;
use std::error::Error;

fn main() {
	let conf: Confindent = "User gennyble\n\tEmail [email protected]\n\tID 256".parse().unwrap();

	let user = conf.child("User").unwrap();
	let username = user.value().unwrap();
	let email = user.child_value("Email").unwrap();
	let id: usize = user.child_parse("ID").unwrap();

	println!("User {username}: {id} Contact: {email}");
}

Quickstart!

The format, briefly.

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!

Using the crate, quickly! also, here are the docs again

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.

confindent's People

Contributors

gennyble avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.