diff options
author | CfirTsabari <cfir16@gmail.com> | 2022-03-27 11:22:53 +0000 |
---|---|---|
committer | CfirTsabari <cfir16@gmail.com> | 2022-04-10 18:37:35 +0000 |
commit | 34ea07a8a714fd4c8d4b4bc0606e2d536dc275d5 (patch) | |
tree | 1bd52456c912a832b637a0728b9c6c8c355fc98c | |
parent | 8f1ccff320aa81eed85d4d4d686b56d2dd0e5643 (diff) |
fix: dot in config name
Rust std `set_extension` is either add an extension if none exists, or
edit the current one if such exists.
This caused a mishandling when user is using a name with a dot,
part of the name was treated as an extension, and be overwritten by
the different format extensions.
So manually *adding* a new dummy extension will cause the current code,
to behave as expected, since it will always overwrite the new dummy
extension and not part of the name.
-rw-r--r-- | src/file/source/file.rs | 20 | ||||
-rw-r--r-- | tests/Settings2.default.ini | 9 | ||||
-rw-r--r-- | tests/file.rs | 10 |
3 files changed, 38 insertions, 1 deletions
diff --git a/src/file/source/file.rs b/src/file/source/file.rs index 403f3f2..8ee5d31 100644 --- a/src/file/source/file.rs +++ b/src/file/source/file.rs @@ -27,7 +27,7 @@ impl FileSourceFile { where F: FileStoredFormat + Format + 'static, { - let mut filename = if self.name.is_absolute() { + let filename = if self.name.is_absolute() { self.name.clone() } else { env::current_dir()?.as_path().join(&self.name) @@ -59,6 +59,9 @@ impl FileSourceFile { ))) }; } + // Adding a dummy extension will make sure we will not override secondary extensions, i.e. "file.local" + // This will make the following set_extension function calls to append the extension. + let mut filename = add_dummy_extension(filename); match format_hint { Some(format) => { @@ -121,3 +124,18 @@ where }) } } + +fn add_dummy_extension(mut filename: PathBuf) -> PathBuf { + match filename.extension() { + Some(extension) => { + let mut ext = extension.to_os_string(); + ext.push("."); + ext.push("dummy"); + filename.set_extension(ext); + } + None => { + filename.set_extension("dummy"); + } + } + filename +} diff --git a/tests/Settings2.default.ini b/tests/Settings2.default.ini new file mode 100644 index 0000000..16badd4 --- /dev/null +++ b/tests/Settings2.default.ini @@ -0,0 +1,9 @@ +debug = true
+production = false
+[place]
+name = Torre di Pisa
+longitude = 43.7224985
+latitude = 10.3970522
+favorite = false
+reviews = 3866
+rating = 4.5
diff --git a/tests/file.rs b/tests/file.rs index 7bd576e..9e4469a 100644 --- a/tests/file.rs +++ b/tests/file.rs @@ -58,3 +58,13 @@ fn test_file_ext() { assert_eq!(c.get("debug").ok(), Some(true)); assert_eq!(c.get("production").ok(), Some(false)); } +#[test] +fn test_file_second_ext() { + let c = Config::builder() + .add_source(File::with_name("tests/Settings2.default")) + .build() + .unwrap(); + + assert_eq!(c.get("debug").ok(), Some(true)); + assert_eq!(c.get("production").ok(), Some(false)); +} |