From 2b438ed9b53fee5689032f3b5fcdda8d15becd5f Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Thu, 22 Jun 2017 14:14:29 -0700 Subject: Add builder API to Config --- src/file/source/file.rs | 17 +++++++++++++---- src/file/source/mod.rs | 7 +++++-- src/file/source/string.rs | 5 ++++- 3 files changed, 22 insertions(+), 7 deletions(-) (limited to 'src/file/source') diff --git a/src/file/source/file.rs b/src/file/source/file.rs index 79f46bf..426c0b5 100644 --- a/src/file/source/file.rs +++ b/src/file/source/file.rs @@ -3,7 +3,7 @@ use std::result; use std::error::Error; use std::path::{PathBuf, Path}; -use ::file::format::ALL_EXTENSIONS; +use file::format::ALL_EXTENSIONS; use std::io::{self, Read}; use std::fs; use std::env; @@ -13,6 +13,7 @@ use source::Source; use super::{FileFormat, FileSource}; /// Describes a file sourced from a file +#[derive(Clone, Debug)] pub struct FileSourceFile { /// Basename of configuration file name: String, @@ -30,7 +31,9 @@ impl FileSourceFile { } } - fn find_file(&self, format_hint: Option) -> Result<(PathBuf, FileFormat), Box> { + fn find_file(&self, + format_hint: Option) + -> Result<(PathBuf, FileFormat), Box> { // Build expected configuration file let mut basename = PathBuf::new(); @@ -47,7 +50,11 @@ impl FileSourceFile { Some(format) => Ok((filename, format)), None => { for (format, extensions) in ALL_EXTENSIONS.iter() { - if extensions.contains(&filename.extension().unwrap_or_default().to_string_lossy().as_ref()) { + if extensions.contains(&filename + .extension() + .unwrap_or_default() + .to_string_lossy() + .as_ref()) { return Ok((filename, *format)); } } @@ -90,7 +97,9 @@ impl FileSourceFile { } impl FileSource for FileSourceFile { - fn resolve(&self, format_hint: Option) -> Result<(Option, String, FileFormat), Box> { + fn resolve(&self, + format_hint: Option) + -> Result<(Option, String, FileFormat), Box> { // Find file let (filename, format) = self.find_file(format_hint)?; diff --git a/src/file/source/mod.rs b/src/file/source/mod.rs index 89f1a3c..67e2cf1 100644 --- a/src/file/source/mod.rs +++ b/src/file/source/mod.rs @@ -1,12 +1,15 @@ pub mod file; pub mod string; +use std::fmt::Debug; use std::error::Error; use source::Source; use super::FileFormat; /// Describes where the file is sourced -pub trait FileSource { - fn resolve(&self, format_hint: Option) -> Result<(Option, String, FileFormat), Box>; +pub trait FileSource: Debug + Clone { + fn resolve(&self, + format_hint: Option) + -> Result<(Option, String, FileFormat), Box>; } diff --git a/src/file/source/string.rs b/src/file/source/string.rs index 922e46c..70101d6 100644 --- a/src/file/source/string.rs +++ b/src/file/source/string.rs @@ -6,6 +6,7 @@ use source::Source; use super::{FileSource, FileFormat}; /// Describes a file sourced from a string +#[derive(Clone, Debug)] pub struct FileSourceString(String); impl<'a> From<&'a str> for FileSourceString { @@ -15,7 +16,9 @@ impl<'a> From<&'a str> for FileSourceString { } impl FileSource for FileSourceString { - fn resolve(&self, format_hint: Option) -> Result<(Option, String, FileFormat), Box> { + fn resolve(&self, + format_hint: Option) + -> Result<(Option, String, FileFormat), Box> { Ok((None, self.0.clone(), format_hint.expect("from_str requires a set file format"))) } } -- cgit v1.2.3