Skip to content

Commit

Permalink
refactor: Replace lazy_static with std from 1.80
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Gil <[email protected]>
  • Loading branch information
pando85 committed Jul 26, 2024
1 parent 2a8ee45 commit b6feb95
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 274 deletions.
441 changes: 213 additions & 228 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ resolver = "2"
[workspace.package]
version = "1.10.5"
authors = ["Pando85 <[email protected]>"]
rust-version = "1.74"
rust-version = "1.80"
edition = "2021"
license-file = "LICENSE"
homepage = "https://rash.sh"
Expand All @@ -15,7 +15,6 @@ readme = "README.md"

[workspace.dependencies]
clap = "4.5.4"
lazy_static = "1.4.0"
log = "0.4.21"
regex = "1.10.4"
schemars = "0.8.17"
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG BASE_IMAGE=rust:1.79.0
ARG BASE_IMAGE=rust:1.80.0
FROM ${BASE_IMAGE} AS builder
LABEL mantainer [email protected]

Expand Down
1 change: 0 additions & 1 deletion mdbook_rash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ doc = false

[dependencies]
rash_core = { path = "../rash_core", features = ["docs"], version = "1.10.5" }
lazy_static.workspace = true
log.workspace = true
regex.workspace = true
schemars.workspace = true
Expand Down
19 changes: 11 additions & 8 deletions mdbook_rash/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use rash_core::modules::MODULES;

use std::sync::LazyLock;

use mdbook::book::{Book, BookItem, Chapter};
use mdbook::errors::Error;
use mdbook::preprocess::{LinkPreprocessor, Preprocessor, PreprocessorContext};
Expand All @@ -9,31 +11,32 @@ use schemars::schema::{RootSchema, SingleOrVec};

#[macro_use]
extern crate log;
#[macro_use]
extern crate lazy_static;

pub static SUPPORTED_RENDERER: &[&str] = &["markdown"];

