-
-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): embed the default configuration file into the binary
- Loading branch information
Showing
6 changed files
with
159 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#![allow(missing_docs)] // RustEmbed generated functions does not have doc comments | ||
|
||
use crate::config::Config; | ||
use crate::error::{ | ||
Error, | ||
Result, | ||
}; | ||
use rust_embed::RustEmbed; | ||
use std::str; | ||
|
||
/// Configuration file embedder/extractor. | ||
/// | ||
/// Embeds `config/`[`DEFAULT_CONFIG`] into the binary. | ||
/// | ||
/// [`DEFAULT_CONFIG`]: crate::DEFAULT_CONFIG | ||
#[derive(Debug, RustEmbed)] | ||
#[folder = "../config/"] | ||
pub struct EmbeddedConfig; | ||
|
||
impl EmbeddedConfig { | ||
/// Extracts the embedded content. | ||
pub fn get_config() -> Result<String> { | ||
match Self::get(crate::DEFAULT_CONFIG) { | ||
Some(v) => Ok(str::from_utf8(&v.data.into_owned()).unwrap().to_string()), | ||
None => Err(Error::EmbeddedError(String::from( | ||
"Embedded config not found", | ||
))), | ||
} | ||
} | ||
|
||
/// Parses the extracted content into [`Config`]. | ||
/// | ||
/// [`Config`]: Config | ||
pub fn parse() -> Result<Config> { | ||
Ok(toml::from_str(&Self::get_config()?)?) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters