hippo_shared/
configuration.rs

1//
2// Hippo
3// (C) 2021 Brave Monday
4//
5
6use std::collections::HashMap;
7use std::env;
8use std::fs;
9use std::io::{Error, ErrorKind, Result};
10use std::path::{Path, PathBuf};
11
12use serde::Deserialize;
13
14use super::Preprocessor;
15
16/// A collection of program options.
17#[derive(Deserialize)]
18pub struct Configuration {
19	#[serde(flatten)]
20	pub preprocessors: HashMap<String, Preprocessor>
21}
22
23impl Configuration {
24	/// The default configuration file name.
25	pub const DEFAULT_NAME: &'static str = "Hippo.toml";
26
27	/// Attempt to locate the Hippo on-disk configuration.
28	pub fn locate() -> Option<PathBuf> {
29		env::var("CARGO_MANIFEST_DIR").ok().map(|p| {
30			let mut buf = PathBuf::new();
31			
32			buf.push(p);
33			buf.push(Self::DEFAULT_NAME);
34
35			buf
36		})
37	}
38
39	/// Load an on-disk configuration.
40	pub fn load(path: &Path) -> Result<Self> {
41		toml::from_str(&fs::read_to_string(path)?).map_err(
42			|e| Error::new(ErrorKind::Other, e)
43		)
44	}
45}