Skip to content
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 an option for disabling the automatic resolver in FileSourceFile #133

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ impl File<source::file::FileSourceFile> {
source: source::file::FileSourceFile::new(name.into()),
}
}

/// Given the full name of a file, will use it only if with the exact name without
/// any attempt to locate another file. It will analyze the extension if the property
/// format isn't setted but without using file with a different fullname.
pub fn with_exact_name(name: &str) -> Self {
Self::with_name(name).exact(true)
}

/// If enabeld, and a file with the exact name is not found,
/// will not attempt to locate a file based on the format property.
pub fn exact(mut self, flag: bool) -> Self {
self.source.disable_file_resolve(flag);
self
}
}

impl<'a> From<&'a Path> for File<source::file::FileSourceFile> {
Expand Down
41 changes: 25 additions & 16 deletions src/file/source/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@ use source::Source;
pub struct FileSourceFile {
/// Path of configuration file
name: PathBuf,

/// Raise error if the exact filename is not found
disable_file_resolve: bool,
}

impl FileSourceFile {
pub fn new(name: PathBuf) -> FileSourceFile {
FileSourceFile { name: name }
FileSourceFile { name: name, disable_file_resolve: false }
}

pub fn disable_file_resolve(&mut self, flag: bool) {
self.disable_file_resolve = flag;
}

fn find_file(
Expand Down Expand Up @@ -50,31 +57,33 @@ impl FileSourceFile {
io::ErrorKind::NotFound,
format!(
"configuration file \"{}\" is not of a registered file format",
filename.to_string_lossy()
self.name.to_string_lossy()
),
)))
}
};
}

match format_hint {
Some(format) => for ext in format.extensions() {
filename.set_extension(ext);

if filename.is_file() {
return Ok((filename, format));
}
},

None => for (format, extensions) in ALL_EXTENSIONS.iter() {
for ext in format.extensions() {
if !self.disable_file_resolve {
match format_hint {
Some(format) => for ext in format.extensions() {
filename.set_extension(ext);

if filename.is_file() {
return Ok((filename, *format));
return Ok((filename, format));
}
}
},
},

None => for (format, extensions) in ALL_EXTENSIONS.iter() {
for ext in format.extensions() {
filename.set_extension(ext);

if filename.is_file() {
return Ok((filename, *format));
}
}
},
}
}

Err(Box::new(io::Error::new(
Expand Down
8 changes: 8 additions & 0 deletions tests/Settings.wrongextension
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
debug = false
production = true

[place]
rating = 4.9

[place.creator]
name = "Somebody New"
34 changes: 34 additions & 0 deletions tests/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,40 @@ fn test_file_required_not_found() {
);
}

#[test]
fn test_file_exact_not_exist() {
let mut c = Config::default();
let res = c.merge(File::with_exact_name("tests/Settings.wrong"));

assert!(res.is_err());
assert_eq!(
res.unwrap_err().to_string(),
"configuration file \"tests/Settings.wrong\" not found".to_string()
);
}

#[test]
fn test_file_exact_exist_invalid_extension() {
let mut c = Config::default();
let res = c.merge(File::with_exact_name("tests/Settings.wrongextension"));

assert!(res.is_err());
assert_eq!(
res.unwrap_err().to_string(),
"configuration file \"tests/Settings.wrongextension\" is not of a registered file format".to_string()
);
}

#[test]
fn test_file_exact_explicit_format() {
let mut c = Config::default();
c.merge(File::new("tests/Settings.wrongextension", FileFormat::Toml).exact(true))
.unwrap();

assert_eq!(c.get("debug").ok(), Some(false));
assert_eq!(c.get("production").ok(), Some(true));
}

#[test]
fn test_file_auto() {
let mut c = Config::default();
Expand Down