summaryrefslogtreecommitdiffstats
path: root/src/file/mod.rs
diff options
context:
space:
mode:
authorRyan Leckey <ryan@launchbadge.com>2017-06-14 00:28:43 -0700
committerRyan Leckey <ryan@launchbadge.com>2017-06-14 00:28:43 -0700
commit1716c2fe7571b03215a5ae09c91f508d03335c2a (patch)
treef2b3144044f907c8eb1c594460d6cd74eed92b1f /src/file/mod.rs
parent9a2e756075dce24e764c8c89e2d71703a69fd80a (diff)
parent736738a2a798d58b1a73b0da146924a7e92ceba3 (diff)
Merge branch 'feature/with_name' of https://github.com/JordiPolo/config-rs into JordiPolo-feature/with_name
Diffstat (limited to 'src/file/mod.rs')
-rw-r--r--src/file/mod.rs18
1 files changed, 15 insertions, 3 deletions
diff --git a/src/file/mod.rs b/src/file/mod.rs
index 7ab77d8..d19f973 100644
--- a/src/file/mod.rs
+++ b/src/file/mod.rs
@@ -5,6 +5,7 @@ use source::Source;
use error::*;
use value::Value;
use std::collections::HashMap;
+use std::path::Path;
use self::source::FileSource;
pub use self::format::FileFormat;
@@ -44,6 +45,17 @@ impl File<source::file::FileSourceFile> {
source: source::file::FileSourceFile::new(name),
}
}
+
+ /// Given the basename of a file, will attempt to locate a file by setting its
+ /// extension to a registered format.
+ pub fn with_name(name: &str) -> Self {
+ File {
+ format: None,
+ required: true,
+ namespace: None,
+ source: source::file::FileSourceFile::new(name),
+ }
+ }
}
impl<T: FileSource> File<T> {
@@ -61,10 +73,10 @@ impl<T: FileSource> File<T> {
impl<T: FileSource> Source for File<T> {
fn collect(&self) -> Result<HashMap<String, Value>> {
// Coerce the file contents to a string
- let (uri, contents) = match self.source.resolve(self.format).map_err(|err| {
+ let (uri, contents, format) = match self.source.resolve(self.format).map_err(|err| {
ConfigError::Foreign(err)
}) {
- Ok((uri, contents)) => (uri, contents),
+ Ok((uri, contents, format)) => (uri, contents, format),
Err(error) => {
if !self.required {
@@ -76,7 +88,7 @@ impl<T: FileSource> Source for File<T> {
};
// Parse the string using the given format
- let result = self.format.unwrap().parse(uri.as_ref(), &contents, self.namespace.as_ref()).map_err(|cause| {
+ let result = format.parse(uri.as_ref(), &contents, self.namespace.as_ref()).map_err(|cause| {
ConfigError::FileParse {
uri: uri,
cause: cause