use crate::config::Config;
use crate::error::{
Error,
Result,
};
use rust_embed::RustEmbed;
use std::path::Path;
use std::str;
#[derive(Debug, RustEmbed)]
#[folder = "config/"]
pub struct EmbeddedConfig;
impl EmbeddedConfig {
pub fn get_config() -> Result<String> {
match Self::get(crate::DEFAULT_CONFIG) {
Some(v) => Ok(str::from_utf8(&v.data)?.to_string()),
None => Err(Error::EmbeddedError(String::from(
"Embedded config not found",
))),
}
}
pub fn parse() -> Result<Config> {
Config::parse_from_str(&Self::get_config()?)
}
}
#[derive(RustEmbed)]
#[folder = "examples/"]
pub struct BuiltinConfig;
impl BuiltinConfig {
pub fn get_config(mut name: String) -> Result<String> {
if !Path::new(&name)
.extension()
.map_or(false, |ext| ext.eq_ignore_ascii_case("toml"))
{
name = format!("{name}.toml");
}
let contents = match Self::get(&name) {
Some(v) => Ok(str::from_utf8(&v.data)?.to_string()),
None => Err(Error::EmbeddedError(format!("config {} not found", name,))),
}?;
Ok(contents)
}
pub fn parse(name: String) -> Result<(Config, String)> {
Ok((toml::from_str(&Self::get_config(name.to_string())?)?, name))
}
}