lwb_parser/config/
toml.rs

1use crate::config::Config;
2use std::path::{Path, PathBuf};
3use thiserror::Error;
4
5/// Find the location of the LWB config.
6/// This function should only be run from build scripts.
7pub fn find_config_path() -> PathBuf {
8    let p = PathBuf::from(
9        std::env::var("CARGO_MANIFEST_DIR").expect("couldn't find cargo manifest dir"),
10    );
11    p.join("lwb.toml")
12}
13
14#[derive(Debug, Error)]
15pub enum ParseConfigError {
16    #[error("toml deserialize error: {0}")]
17    Toml(#[from] toml::de::Error),
18}
19
20#[derive(Debug, Error)]
21pub enum ReadConfigError {
22    #[error(transparent)]
23    Io(#[from] std::io::Error),
24
25    #[error("parse error: {0}")]
26    Parse(#[from] ParseConfigError),
27}
28
29pub fn read_config(path: impl AsRef<Path>) -> Result<Config, ReadConfigError> {
30    let f = std::fs::read_to_string(path)?;
31
32    Ok(parse_config(f)?)
33}
34
35pub fn parse_config(config_str: impl AsRef<str>) -> Result<Config, ParseConfigError> {
36    let res = toml::from_str(config_str.as_ref())?;
37
38    Ok(res)
39}