Kōsei

こうせい

A easy-to-use configuration crate with the Rust programming language.

Features

Quickstart

```rust

[test]

fn basetest() { // Panic if no such file config/config.yaml let config: Config = Config::fromfile("config/config.yaml"); let entry: &Entry = config.asinner(); // borrowed value has the same lifetimes as config let entry: Entry = config.toinner(); // clone a new Entry let entry: Entry = config.into_inner(); // take ownership } ```

```rust

[tokio::test]

async fn dynamictest() {
// Create a dynamic config and a watcher let (config, mut watcher) = DynamicConfig::::watch
file("config/config.yaml"); // Listen to file modify event watcher.watch().unwrap(); let lock = config.lock(); let entry: &Entry = lock.asinner(); // borrow Entry let entry: Entry = lock.toinner(); // clone a new Entry // let entry: Entry = lock.intoinner(); panic! cannot take the lock ownership let arc = config.asarc(); // clone a new arc // Stop watching watcher.stop().unwrap(); // You can watch twice watcher.watch().unwrap(); } ```

```rust

[tokio::test]

async fn apollotest() { // First build a ApolloClient let client = ApolloClient::new("http://localhost:8080") .appid("114514") .namespace("test", ConfigType::TOML); // Create a watcher to fetch apollo config changes at a RealTime mode. // Entry indicates how data should be deserialized. // The returned config type is DynamicConfig<Entry> let (config, mut watcher) = DynamicConfig::::watchapollo(client, WatchMode::RealTime).await; // Enable verbose mode (log messages with INFO level) watcher.verbose(); // Start watching watcher.watch().unwrap();

println!("{:?}", config); // Stop watching watcher.stop().unwrap();

// You can start twice even you forget to call stop() watcher.watch().unwrap();

// All changes will be reflected to config in time do_somthing(config); }

```