Use once_cell instead of lazy_static

This commit is contained in:
Konrad Borowski 2019-12-11 15:05:14 +01:00
parent 280212bc78
commit e9765acc36
5 changed files with 44 additions and 44 deletions

8
Cargo.lock generated
View File

@ -1129,6 +1129,11 @@ dependencies = [
"libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "once_cell"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "opaque-debug" name = "opaque-debug"
version = "0.2.3" version = "0.2.3"
@ -1223,9 +1228,9 @@ dependencies = [
"futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"mime 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "mime 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
"once_cell 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"pulldown-cmark 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", "pulldown-cmark 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
"reqwest 0.9.22 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.9.22 (registry+https://github.com/rust-lang/crates.io-index)",
@ -2535,6 +2540,7 @@ dependencies = [
"checksum num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "b85e541ef8255f6cf42bbfe4ef361305c6c135d10919ecc26126c4e5ae94bc09" "checksum num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "b85e541ef8255f6cf42bbfe4ef361305c6c135d10919ecc26126c4e5ae94bc09"
"checksum num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "d4c81ffc11c212fa327657cb19dd85eb7419e163b5b076bede2bdb5c974c07e4" "checksum num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "d4c81ffc11c212fa327657cb19dd85eb7419e163b5b076bede2bdb5c974c07e4"
"checksum num_cpus 1.11.1 (registry+https://github.com/rust-lang/crates.io-index)" = "76dac5ed2a876980778b8b85f75a71b6cbf0db0b1232ee12f826bccb00d09d72" "checksum num_cpus 1.11.1 (registry+https://github.com/rust-lang/crates.io-index)" = "76dac5ed2a876980778b8b85f75a71b6cbf0db0b1232ee12f826bccb00d09d72"
"checksum once_cell 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "891f486f630e5c5a4916c7e16c4b24a53e78c860b646e9f8e005e4f16847bfed"
"checksum opaque-debug 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c" "checksum opaque-debug 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c"
"checksum openssl 0.10.26 (registry+https://github.com/rust-lang/crates.io-index)" = "3a3cc5799d98e1088141b8e01ff760112bbd9f19d850c124500566ca6901a585" "checksum openssl 0.10.26 (registry+https://github.com/rust-lang/crates.io-index)" = "3a3cc5799d98e1088141b8e01ff760112bbd9f19d850c124500566ca6901a585"
"checksum openssl-probe 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "77af24da69f9d9341038eba93a073b1fdaaa1b788221b00a69bce9e762cb32de" "checksum openssl-probe 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "77af24da69f9d9341038eba93a073b1fdaaa1b788221b00a69bce9e762cb32de"

View File

@ -16,9 +16,9 @@ env_logger = { version = "0.6.0", default-features = false }
futures = "0.1.28" futures = "0.1.28"
futures03 = { version = "0.3.1", package = "futures", features = ["compat"] } futures03 = { version = "0.3.1", package = "futures", features = ["compat"] }
itertools = "0.8.1" itertools = "0.8.1"
lazy_static = "1.3.0"
log = "0.4.6" log = "0.4.6"
mime = "0.3.13" mime = "0.3.13"
once_cell = "1.2.0"
pulldown-cmark = "0.5.0" pulldown-cmark = "0.5.0"
rand = "0.7.0" rand = "0.7.0"
reqwest = "0.9.19" reqwest = "0.9.19"

View File

@ -5,8 +5,8 @@ use ammonia::Builder;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use diesel::prelude::*; use diesel::prelude::*;
use itertools::Itertools; use itertools::Itertools;
use lazy_static::lazy_static;
use log::info; use log::info;
use once_cell::sync::Lazy;
use pulldown_cmark::{Options, Parser}; use pulldown_cmark::{Options, Parser};
use rand::seq::SliceRandom; use rand::seq::SliceRandom;
use std::iter; use std::iter;
@ -144,27 +144,25 @@ impl ExternPaste {
} }
fn render_markdown(markdown: &str) -> String { fn render_markdown(markdown: &str) -> String {
lazy_static! { static FILTER: Lazy<Builder<'static>> = Lazy::new(|| {
static ref FILTER: Builder<'static> = { let mut builder = Builder::new();
let mut builder = Builder::new(); builder.link_rel(Some("noopener noreferrer nofollow"));
builder.link_rel(Some("noopener noreferrer nofollow")); builder.add_generic_attributes(iter::once("class"));
builder.add_generic_attributes(iter::once("class")); builder.attribute_filter(|_, attribute, value| {
builder.attribute_filter(|_, attribute, value| { if attribute == "class" {
if attribute == "class" { Some(
Some( value
value .split_ascii_whitespace()
.split_ascii_whitespace() .filter(|value| value.starts_with("language-"))
.filter(|value| value.starts_with("language-")) .join(" ")
.join(" ") .into(),
.into(), )
) } else {
} else { Some(value.into())
Some(value.into()) }
} });
}); builder
builder });
};
}
let mut output = String::new(); let mut output = String::new();
let options = Options::ENABLE_TABLES | Options::ENABLE_STRIKETHROUGH; let options = Options::ENABLE_TABLES | Options::ENABLE_STRIKETHROUGH;
pulldown_cmark::html::push_html(&mut output, Parser::new_ext(markdown, options)); pulldown_cmark::html::push_html(&mut output, Parser::new_ext(markdown, options));

View File

@ -213,7 +213,7 @@ mod test {
use crate::migration; use crate::migration;
use diesel::r2d2::{ConnectionManager, CustomizeConnection, Pool}; use diesel::r2d2::{ConnectionManager, CustomizeConnection, Pool};
use diesel::Connection; use diesel::Connection;
use lazy_static::lazy_static; use once_cell::sync::Lazy;
use scraper::{Html, Selector}; use scraper::{Html, Selector};
use serde::Deserialize; use serde::Deserialize;
use std::env; use std::env;
@ -224,20 +224,18 @@ mod test {
use warp::reply::{Reply, Response}; use warp::reply::{Reply, Response};
use warp::Filter; use warp::Filter;
lazy_static! { static ROUTES: Lazy<BoxedFilter<(Response,)>> = Lazy::new(|| {
static ref ROUTES: BoxedFilter<(Response,)> = { let pool = Pool::builder()
let pool = Pool::builder() .connection_customizer(Box::new(ExecuteWithinTransaction))
.connection_customizer(Box::new(ExecuteWithinTransaction)) .max_size(1)
.max_size(1) .build(ConnectionManager::new(env::var("DATABASE_URL").expect(
.build(ConnectionManager::new(env::var("DATABASE_URL").expect( "Setting DATABASE_URL environment variable required to run tests",
"Setting DATABASE_URL environment variable required to run tests", )))
))) .expect("Couldn't create a connection connection");
.expect("Couldn't create a connection connection"); diesel_migrations::run_pending_migrations(&pool.get().unwrap()).unwrap();
diesel_migrations::run_pending_migrations(&pool.get().unwrap()).unwrap(); migration::run(&pool.get().unwrap()).unwrap();
migration::run(&pool.get().unwrap()).unwrap(); routes(pool).map(Reply::into_response).boxed()
routes(pool).map(Reply::into_response).boxed() });
};
}
#[derive(Debug)] #[derive(Debug)]
struct ExecuteWithinTransaction; struct ExecuteWithinTransaction;

View File

@ -3,17 +3,15 @@ use crate::Connection;
use diesel::prelude::*; use diesel::prelude::*;
use futures::Future; use futures::Future;
use futures03::TryFutureExt; use futures03::TryFutureExt;
use lazy_static::lazy_static; use once_cell::sync::Lazy;
use reqwest::r#async::Client; use reqwest::r#async::Client;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::env; use std::env;
use tokio_executor::blocking; use tokio_executor::blocking;
use warp::{Rejection, Reply}; use warp::{Rejection, Reply};
lazy_static! { static CLIENT: Lazy<Client> = Lazy::new(Client::new);
static ref CLIENT: Client = Client::new(); static SANDBOX_URL: Lazy<String> = Lazy::new(|| env::var("SANDBOX_URL").unwrap());
static ref SANDBOX_URL: String = env::var("SANDBOX_URL").unwrap();
}
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]