Skip to content

Commit fd28355

Browse files
committed
refactor(format): Simplify extension mapping
1 parent b154785 commit fd28355

File tree

2 files changed

+45
-36
lines changed

2 files changed

+45
-36
lines changed

src/file/format/mod.rs

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use std::collections::HashMap;
21
use std::error::Error;
3-
use std::sync::OnceLock;
42

53
use crate::map::Map;
64
use crate::{file::FileStoredFormat, value::Value, Format};
@@ -54,41 +52,54 @@ pub enum FileFormat {
5452
Json5,
5553
}
5654

57-
pub(crate) fn all_extensions() -> &'static HashMap<FileFormat, Vec<&'static str>> {
58-
#![allow(unused_mut)] // If no features are used, there is an "unused mut" warning in `all_extensions`
59-
60-
static ALL_EXTENSIONS: OnceLock<HashMap<FileFormat, Vec<&'static str>>> = OnceLock::new();
61-
ALL_EXTENSIONS.get_or_init(|| {
62-
let mut formats: HashMap<FileFormat, Vec<_>> = HashMap::new();
63-
64-
#[cfg(feature = "toml")]
65-
formats.insert(FileFormat::Toml, vec!["toml"]);
55+
impl FileFormat {
56+
pub(crate) fn all() -> &'static [FileFormat] {
57+
&[
58+
#[cfg(feature = "toml")]
59+
FileFormat::Toml,
60+
#[cfg(feature = "json")]
61+
FileFormat::Json,
62+
#[cfg(feature = "yaml")]
63+
FileFormat::Yaml,
64+
#[cfg(feature = "ini")]
65+
FileFormat::Ini,
66+
#[cfg(feature = "ron")]
67+
FileFormat::Ron,
68+
#[cfg(feature = "json5")]
69+
FileFormat::Json5,
70+
]
71+
}
6672

67-
#[cfg(feature = "json")]
68-
formats.insert(FileFormat::Json, vec!["json"]);
73+
pub(crate) fn extensions(&self) -> &'static [&'static str] {
74+
match self {
75+
#[cfg(feature = "toml")]
76+
FileFormat::Toml => &["toml"],
6977

70-
#[cfg(feature = "yaml")]
71-
formats.insert(FileFormat::Yaml, vec!["yaml", "yml"]);
78+
#[cfg(feature = "json")]
79+
FileFormat::Json => &["json"],
7280

73-
#[cfg(feature = "ini")]
74-
formats.insert(FileFormat::Ini, vec!["ini"]);
81+
#[cfg(feature = "yaml")]
82+
FileFormat::Yaml => &["yaml", "yml"],
7583

76-
#[cfg(feature = "ron")]
77-
formats.insert(FileFormat::Ron, vec!["ron"]);
84+
#[cfg(feature = "ini")]
85+
FileFormat::Ini => &["ini"],
7886

79-
#[cfg(feature = "json5")]
80-
formats.insert(FileFormat::Json5, vec!["json5"]);
87+
#[cfg(feature = "ron")]
88+
FileFormat::Ron => &["ron"],
8189

82-
formats
83-
})
84-
}
90+
#[cfg(feature = "json5")]
91+
FileFormat::Json5 => &["json5"],
8592

86-
impl FileFormat {
87-
pub(crate) fn extensions(&self) -> &'static [&'static str] {
88-
// It should not be possible for this to fail
89-
// A FileFormat would need to be declared without being added to the
90-
// all_extensions map.
91-
all_extensions().get(self).unwrap()
93+
#[cfg(all(
94+
not(feature = "toml"),
95+
not(feature = "json"),
96+
not(feature = "yaml"),
97+
not(feature = "ini"),
98+
not(feature = "ron"),
99+
not(feature = "json5"),
100+
))]
101+
_ => unreachable!("No features are enabled, this library won't work without features"),
102+
}
92103
}
93104

94105
pub(crate) fn parse(

src/file/source/file.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ use std::fs;
44
use std::io;
55
use std::path::PathBuf;
66

7-
use crate::file::{
8-
format::all_extensions, source::FileSourceResult, FileSource, FileStoredFormat, Format,
9-
};
7+
use crate::file::{source::FileSourceResult, FileFormat, FileSource, FileStoredFormat, Format};
108

119
/// Describes a file sourced from a file
1210
#[derive(Clone, Debug)]
@@ -39,8 +37,8 @@ impl FileSourceFile {
3937
return Ok((filename, Box::new(format)));
4038
} else {
4139
let ext = filename.extension().unwrap_or_default().to_string_lossy();
42-
for (format, extensions) in all_extensions().iter() {
43-
if extensions.contains(&ext.as_ref()) {
40+
for format in FileFormat::all() {
41+
if format.extensions().contains(&ext.as_ref()) {
4442
return Ok((filename, Box::new(*format)));
4543
}
4644
}
@@ -71,7 +69,7 @@ impl FileSourceFile {
7169
}
7270
}
7371
None => {
74-
for format in all_extensions().keys() {
72+
for format in FileFormat::all() {
7573
for ext in format.extensions() {
7674
filename.set_extension(ext);
7775

0 commit comments

Comments
 (0)