[fractal] Backend: Use lazy_static! for Regex evaluation. Closes #391
- From: Daniel Garcia Moreno <danigm src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [fractal] Backend: Use lazy_static! for Regex evaluation. Closes #391
- Date: Wed, 2 Jan 2019 16:59:50 +0000 (UTC)
commit e52fca475cc4b0ed1db1a1acfba18fd1159590a0
Author: Alejandro DomÃnguez <adomu net-c com>
Date: Sat Dec 29 22:56:12 2018 +0100
Backend: Use lazy_static! for Regex evaluation. Closes #391
Cargo.lock | 1 +
fractal-matrix-api/Cargo.toml | 1 +
fractal-matrix-api/src/backend/register.rs | 6 +-----
fractal-matrix-api/src/globals.rs | 10 ++++++++++
fractal-matrix-api/src/lib.rs | 2 ++
fractal-matrix-api/src/util.rs | 6 +++---
6 files changed, 18 insertions(+), 8 deletions(-)
---
diff --git a/Cargo.lock b/Cargo.lock
index 792d923f..b85a9882 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -480,6 +480,7 @@ dependencies = [
"cairo-rs 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"glib 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"md5 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
diff --git a/fractal-matrix-api/Cargo.toml b/fractal-matrix-api/Cargo.toml
index 014de965..83de5a93 100644
--- a/fractal-matrix-api/Cargo.toml
+++ b/fractal-matrix-api/Cargo.toml
@@ -27,6 +27,7 @@ tree_magic = "0.2.1"
url = "1.7.2"
urlencoding = "1.0.0"
md5 = "0.6.1"
+lazy_static = "1.2.0"
[dependencies.cairo-rs]
features = ["png"]
diff --git a/fractal-matrix-api/src/backend/register.rs b/fractal-matrix-api/src/backend/register.rs
index 95bd71a9..8623dfc4 100644
--- a/fractal-matrix-api/src/backend/register.rs
+++ b/fractal-matrix-api/src/backend/register.rs
@@ -40,12 +40,8 @@ pub fn guest(bk: &Backend, server: &str) -> Result<(), Error> {
}
fn build_login_attrs(user: &str, password: &str) -> Result<JsonValue, Error> {
- let emailre = Regex::new(
- r"^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])+@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$",
- )?;
-
// Email
- let attrs = if emailre.is_match(&user) {
+ let attrs = if globals::EMAIL_RE.is_match(&user) {
json!({
"type": "m.login.password",
"password": password,
diff --git a/fractal-matrix-api/src/globals.rs b/fractal-matrix-api/src/globals.rs
index 4e0316c1..41156dbf 100644
--- a/fractal-matrix-api/src/globals.rs
+++ b/fractal-matrix-api/src/globals.rs
@@ -1,4 +1,14 @@
+use regex::Regex;
+
pub static TIMEOUT: u64 = 80;
pub static PAGE_LIMIT: i32 = 40;
pub static ROOM_DIRECTORY_LIMIT: i32 = 20;
pub static THUMBNAIL_SIZE: i32 = 128;
+
+lazy_static! {
+ pub static ref MATRIX_RE: Regex = Regex::new(r"mxc://(?P<server>[^/]+)/(?P<media>.+)").unwrap();
+ pub static ref EMAIL_RE: Regex = Regex::new(
+ r"^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])+@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"
+ )
+ .unwrap();
+}
diff --git a/fractal-matrix-api/src/lib.rs b/fractal-matrix-api/src/lib.rs
index 52ea8f89..45ae9b65 100644
--- a/fractal-matrix-api/src/lib.rs
+++ b/fractal-matrix-api/src/lib.rs
@@ -6,6 +6,8 @@ extern crate serde_json;
extern crate serde_derive;
#[macro_use]
extern crate log;
+#[macro_use]
+extern crate lazy_static;
extern crate cairo;
extern crate chrono;
diff --git a/fractal-matrix-api/src/util.rs b/fractal-matrix-api/src/util.rs
index 8bb25cb0..91242d79 100644
--- a/fractal-matrix-api/src/util.rs
+++ b/fractal-matrix-api/src/util.rs
@@ -1,4 +1,3 @@
-use regex::Regex;
use reqwest;
use serde_json::Value as JsonValue;
@@ -507,8 +506,9 @@ pub fn put_media(url: &str, file: Vec<u8>) -> Result<JsonValue, Error> {
}
pub fn resolve_media_url(base: &Url, url: &str, thumb: bool, w: i32, h: i32) -> Result<Url, Error> {
- let re = Regex::new(r"mxc://(?P<server>[^/]+)/(?P<media>.+)")?;
- let caps = re.captures(url).ok_or(Error::BackendError)?;
+ let caps = globals::MATRIX_RE
+ .captures(url)
+ .ok_or(Error::BackendError)?;
let server = String::from(&caps["server"]);
let media = String::from(&caps["media"]);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]