-
Notifications
You must be signed in to change notification settings - Fork 221
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
Introduce Format
trait
#219
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a78fc37
Add Format trait
szarykott 141ec79
Implement Format for FileFormat
szarykott 63c16af
Add FileExtensions trait
szarykott 0cf715f
Implement FileExtensions trait for FileFormat
szarykott abe7f76
Make File generic over Format and FileExtensions
szarykott 4775107
Satisfy clippy's type_complexity lint
szarykott bb23458
Add documentation to file related types
szarykott 6fa1b27
Add custom_format example
szarykott 583a477
Change FileExtensions signature
szarykott 5ba9013
Apply clippy lints and format
szarykott e0df152
Add information to changelog and README
szarykott b09d57a
Change FileExtensions (rename, make subtrait)
szarykott 3676320
Fix reqwest version to make it work on rust 1.46
szarykott 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 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
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,50 @@ | ||
use config::{Config, File, FileStoredFormat, Format, Map, Value, ValueKind}; | ||
|
||
fn main() { | ||
let config = Config::builder() | ||
.add_source(File::from_str("bad", MyFormat)) | ||
.add_source(File::from_str("good", MyFormat)) | ||
.build(); | ||
|
||
match config { | ||
Ok(cfg) => println!("A config: {:#?}", cfg), | ||
Err(e) => println!("An error: {}", e), | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct MyFormat; | ||
|
||
impl Format for MyFormat { | ||
fn parse( | ||
&self, | ||
uri: Option<&String>, | ||
text: &str, | ||
) -> Result<Map<String, config::Value>, Box<dyn std::error::Error + Send + Sync>> { | ||
// Let's assume our format is somewhat crippled, but this is fine | ||
// In real life anything can be used here - nom, serde or other. | ||
// | ||
// For some more real-life examples refer to format implementation within the library code | ||
let mut result = Map::new(); | ||
|
||
if text == "good" { | ||
result.insert( | ||
"key".to_string(), | ||
Value::new(uri, ValueKind::String(text.into())), | ||
); | ||
} else { | ||
println!("Something went wrong in {:?}", uri); | ||
} | ||
|
||
Ok(result) | ||
} | ||
} | ||
|
||
// As crazy as it seems for config sourced from a string, legacy demands its sacrifice | ||
// It is only required for File source, custom sources can use Format without caring for extensions | ||
static MY_FORMAT_EXT: Vec<&'static str> = vec![]; | ||
impl FileStoredFormat for MyFormat { | ||
fn file_extensions(&self) -> &'static [&'static str] { | ||
&MY_FORMAT_EXT | ||
} | ||
} |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed where clause on the struct as this is not recommended to have them either way.