Replace lazy_static with once_cell

This commit is contained in:
Filip Tibell 2023-03-25 17:37:12 +01:00
parent 191bbf15bb
commit e9bea839b3
No known key found for this signature in database
10 changed files with 54 additions and 58 deletions

6
Cargo.lock generated
View file

@ -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",

View file

@ -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"] }

View file

@ -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

View file

@ -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<String> {
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::<Vec<_>>();
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()),
);
}

View file

@ -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<String> = 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.

View file

@ -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<Style> = Lazy::new(|| Style::new().blue());
pub static STYLE_DIM: Lazy<Style> = Lazy::new(|| Style::new().dim());
pub async fn find_lune_scripts() -> Result<Vec<(String, String)>> {
let mut lune_dir = fs::read_dir("lune").await;

View file

@ -16,7 +16,7 @@ path = "src/lib.rs"
[dependencies]
mlua.workspace = true
lazy_static.workspace = true
once_cell.workspace = true
glam = "0.23"
rand = "0.8"

View file

@ -5,6 +5,7 @@ use std::{
};
use mlua::prelude::*;
use once_cell::sync::Lazy;
use rbx_dom_weak::{
types::{Ref as DomRef, Variant as DomValue, VariantType as DomType},
Instance as DomInstance, InstanceBuilder as DomInstanceBuilder, WeakDom,
@ -22,10 +23,8 @@ use crate::{
pub(crate) mod data_model;
lazy_static::lazy_static! {
static ref INTERNAL_DOM: RwLock<WeakDom> =
RwLock::new(WeakDom::new(DomInstanceBuilder::new("ROOT")));
}
static INTERNAL_DOM: Lazy<RwLock<WeakDom>> =
Lazy::new(|| RwLock::new(WeakDom::new(DomInstanceBuilder::new("ROOT"))));
#[derive(Debug, Clone)]
pub struct Instance {

View file

@ -24,8 +24,8 @@ rbx_cookie = { version = "0.1.2", optional = true }
console.workspace = true
futures-util.workspace = true
lazy_static.workspace = true
mlua.workspace = true
once_cell.workspace = true
serde.workspace = true
serde_json.workspace = true
serde_yaml.workspace = true

View file

@ -1,8 +1,8 @@
use std::fmt::Write;
use console::{colors_enabled, set_colors_enabled, style, Style};
use lazy_static::lazy_static;
use mlua::prelude::*;
use once_cell::sync::Lazy;
use crate::lua::task::TaskReference;
@ -12,20 +12,19 @@ const INDENT: &str = " ";
pub const STYLE_RESET_STR: &str = "\x1b[0m";
lazy_static! {
// Colors
pub static ref COLOR_BLACK: Style = Style::new().black();
pub static ref COLOR_RED: Style = Style::new().red();
pub static ref COLOR_GREEN: Style = Style::new().green();
pub static ref COLOR_YELLOW: Style = Style::new().yellow();
pub static ref COLOR_BLUE: Style = Style::new().blue();
pub static ref COLOR_PURPLE: Style = Style::new().magenta();
pub static ref COLOR_CYAN: Style = Style::new().cyan();
pub static ref COLOR_WHITE: Style = Style::new().white();
// Styles
pub static ref STYLE_BOLD: Style = Style::new().bold();
pub static ref STYLE_DIM: Style = Style::new().dim();
}
// Colors
pub static COLOR_BLACK: Lazy<Style> = Lazy::new(|| Style::new().black());
pub static COLOR_RED: Lazy<Style> = Lazy::new(|| Style::new().red());
pub static COLOR_GREEN: Lazy<Style> = Lazy::new(|| Style::new().green());
pub static COLOR_YELLOW: Lazy<Style> = Lazy::new(|| Style::new().yellow());
pub static COLOR_BLUE: Lazy<Style> = Lazy::new(|| Style::new().blue());
pub static COLOR_PURPLE: Lazy<Style> = Lazy::new(|| Style::new().magenta());
pub static COLOR_CYAN: Lazy<Style> = Lazy::new(|| Style::new().cyan());
pub static COLOR_WHITE: Lazy<Style> = Lazy::new(|| Style::new().white());
// Styles
pub static STYLE_BOLD: Lazy<Style> = Lazy::new(|| Style::new().bold());
pub static STYLE_DIM: Lazy<Style> = Lazy::new(|| Style::new().dim());
fn can_be_plain_lua_table_key(s: &LuaString) -> bool {
let str = s.to_string_lossy().to_string();