Skip to content

Commit

Permalink
fix: replace lazy_static with OnceLock
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Kröning <[email protected]>
  • Loading branch information
mkroening committed Mar 5, 2024
1 parent 5a27478 commit 5ed4f97
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 24 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.

1 change: 0 additions & 1 deletion edu-sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ edu-ws = { path = "../edu-ws" }

directories = "5"
filetime = "0.2"
lazy_static = "1.4"
log = "0.4"
regex = "1.5"
reqwest = { version = "0.11", default-features = false, features = ["rustls-tls", "stream"] }
Expand Down
14 changes: 7 additions & 7 deletions edu-sync/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use std::{
fmt::{self, Display},
io::{self, ErrorKind},
path::{Path, PathBuf},
sync::OnceLock,
};

use edu_ws::{
response::{course::Course, info::Info},
token::Token,
ws,
};
use lazy_static::lazy_static;
use log::warn;
use reqwest::Url;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -169,16 +169,16 @@ pub struct Config {
impl Config {
#[must_use]
pub fn path() -> &'static Path {
lazy_static! {
static ref CONFIG_PATH: PathBuf = {
static CONFIG_PATH: OnceLock<PathBuf> = OnceLock::new();

CONFIG_PATH
.get_or_init(|| {
let project_dirs = util::project_dirs();
let mut config_path = project_dirs.config_dir().join(project_dirs.project_path());
config_path.set_extension("toml");
config_path
};
}

CONFIG_PATH.as_path()
})
.as_path()
}

pub fn has_accounts(&self) -> bool {
Expand Down
26 changes: 11 additions & 15 deletions edu-sync/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
use std::{borrow::Cow, ffi::OsStr, mem, path::PathBuf};
use std::{borrow::Cow, ffi::OsStr, mem, path::PathBuf, sync::OnceLock};

use directories::ProjectDirs;
use lazy_static::lazy_static;
use regex::{NoExpand, Regex};

pub fn project_dirs() -> &'static ProjectDirs {
lazy_static! {
static ref PROJECT_DIRS: ProjectDirs = ProjectDirs::from("org", "Edu Sync", "Edu Sync")
.expect("no valid home directory path could be retrieved from the operating system");
}
static PROJECT_DIRS: OnceLock<ProjectDirs> = OnceLock::new();

&PROJECT_DIRS
PROJECT_DIRS.get_or_init(|| {
ProjectDirs::from("org", "Edu Sync", "Edu Sync")
.expect("no valid home directory path could be retrieved from the operating system")
})
}

pub fn shared_http() -> reqwest::Client {
lazy_static! {
static ref SHARED: reqwest::Client = reqwest::Client::new();
}
static SHARED: OnceLock<reqwest::Client> = OnceLock::new();

SHARED.clone()
SHARED.get_or_init(reqwest::Client::new).clone()
}

fn split_file_at_dot(file: &OsStr) -> (&OsStr, Option<&OsStr>) {
Expand Down Expand Up @@ -75,11 +72,10 @@ impl PathBufExt for PathBuf {
}

pub fn sanitize_path_component(path_component: &str) -> Cow<'_, str> {
lazy_static! {
static ref RE: Regex = Regex::new(r"[\\/]+|^\.\.??$").unwrap();
}
static RE: OnceLock<Regex> = OnceLock::new();

RE.replace_all(path_component, NoExpand("_"))
RE.get_or_init(|| Regex::new(r"[\\/]+|^\.\.??$").unwrap())
.replace_all(path_component, NoExpand("_"))
}

#[cfg(test)]
Expand Down

0 comments on commit 5ed4f97

Please sign in to comment.