Skip to content

Commit

Permalink
refactor: Drop lazy_static dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Feb 1, 2024
1 parent 929e1be commit d98634d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 9 additions & 11 deletions src/file/format/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// If no features are used, there is an "unused mut" warning in `ALL_EXTENSIONS`
// BUG: ? For some reason this doesn't do anything if I try and function scope this
#![allow(unused_mut)]

use std::collections::HashMap;
use std::error::Error;
use std::sync::OnceLock;

use crate::map::Map;
use crate::{file::FileStoredFormat, value::Value, Format};
Expand Down Expand Up @@ -56,10 +53,11 @@ pub enum FileFormat {
Json5,
}

lazy_static! {
#[doc(hidden)]
// #[allow(unused_mut)] ?
pub static ref ALL_EXTENSIONS: HashMap<FileFormat, Vec<&'static str>> = {
pub(crate) fn all_extensions() -> &'static HashMap<FileFormat, Vec<&'static str>> {
#![allow(unused_mut)] // If no features are used, there is an "unused mut" warning in `all_extensions`

static ALL_EXTENSIONS: OnceLock<HashMap<FileFormat, Vec<&'static str>>> = OnceLock::new();
ALL_EXTENSIONS.get_or_init(|| {
let mut formats: HashMap<FileFormat, Vec<_>> = HashMap::new();

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

formats
};
})
}

impl FileFormat {
pub(crate) fn extensions(&self) -> &'static [&'static str] {
// It should not be possible for this to fail
// A FileFormat would need to be declared without being added to the
// ALL_EXTENSIONS map.
ALL_EXTENSIONS.get(self).unwrap()
// all_extensions map.
all_extensions().get(self).unwrap()
}

pub(crate) fn parse(
Expand Down
6 changes: 3 additions & 3 deletions src/file/source/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::io;
use std::path::PathBuf;

use crate::file::{
format::ALL_EXTENSIONS, source::FileSourceResult, FileSource, FileStoredFormat, Format,
format::all_extensions, source::FileSourceResult, FileSource, FileStoredFormat, Format,
};

/// Describes a file sourced from a file
Expand Down Expand Up @@ -38,7 +38,7 @@ impl FileSourceFile {
return if let Some(format) = format_hint {
Ok((filename, Box::new(format)))
} else {
for (format, extensions) in ALL_EXTENSIONS.iter() {
for (format, extensions) in all_extensions().iter() {
if extensions.contains(
&filename
.extension()
Expand Down Expand Up @@ -75,7 +75,7 @@ impl FileSourceFile {
}

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

Expand Down

0 comments on commit d98634d

Please sign in to comment.