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

Use rust stable OnceLock, drop OnceCell crate #181

Merged
merged 2 commits into from
Aug 3, 2023
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
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ categories = ["development-tools", "wasm", "web-programming"]
keywords = ["leptos"]
version = "0.1.12-alpha"
edition = "2021"
# OnceLock was stabailzed in 1.70
rust-version = "1.70"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand All @@ -44,7 +46,7 @@ cargo_metadata = { version = "0.15", features = ["builder"] }
serde_json = "1.0"
wasm-bindgen-cli-support = "0.2"
ansi_term = "0.12"
once_cell = "1.16"

seahash = "4.1"
reqwest = { version = "0.11", features = [
"blocking",
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub async fn run(args: Cli) -> Result<()> {
let verbose = args.opts().map(|o| o.verbose).unwrap_or(0);
logger::setup(verbose, &args.log);

if let Commands::New(new) = &args.command {
if let New(new) = &args.command {
return new.run().await;
}

Expand All @@ -33,7 +33,7 @@ pub async fn run(args: Cli) -> Result<()> {
.unwrap_or_else(|| Utf8PathBuf::from("Cargo.toml"))
.resolve_home_dir()
.context(format!("manifest_path: {:?}", &args.manifest_path))?;
let mut cwd = Utf8PathBuf::from_path_buf(std::env::current_dir().unwrap()).unwrap();
let mut cwd = Utf8PathBuf::from_path_buf(env::current_dir().unwrap()).unwrap();
cwd.clean_windows_path();

let opts = args.opts().unwrap();
Expand Down
18 changes: 11 additions & 7 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ use flexi_logger::{
filter::{LogLineFilter, LogLineWriter},
DeferredNow, Level, Record,
};
use once_cell::sync::OnceCell;
use std::io::Write;
use std::sync::OnceLock;

use crate::{config::Log, ext::StrAdditions};
use crate::ext::anyhow::Context;

// https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
lazy_static::lazy_static! {
Expand All @@ -18,7 +19,7 @@ lazy_static::lazy_static! {

pub static ref GRAY: ansi_term::Color = Fixed(241);
pub static ref BOLD: ansi_term::Style = Style::new().bold();
static ref LOG_SELECT: OnceCell<LogFlag> = OnceCell::new();
static ref LOG_SELECT: OnceLock<LogFlag> = OnceLock::new();
}

pub fn setup(verbose: u8, logs: &[Log]) {
Expand All @@ -28,16 +29,18 @@ pub fn setup(verbose: u8, logs: &[Log]) {
_ => "trace",
};

if LOG_SELECT.get().is_none() {
// OnceLock::get_or_try_init() is more idiomatic, but unstable at the moment
_ = LOG_SELECT.get_or_init(|| {
flexi_logger::Logger::try_with_str(log_level)
.with_context(|| "Logger setup failed")
.unwrap()
.filter(Box::new(Filter))
.format(format)
.start()
.unwrap();

LOG_SELECT.set(LogFlag::new(logs)).unwrap();
}
LogFlag::new(logs)
});
}

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -117,13 +120,14 @@ impl LogLineFilter for Filter {
fn write(
&self,
now: &mut DeferredNow,
record: &log::Record,
record: &Record,
log_line_writer: &dyn LogLineWriter,
) -> std::io::Result<()> {
let target = record.target();
if record.level() == Level::Error
|| target.starts_with("cargo_leptos")
|| LOG_SELECT.get().unwrap().matches(target)
// LOG_SELECT will have been initialized by now, get_or_init() not required
|| LOG_SELECT.get().is_some_and(|flag| flag.matches(target))
{
log_line_writer.write(now, record)?;
}
Expand Down
Loading