Crate luci

source ·
Expand description

Define and read configuration file.

A configuration file must not be: execute by owner; read, write or execute by other users; read, write or execute by other groups. That mean a configuration file may has the following modes: 400 or 600. Other than that, reading returns error::ErrorKind::Insecured.

use std::process;

use serde::Deserialize;

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Configuration {
   pub host: String,
   pub port: u16,
}

fn main() {
   let files = ["examples/foo.json", "~/.config/foo.json", "/etc/foo.json"];
   let (config, file) = match luci::read_jsonc::<Configuration, _>(files.iter()) {
       Ok(v) => v,
       Err(e) => {
           println!("Can not read configuration file.\n{}", e);
           process::exit(1);
       }
   };
   println!("Read configuration from {}", file.to_string_lossy());
   println!("> host={}", config.host);
   println!("> port={}", config.port);
}

Modules§

Functions§

  • Go through a list of JSONC files. For each file, try to map it into data structure T. Return result on the first success or failure. The configuration file must be secured by mode=600.