1use std::path::Path;
2use std::str;
3
4use rust_embed::RustEmbed;
5
6use crate::config::Config;
7use crate::error::{Error, Result};
8
9#[derive(Debug, RustEmbed)]
15#[folder = "config/"]
16pub struct EmbeddedConfig;
17
18impl EmbeddedConfig {
19 pub fn get_config() -> Result<String> {
21 match Self::get(crate::DEFAULT_CONFIG) {
22 Some(v) => Ok(str::from_utf8(&v.data)?.to_string()),
23 None => Err(Error::EmbeddedError(String::from(
24 "Embedded config not found",
25 ))),
26 }
27 }
28
29 pub fn parse() -> Result<Config> {
33 Self::get_config()?.parse()
34 }
35}
36
37#[derive(RustEmbed)]
41#[folder = "examples/"]
42pub struct BuiltinConfig;
43
44impl BuiltinConfig {
45 pub fn get_config(mut name: String) -> Result<String> {
47 if !Path::new(&name)
48 .extension()
49 .is_some_and(|ext| ext.eq_ignore_ascii_case("toml"))
50 {
51 name = format!("{name}.toml");
52 }
53 let contents = match Self::get(&name) {
54 Some(v) => Ok(str::from_utf8(&v.data)?.to_string()),
55 None => Err(Error::EmbeddedError(format!("config {} not found", name,))),
56 }?;
57 Ok(contents)
58 }
59
60 pub fn parse(name: String) -> Result<(Config, String)> {
64 let parsed = Self::get_config(name.to_string())?.parse()?;
65 Ok((parsed, name))
66 }
67}