|
1 |
| -use std::collections::HashMap; |
2 | 1 | use std::error::Error;
|
3 |
| -use std::sync::OnceLock; |
4 | 2 |
|
5 | 3 | use crate::map::Map;
|
6 | 4 | use crate::{file::FileStoredFormat, value::Value, Format};
|
@@ -54,41 +52,54 @@ pub enum FileFormat {
|
54 | 52 | Json5,
|
55 | 53 | }
|
56 | 54 |
|
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 | + } |
66 | 72 |
|
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"], |
69 | 77 |
|
70 |
| - #[cfg(feature = "yaml")] |
71 |
| - formats.insert(FileFormat::Yaml, vec!["yaml", "yml"]); |
| 78 | + #[cfg(feature = "json")] |
| 79 | + FileFormat::Json => &["json"], |
72 | 80 |
|
73 |
| - #[cfg(feature = "ini")] |
74 |
| - formats.insert(FileFormat::Ini, vec!["ini"]); |
| 81 | + #[cfg(feature = "yaml")] |
| 82 | + FileFormat::Yaml => &["yaml", "yml"], |
75 | 83 |
|
76 |
| - #[cfg(feature = "ron")] |
77 |
| - formats.insert(FileFormat::Ron, vec!["ron"]); |
| 84 | + #[cfg(feature = "ini")] |
| 85 | + FileFormat::Ini => &["ini"], |
78 | 86 |
|
79 |
| - #[cfg(feature = "json5")] |
80 |
| - formats.insert(FileFormat::Json5, vec!["json5"]); |
| 87 | + #[cfg(feature = "ron")] |
| 88 | + FileFormat::Ron => &["ron"], |
81 | 89 |
|
82 |
| - formats |
83 |
| - }) |
84 |
| -} |
| 90 | + #[cfg(feature = "json5")] |
| 91 | + FileFormat::Json5 => &["json5"], |
85 | 92 |
|
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 | + } |
92 | 103 | }
|
93 | 104 |
|
94 | 105 | pub(crate) fn parse(
|
|
0 commit comments