#![warn(missing_docs, unsafe_op_in_unsafe_fn)]
#![warn(clippy::pedantic)]
#![allow(clippy::missing_errors_doc, clippy::module_name_repetitions, clippy::must_use_candidate)]
mod config;
mod format;
pub use crate::{config::*, format::*};
use anyhow::{anyhow, bail, Result};
use std::{env, fs::File, io::prelude::*, path::Path};
pub const CONFIG_NAME: &str = "Drone.toml";
impl Config {
pub fn read_from_current_dir() -> Result<Self> {
Self::read(Path::new("."))
}
pub fn read_from_cargo_manifest_dir() -> Result<Self> {
if let Ok(string) = env::var("CARGO_MANIFEST_DIR_OVERRIDE") {
Self::parse(&string)
} else {
Self::read(
env::var_os("CARGO_MANIFEST_DIR")
.ok_or_else(|| anyhow!("`CARGO_MANIFEST_DIR` is not set"))?
.as_ref(),
)
}
}
pub fn read(crate_root: &Path) -> Result<Self> {
let crate_root = crate_root.canonicalize()?;
let path = crate_root.join(CONFIG_NAME);
if !path.exists() {
bail!("`{}` not exists in `{}", CONFIG_NAME, crate_root.display());
}
let mut buffer = String::new();
let mut file = File::open(&path)?;
file.read_to_string(&mut buffer)?;
Self::parse(&buffer)
}
pub fn parse(string: &str) -> Result<Self> {
let config = toml::from_str::<Self>(&string)?;
config.check_heaps()?;
Ok(config)
}
fn check_heaps(&self) -> Result<()> {
let Self { heap: Heap { main, extra }, .. } = self;
main.check_pools()?;
for heap in extra.values() {
heap.block.check_pools()?;
}
Ok(())
}
}
impl HeapBlock {
fn check_pools(&self) -> Result<()> {
let Self { size, pools } = self;
let used: u32 = pools.iter().map(|pool| pool.block * pool.capacity).sum();
if used != *size {
bail!("{}: `heap.pools` adds up to {}, but `heap.size = {}", CONFIG_NAME, used, size);
}
Ok(())
}
}