-
Notifications
You must be signed in to change notification settings - Fork 236
docs: Example for conditionally loading sources #479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,27 @@ | ||
-----BEGIN RSA PRIVATE KEY----- | ||
MIIEpQIBAAKCAQEA7P+5Ow3YfQJJ0W4DhwdJRUWi1cYOen7qQ+XPAtFOdbJcvIZe | ||
T+D+fEENDpkDM+lOE1KtpehW4JOZ13ePLM0phktEf9hT1aB0Zc5LXB3M4YuW+lAW | ||
iXF9moHWxa2DlpyGck7cSlVVKbdljP9AQOzMdTXi3JJUWlnqjeUSINnBqCW21nOh | ||
frUZZbqBKOx7/iEgjdAsR7K5WAyVIXgbIipCAgWWjP3ejUjrl20QpXu06dGQF8O8 | ||
S7ztwLwtmdUJ5SBrhiub1Ocjr+DSUvIg8kOzUp9gQiMtV8RGTr4W0Bfr75ZwgAfC | ||
oDNZ+D9S/rlZX0eJJd5rMobiQRE8qBk2oxWcxwIDAQABAoIBAQCtP3MEvGZZW+bi | ||
de2WM7lYLkOOyi2jVkuiPshJYwBcAXrRRdiDxBHEezk0Rp6UwCQW9AWEloeLu9pm | ||
LDw5n/CO/06ftl/ydk0gbuGgARjYd9ZyPUF8T75lyCxcbS8YVmvh+8wFesO6rxpJ | ||
K/6od3Iu7KleXInVUo2oFKBf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BL | ||
8jqfXxr2ZpYf9mEgZI8v8Pr013R3Vqkpy+B8jlfpvxFdOwSzkY42ur4= | ||
-----END RSA PRIVATE KEY----- |
This file contains hidden or 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,9 @@ | ||
-----BEGIN PUBLIC KEY----- | ||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA7P+5Ow3YfQJJ0W4DhwdJ | ||
RUWi1cYOen7qQ+XPAtFOdbJcvIZeT+D+fEENDpkDM+lOE1KtpehW4JOZ13ePLM0p | ||
hktEf9hT1aB0Zc5LXB3M4YuW+lAWiXF9moHWxa2DlpyGck7cSlVVKbdljP9AQOzM | ||
dTXi3JJUWlnqjeUSINnBqCW21nOhfrUZZbqBKOx7/iEgjdAsR7K5WAyVIXgbIipC | ||
AgWWjP3ejUjrl20QpXu06dGQF8O8S7ztwLwtmdUJ5SBrhiub1Ocjr+DSUvIg8kOz | ||
Up9gQiMtV8RGTr4W0Bfr75ZwgAfCoDNZ+D9S/rlZX0eJJd5rMobiQRE8qBk2oxWc | ||
xwIDAQAB | ||
-----END PUBLIC KEY----- |
This file contains hidden or 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,73 @@ | ||
use config::{Config, File, FileStoredFormat, Format, Map, Value, ValueKind}; | ||
use std::io::{Error, ErrorKind}; | ||
|
||
/// The private and public key sources will be read into their associated variable: | ||
#[derive(serde::Deserialize, Clone, Debug)] | ||
pub struct Settings { | ||
pub private_key: Option<String>, | ||
pub public_key: Option<String>, | ||
} | ||
|
||
fn main() { | ||
// Sourcing from two separate files for the `Settings` struct,: | ||
let file_public_key = File::new("examples/custom_file_format/files/public.pem", PemFile); | ||
let file_private_key = File::new("examples/custom_file_format/files/private.pem", PemFile); | ||
|
||
// Provide the sources and build the config object: | ||
// Both are marked as optional to avoid failure if the file doesn't exist. | ||
let settings = Config::builder() | ||
.add_source(file_public_key.required(false)) | ||
.add_source(file_private_key.required(false)) | ||
.build() | ||
.unwrap(); | ||
|
||
// Deserialize the config object into your Settings struct: | ||
let settings: Settings = settings.try_deserialize().unwrap(); | ||
println!("{:#?}", settings); | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct PemFile; | ||
|
||
impl Format for PemFile { | ||
fn parse( | ||
&self, | ||
uri: Option<&String>, | ||
text: &str, | ||
) -> Result<Map<String, config::Value>, Box<dyn std::error::Error + Send + Sync>> { | ||
// Store any valid keys into this map, they'll be merged with other sources into the final config map: | ||
let mut result = Map::new(); | ||
|
||
// Identify the PEM encoded data type by the first occurrence found: | ||
// NOTE: This example is kept simple, multiple or other encoded types are not handled. | ||
let key_type = vec!["PUBLIC", "PRIVATE"] | ||
.into_iter() | ||
.find(|s| text.contains(s)); | ||
let key = match key_type { | ||
Some("PRIVATE") => "private_key", | ||
Some("PUBLIC") => "public_key", | ||
// Otherwise fail with an error message (the filename is implicitly appended): | ||
_ => { | ||
return Err(Box::new(Error::new( | ||
ErrorKind::InvalidData, | ||
"PEM file did not contain a Private or Public key", | ||
))) | ||
} | ||
}; | ||
|
||
result.insert( | ||
key.to_owned(), | ||
Value::new(uri, ValueKind::String(text.into())), | ||
); | ||
|
||
Ok(result) | ||
} | ||
} | ||
|
||
// A slice of extensions associated to this format, when an extension | ||
// is omitted from a file source, these will be tried implicitly: | ||
impl FileStoredFormat for PemFile { | ||
fn file_extensions(&self) -> &'static [&'static str] { | ||
&["pem"] | ||
} | ||
} |
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.