Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Drop lazy_static dependency #515

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ preserve_order = ["indexmap", "toml?/preserve_order", "serde_json?/preserve_orde
async = ["async-trait"]

[dependencies]
lazy_static = "1.4"
serde = "1.0"
nom = "7"

Expand Down
11 changes: 6 additions & 5 deletions examples/global/main.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#![allow(deprecated)]
use config::Config;
use lazy_static::lazy_static;
use std::error::Error;
use std::sync::OnceLock;
use std::sync::RwLock;

lazy_static! {
static ref SETTINGS: RwLock<Config> = RwLock::new(Config::default());
fn settings() -> &'static RwLock<Config> {
static CONFIG: OnceLock<RwLock<Config>> = OnceLock::new();
CONFIG.get_or_init(|| RwLock::new(Config::default()))
}

fn try_main() -> Result<(), Box<dyn Error>> {
// Set property
SETTINGS.write()?.set("property", 42)?;
settings().write()?.set("property", 42)?;

// Get property
println!("property: {}", SETTINGS.read()?.get::<i32>("property")?);
println!("property: {}", settings().read()?.get::<i32>("property")?);

Ok(())
}
Expand Down
18 changes: 11 additions & 7 deletions examples/static_env.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
use std::sync::OnceLock;

use config::Config;

lazy_static::lazy_static! {
#[derive(Debug)]
pub static ref CONFIG: Config = Config::builder()
.add_source(config::Environment::with_prefix("APP_NAME").separator("_"))
.build()
.unwrap();
fn config() -> &'static Config {
static CONFIG: OnceLock<Config> = OnceLock::new();
CONFIG.get_or_init(|| {
Config::builder()
.add_source(config::Environment::with_prefix("APP_NAME").separator("_"))
.build()
.unwrap()
})
}

/// Get a configuration value from the static configuration object
pub fn get<'a, T: serde::Deserialize<'a>>(key: &str) -> T {
// You shouldn't probably do it like that and actually handle that error that might happen
// here, but for the sake of simplicity, we do it like this here
CONFIG.get::<T>(key).unwrap()
config().get::<T>(key).unwrap()
}

fn main() {
Expand Down
18 changes: 11 additions & 7 deletions examples/watch/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@ use notify::{Event, RecommendedWatcher, RecursiveMode, Watcher};
use std::collections::HashMap;
use std::path::Path;
use std::sync::mpsc::channel;
use std::sync::OnceLock;
use std::sync::RwLock;
use std::time::Duration;

lazy_static::lazy_static! {
static ref SETTINGS: RwLock<Config> = RwLock::new({
fn settings() -> &'static RwLock<Config> {
static CONFIG: OnceLock<RwLock<Config>> = OnceLock::new();
CONFIG.get_or_init(|| {
let mut settings = Config::default();
settings.merge(File::with_name("examples/watch/Settings.toml")).unwrap();

settings
});
.merge(File::with_name("examples/watch/Settings.toml"))
.unwrap();

RwLock::new(settings)
})
}

fn show() {
println!(
" * Settings :: \n\x1b[31m{:?}\x1b[0m",
SETTINGS
settings()
.read()
.unwrap()
.clone()
Expand Down Expand Up @@ -58,7 +62,7 @@ fn watch() -> ! {
..
})) => {
println!(" * Settings.toml written; refreshing configuration ...");
SETTINGS.write().unwrap().refresh().unwrap();
settings().write().unwrap().refresh().unwrap();
show();
}

Expand Down
21 changes: 9 additions & 12 deletions src/file/format/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +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 lazy_static::lazy_static;
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 @@ -57,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 @@ -82,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
Loading