From e9bea839b363534b5d6a7d504cf96003271f4703 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Sat, 25 Mar 2023 17:37:12 +0100 Subject: [PATCH] Replace lazy_static with once_cell --- Cargo.lock | 6 ++-- Cargo.toml | 2 +- packages/cli/Cargo.toml | 2 +- packages/cli/src/gen/docs_file.rs | 44 ++++++++++++------------ packages/cli/src/utils/files.rs | 10 +++--- packages/cli/src/utils/listing.rs | 8 ++--- packages/lib-roblox/Cargo.toml | 2 +- packages/lib-roblox/src/instance/mod.rs | 7 ++-- packages/lib/Cargo.toml | 2 +- packages/lib/src/lua/stdio/formatting.rs | 29 ++++++++-------- 10 files changed, 54 insertions(+), 58 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 000ae20..765239b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1011,9 +1011,9 @@ dependencies = [ "futures-util", "hyper", "hyper-tungstenite", - "lazy_static", "lune-roblox", "mlua", + "once_cell", "os_str_bytes", "pin-project", "rbx_cookie", @@ -1037,8 +1037,8 @@ dependencies = [ "full_moon", "futures-util", "include_dir", - "lazy_static", "lune", + "once_cell", "regex", "serde", "serde_json", @@ -1052,8 +1052,8 @@ version = "0.6.2" dependencies = [ "anyhow", "glam", - "lazy_static", "mlua", + "once_cell", "rand", "rbx_binary", "rbx_dom_weak", diff --git a/Cargo.toml b/Cargo.toml index 72c03a9..e9f8e23 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ categories = ["command-line-interface"] [workspace.dependencies] console = "0.15" futures-util = "0.3" -lazy_static = "1.4" +once_cell = "1.17" mlua = { version = "0.8", features = ["luau", "serialize"] } diff --git a/packages/cli/Cargo.toml b/packages/cli/Cargo.toml index 5d18043..350150a 100644 --- a/packages/cli/Cargo.toml +++ b/packages/cli/Cargo.toml @@ -24,7 +24,7 @@ lune = { path = "../lib" } console.workspace = true futures-util.workspace = true -lazy_static.workspace = true +once_cell.workspace = true serde.workspace = true serde_json.workspace = true serde_yaml.workspace = true diff --git a/packages/cli/src/gen/docs_file.rs b/packages/cli/src/gen/docs_file.rs index c91dd31..5d3a6ea 100644 --- a/packages/cli/src/gen/docs_file.rs +++ b/packages/cli/src/gen/docs_file.rs @@ -1,17 +1,14 @@ use anyhow::{Context, Result}; -use lazy_static::lazy_static; use serde_json::{Map as JsonMap, Value as JsonValue}; use super::definitions::{DefinitionsItem, DefinitionsItemTag, DefinitionsTree}; -lazy_static! { - static ref KEY_DOCUMENTATION: String = "documentation".to_string(); - static ref KEY_KEYS: String = "keys".to_string(); - static ref KEY_NAME: String = "name".to_string(); - static ref KEY_CODE_SAMPLE: String = "code_sample".to_string(); - static ref KEY_LEARN_MORE_LINK: String = "learn_more_link".to_string(); - static ref VALUE_EMPTY: String = String::new(); -} +static KEY_DOCUMENTATION: &str = "documentation"; +static KEY_KEYS: &str = "keys"; +static KEY_NAME: &str = "name"; +static KEY_CODE_SAMPLE: &str = "code_sample"; +static KEY_LEARN_MORE_LINK: &str = "learn_more_link"; +static VALUE_EMPTY: &str = ""; pub fn generate_from_type_definitions(contents: &str, namespace: &str) -> Result { let tree = DefinitionsTree::from_type_definitions(contents)?; @@ -91,24 +88,27 @@ fn parse_and_insert( .context("Missing description value for doc item")? .to_string(), ); - item_map.insert(KEY_DOCUMENTATION.clone(), JsonValue::String(description)); + item_map.insert( + KEY_DOCUMENTATION.to_string(), + JsonValue::String(description), + ); if let Some(code_sample) = code_sample { - item_map.insert(KEY_CODE_SAMPLE.clone(), JsonValue::String(code_sample)); + item_map.insert(KEY_CODE_SAMPLE.to_string(), JsonValue::String(code_sample)); } else { item_map.insert( - KEY_CODE_SAMPLE.clone(), - JsonValue::String(VALUE_EMPTY.clone()), + KEY_CODE_SAMPLE.to_string(), + JsonValue::String(VALUE_EMPTY.to_string()), ); } if let Some(learn_more_link) = learn_more_link { item_map.insert( - KEY_LEARN_MORE_LINK.clone(), + KEY_LEARN_MORE_LINK.to_string(), JsonValue::String(learn_more_link), ); } else { item_map.insert( - KEY_LEARN_MORE_LINK.clone(), - JsonValue::String(VALUE_EMPTY.clone()), + KEY_LEARN_MORE_LINK.to_string(), + JsonValue::String(VALUE_EMPTY.to_string()), ); } } @@ -134,7 +134,7 @@ fn parse_and_insert( }) .collect::>(); if keys.is_empty() { - item_map.insert("keys".to_string(), JsonValue::Object(JsonMap::new())); + item_map.insert(KEY_KEYS.to_string(), JsonValue::Object(JsonMap::new())); } else { let mut keys_map = JsonMap::new(); for key in keys.drain(..) { @@ -143,7 +143,7 @@ fn parse_and_insert( JsonValue::String(format!("@{namespace}/{item_name_full}.{key}")), ); } - item_map.insert("keys".to_string(), JsonValue::Object(keys_map)); + item_map.insert(KEY_KEYS.to_string(), JsonValue::Object(keys_map)); } } else if item.is_function() { // Add links to params @@ -154,9 +154,9 @@ fn parse_and_insert( for (index, param) in params.iter().enumerate() { let mut param_map = JsonMap::new(); if let DefinitionsItemTag::Param((name, _)) = param { - param_map.insert(KEY_NAME.clone(), JsonValue::String(name.to_string())); + param_map.insert(KEY_NAME.to_string(), JsonValue::String(name.to_string())); param_map.insert( - KEY_DOCUMENTATION.clone(), + KEY_DOCUMENTATION.to_string(), JsonValue::String(format!("@{namespace}/{item_name_full}/param/{index}")), ); } @@ -196,7 +196,7 @@ fn parse_and_insert( let mut param_map = JsonMap::new(); if let DefinitionsItemTag::Param((_, doc)) = param { param_map.insert( - KEY_DOCUMENTATION.clone(), + KEY_DOCUMENTATION.to_string(), JsonValue::String(format!("{doc}\n\n---\n")), ); } @@ -209,7 +209,7 @@ fn parse_and_insert( let mut return_map = JsonMap::new(); if let DefinitionsItemTag::Return(doc) = ret { return_map.insert( - KEY_DOCUMENTATION.clone(), + KEY_DOCUMENTATION.to_string(), JsonValue::String(doc.to_string()), ); } diff --git a/packages/cli/src/utils/files.rs b/packages/cli/src/utils/files.rs index d7548a5..ea25244 100644 --- a/packages/cli/src/utils/files.rs +++ b/packages/cli/src/utils/files.rs @@ -5,12 +5,12 @@ use std::{ use anyhow::{anyhow, Result}; use console::style; -use lazy_static::lazy_static; +use once_cell::sync::Lazy; const LUNE_COMMENT_PREFIX: &str = "-->"; -lazy_static! { - static ref ERR_MESSAGE_HELP_NOTE: String = format!( +static ERR_MESSAGE_HELP_NOTE: Lazy = Lazy::new(|| { + format!( "To run this file, either:\n{}\n{}", format_args!( "{} rename it to use a {} or {} extension", @@ -22,8 +22,8 @@ lazy_static! { "{} pass it as an absolute path instead of relative", style("-").dim() ), - ); -} + ) +}); /** Discovers a script file path based on a given script name. diff --git a/packages/cli/src/utils/listing.rs b/packages/cli/src/utils/listing.rs index 23d43c8..867d871 100644 --- a/packages/cli/src/utils/listing.rs +++ b/packages/cli/src/utils/listing.rs @@ -2,15 +2,13 @@ use std::{cmp::Ordering, fmt::Write as _}; use anyhow::{bail, Result}; use console::Style; -use lazy_static::lazy_static; +use once_cell::sync::Lazy; use tokio::{fs, io}; use super::files::parse_lune_description_from_file; -lazy_static! { - pub static ref COLOR_BLUE: Style = Style::new().blue(); - pub static ref STYLE_DIM: Style = Style::new().dim(); -} +pub static COLOR_BLUE: Lazy