Skip to content

Commit

Permalink
core: simplify examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ramiroaisen committed Jul 6, 2024
1 parent 2276219 commit a02ca8b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,35 @@


## What is it?
**metre** is a configuration loader for Rust that allows you to load configurations from a variety of formats such as **toml**, **json**, **jsonc** and **yaml**

It also supports a variety of sources such as **program defaults**, **env variables**, **files**, and **urls**.

### Usage
## Usage
```rust
#[derive(Config)]
struct MyConfig {
foo: u16,
bar: String,
}

let mut loader = ConfigLoader::<MyConfig>::new();

loader.file("config.json", metre::Format::Json)?;
loader.env()?;
loader.defaults()?;

// config have the type MyConfig here
let config = loader.finish()?;
use metre::{Config, ConfigLoader, Format};

```
#[derive(Config)]
struct MyConfig {
foo: u16,
bar: String,
}

let mut loader = ConfigLoader::<MyConfig>::new();

**metre** is a configuration loader for Rust that allows you to load configurations from a variety of formats such as **toml**, **json**, **jsonc** and **yaml**
loader.file("config.json", Format::Json)?;
loader.env()?;
loader.defaults()?;

It also supports a variety of sources such as **program defaults**, **env variables**, **files**, and **urls**.
// config have the type MyConfig here
let config = loader.finish()?;
```

## Focus

**metre** focus is to provide a **declarative** and **type-safe** way to load configurations in Rust.


## How?

**metre** works by defining a struct that implements the `Config` trait, usually via the `#[derive(Config)]` macro.
Expand Down
13 changes: 9 additions & 4 deletions metre/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,27 @@
//!
//! To see all macro attributes available see the [Config](macro@Config) derive macro documentation.
//!
//! ```no_run
//! use metre::{Config, ConfigLoader};
//! ```
//! # fn load_config() -> Result<(), Box<dyn std::error::Error>> {
//! use metre::{Config, ConfigLoader, Format};
//!
//! #[derive(Config)]
//! struct MyConfig {
//! foo: u16,
//! bar: String,
//! }
//!
//! let loader = ConfigLoader::<MyConfig>::new();
//! let mut loader = ConfigLoader::<MyConfig>::new();
//!
//! loader.file("config.json", metre::Format::Json)?;
//! loader.file("config.json", Format::Json)?;
//! loader.env()?;
//! loader.defaults()?;
//!
//! // config have the type MyConfig here, or loader.finish() returns an error
//! let config = loader.finish()?;
//!
//! # Ok(())
//! # }
//! ```
use owo_colors::*;
Expand Down
4 changes: 2 additions & 2 deletions metre/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::collections::HashMap;
#[ignore]
#[allow(unused)]
fn readme_example() -> Result<(), Box<dyn std::error::Error>> {
use metre::{Config, ConfigLoader};
use metre::{Config, ConfigLoader, Format};

#[derive(Config)]
struct MyConfig {
Expand All @@ -18,7 +18,7 @@ fn readme_example() -> Result<(), Box<dyn std::error::Error>> {

let mut loader = ConfigLoader::<MyConfig>::new();

loader.file("config.json", metre::Format::Json)?;
loader.file("config.json", Format::Json)?;
loader.env()?;
loader.defaults()?;

Expand Down

0 comments on commit a02ca8b

Please sign in to comment.