Skip to content

Commit b7d8e2c

Browse files
committed
refactor: Drop lazy_static dependency
Fixes #514
1 parent ac99226 commit b7d8e2c

File tree

4 files changed

+12
-17
lines changed

4 files changed

+12
-17
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ preserve_order = ["indexmap", "toml?/preserve_order", "serde_json?/preserve_orde
2525
async = ["async-trait"]
2626

2727
[dependencies]
28-
lazy_static = "1.4"
2928
serde = "1.0"
3029
nom = "7"
3130

src/file/format/mod.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
// If no features are used, there is an "unused mut" warning in `ALL_EXTENSIONS`
2-
// BUG: ? For some reason this doesn't do anything if I try and function scope this
3-
#![allow(unused_mut)]
4-
5-
use lazy_static::lazy_static;
61
use std::collections::HashMap;
72
use std::error::Error;
3+
use std::sync::OnceLock;
84

95
use crate::map::Map;
106
use crate::{file::FileStoredFormat, value::Value, Format};
@@ -57,10 +53,11 @@ pub enum FileFormat {
5753
Json5,
5854
}
5955

60-
lazy_static! {
61-
#[doc(hidden)]
62-
// #[allow(unused_mut)] ?
63-
pub static ref ALL_EXTENSIONS: HashMap<FileFormat, Vec<&'static str>> = {
56+
pub(crate) fn all_extensions() -> &'static HashMap<FileFormat, Vec<&'static str>> {
57+
#![allow(unused_mut)] // If no features are used, there is an "unused mut" warning in `all_extensions`
58+
59+
static ALL_EXTENSIONS: OnceLock<HashMap<FileFormat, Vec<&'static str>>> = OnceLock::new();
60+
ALL_EXTENSIONS.get_or_init(|| {
6461
let mut formats: HashMap<FileFormat, Vec<_>> = HashMap::new();
6562

6663
#[cfg(feature = "toml")]
@@ -82,15 +79,15 @@ lazy_static! {
8279
formats.insert(FileFormat::Json5, vec!["json5"]);
8380

8481
formats
85-
};
82+
})
8683
}
8784

8885
impl FileFormat {
8986
pub(crate) fn extensions(&self) -> &'static [&'static str] {
9087
// It should not be possible for this to fail
9188
// A FileFormat would need to be declared without being added to the
92-
// ALL_EXTENSIONS map.
93-
ALL_EXTENSIONS.get(self).unwrap()
89+
// all_extensions map.
90+
all_extensions().get(self).unwrap()
9491
}
9592

9693
pub(crate) fn parse(

src/file/source/file.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::io;
55
use std::path::PathBuf;
66

77
use crate::file::{
8-
format::ALL_EXTENSIONS, source::FileSourceResult, FileSource, FileStoredFormat, Format,
8+
format::all_extensions, source::FileSourceResult, FileSource, FileStoredFormat, Format,
99
};
1010

1111
/// Describes a file sourced from a file
@@ -38,7 +38,7 @@ impl FileSourceFile {
3838
return if let Some(format) = format_hint {
3939
Ok((filename, Box::new(format)))
4040
} else {
41-
for (format, extensions) in ALL_EXTENSIONS.iter() {
41+
for (format, extensions) in all_extensions().iter() {
4242
if extensions.contains(
4343
&filename
4444
.extension()
@@ -75,7 +75,7 @@ impl FileSourceFile {
7575
}
7676

7777
None => {
78-
for format in ALL_EXTENSIONS.keys() {
78+
for format in all_extensions().keys() {
7979
for ext in format.extensions() {
8080
filename.set_extension(ext);
8181

0 commit comments

Comments
 (0)