lazy_static! {
static ref RE: Regex = Regex::new(
static RE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(
r#"(?x) # insignificant whitespace mode
\{\s* # link opening parens and whitespace
\$([a-zA-Z0-9_]+) # link type
(?:\s+ # separating whitespace
([a-zA-Z0-9\s_.,\*\{\}\[\]\(\)\|'\-\\/`"\#+=:/\\]+))? # all doc
\s*\} # whitespace and link closing parens"#
)
.unwrap();
static ref FORMAT: format::TableFormat = format::FormatBuilder::new()
.unwrap()
});

static FORMAT: LazyLock<format::TableFormat> = LazyLock::new(|| {
format::FormatBuilder::new()
.padding(1, 1)
.borders('|')
.separator(
format::LinePosition::Title,
format::LineSeparator::new('-', '|', '|', '|'),
)
.column_separator('|')
.build();
}
.build()
});

fn get_matches(ch: &Chapter) -> Option<Vec<(Match, Option<String>, String)>> {
RE.captures_iter(&ch.content)
Expand Down
1 change: 0 additions & 1 deletion rash_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ docs = ["rash_derive/docs", "schemars"]

[dependencies]
rash_derive = { path = "../rash_derive", version = "1.10.5" }
lazy_static.workspace = true
log.workspace = true
regex.workspace = true
schemars = { workspace = true, optional = true }
Expand Down
6 changes: 3 additions & 3 deletions rash_core/src/docopt/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ use crate::utils::merge_json;
use crate::vars::Vars;

use std::collections::HashSet;
use std::sync::LazyLock;

use itertools::Itertools;
use regex::Regex;
use tera::Context;

const OPTIONS_MARK: &str = "[options]";

lazy_static! {
static ref RE_DEFAULT_VALUE: Regex = Regex::new(r"\[default: (.*)\]").unwrap();
}
static RE_DEFAULT_VALUE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\[default: (.*)\]").unwrap());

#[derive(Clone, Debug, Hash, Eq, PartialEq)]
pub enum OptionArg {
Expand Down
20 changes: 12 additions & 8 deletions rash_core/src/docopt/utils.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
use itertools::Itertools;
use lazy_static;
use regex::{Captures, Regex};

use std::sync::LazyLock;

pub const WORDS_REGEX: &str = r"[a-z]+(?:[_\-][a-z]+)*";
pub const WORDS_UPPERCASE_REGEX: &str = r"[A-Z]+(?:[_\-][A-Z]+)*";

lazy_static! {
static ref RE_INNER_PARENTHESIS: Regex = Regex::new(r"\(([^\(]+?)\)(\.\.\.)?").unwrap();
static ref RE_INNER_BRACKETS: Regex = Regex::new(r"\[([^\[]+?)\](\.\.\.)?").unwrap();
static ref RE_INNER_CURLY_BRACES: Regex = Regex::new(r"(\{[^\[]+?\})(\.\.\.)?").unwrap();
static ref RE_REPEATABLE: Regex = Regex::new(&format!(
static RE_INNER_PARENTHESIS: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\(([^\(]+?)\)(\.\.\.)?").unwrap());
static RE_INNER_BRACKETS: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\[([^\[]+?)\](\.\.\.)?").unwrap());
static RE_INNER_CURLY_BRACES: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"(\{[^\[]+?\})(\.\.\.)?").unwrap());
static RE_REPEATABLE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(&format!(
r"(<{WORDS_REGEX}>|{WORDS_UPPERCASE_REGEX})\x20?(\.\.\.)"
))
.unwrap();
}
.unwrap()
});

#[derive(Debug, Clone, Copy)]
pub enum RegexMatch {
Expand Down
2 changes: 0 additions & 2 deletions rash_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ pub mod task;
pub mod utils;
pub mod vars;

#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate log;
#[macro_use]
Expand Down
33 changes: 16 additions & 17 deletions rash_core/src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::modules::template::Template;
use crate::vars::Vars;

use std::collections::HashMap;
use std::sync::LazyLock;

#[cfg(feature = "docs")]
use schemars::schema::RootSchema;
Expand Down Expand Up @@ -79,23 +80,21 @@ pub trait Module: Send + Sync + std::fmt::Debug {
fn get_json_schema(&self) -> Option<RootSchema>;
}

lazy_static! {
pub static ref MODULES: HashMap<&'static str, Box<dyn Module>> = {
vec![
(Assert.get_name(), Box::new(Assert) as Box<dyn Module>),
(Command.get_name(), Box::new(Command) as Box<dyn Module>),
(Copy.get_name(), Box::new(Copy) as Box<dyn Module>),
(Debug.get_name(), Box::new(Debug) as Box<dyn Module>),
(File.get_name(), Box::new(File) as Box<dyn Module>),
(Find.get_name(), Box::new(Find) as Box<dyn Module>),
(Pacman.get_name(), Box::new(Pacman) as Box<dyn Module>),
(SetVars.get_name(), Box::new(SetVars) as Box<dyn Module>),
(Template.get_name(), Box::new(Template) as Box<dyn Module>),
]
.into_iter()
.collect()
};
}
pub static MODULES: LazyLock<HashMap<&'static str, Box<dyn Module>>> = LazyLock::new(|| {
vec![
(Assert.get_name(), Box::new(Assert) as Box<dyn Module>),
(Command.get_name(), Box::new(Command) as Box<dyn Module>),
(Copy.get_name(), Box::new(Copy) as Box<dyn Module>),
(Debug.get_name(), Box::new(Debug) as Box<dyn Module>),
(File.get_name(), Box::new(File) as Box<dyn Module>),
(Find.get_name(), Box::new(Find) as Box<dyn Module>),
(Pacman.get_name(), Box::new(Pacman) as Box<dyn Module>),
(SetVars.get_name(), Box::new(SetVars) as Box<dyn Module>),
(Template.get_name(), Box::new(Template) as Box<dyn Module>),
]
.into_iter()
.collect()
});

#[inline(always)]
pub fn is_module(module: &str) -> bool {
Expand Down
5 changes: 2 additions & 3 deletions rash_core/src/utils/tera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::vars::Vars;

use std::collections::HashMap;
use std::error::Error as StdError;
use std::sync::LazyLock;

use serde_yaml::value::Value;
use tera::Tera;
Expand All @@ -21,9 +22,7 @@ fn init_tera() -> Tera {
tera
}

lazy_static! {
static ref TERA: Tera = init_tera();
}
static TERA: LazyLock<Tera> = LazyLock::new(init_tera);

#[inline(always)]
pub fn render(value: Value, vars: &Vars) -> Result<Value> {
Expand Down

0 comments on commit b6feb95

Please sign in to comment.