-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(query-engine-wasm): reduce size of
postgres
Query Engine after …
…regression (#5018) * feat(crosstarget-utils): add Wasm-friendly regex util * fix(query-engine-wasm): reduce size of postgres Driver Adapter via Wasm-friendly regex util * chore: fix clippy * chore: fix clippy * chore: review comments * feat(crosstarget-utils): introduce common trait "RegExprCompact" for "regex" util * chore(crosstarget-utils): rewrite comment * chore(review): use derive_more Display Co-authored-by: Alexey Orlenko <alex@aqrln.net> * fix(crosstarget-utils): don't change the number of captured groups in regex * fix(crosstarget-utils): fixes on regex and derive_more * chore: apply nits * feat(crosstarget-utils): replace every manual "Display" instance with "derive_more" --------- Co-authored-by: Alexey Orlenko <alex@aqrln.net>
- Loading branch information
Showing
20 changed files
with
156 additions
and
33 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod regex; | ||
pub mod spawn; | ||
pub mod timeout; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use derive_more::Display; | ||
|
||
#[derive(Debug, Display)] | ||
#[display(fmt = "Regular expression error: {message}")] | ||
pub struct RegExpError { | ||
pub message: String, | ||
} | ||
|
||
impl std::error::Error for RegExpError {} | ||
|
||
/// Flag modifiers for regular expressions. | ||
#[enumflags2::bitflags] | ||
#[derive(Copy, Clone, Debug, PartialEq)] | ||
#[repr(u8)] | ||
pub enum RegExpFlags { | ||
IgnoreCase = 0b0001, | ||
Multiline = 0b0010, | ||
} | ||
|
||
impl RegExpFlags { | ||
pub fn as_str(&self) -> &'static str { | ||
match self { | ||
Self::IgnoreCase => "i", | ||
Self::Multiline => "m", | ||
} | ||
} | ||
} | ||
|
||
pub trait RegExpCompat { | ||
/// Searches for the first match of this regex in the haystack given, and if found, | ||
/// returns not only the overall match but also the matches of each capture group in the regex. | ||
/// If no match is found, then None is returned. | ||
fn captures(&self, message: &str) -> Option<Vec<String>>; | ||
|
||
/// Tests if the regex matches the input string. | ||
fn test(&self, message: &str) -> bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use derive_more::Display; | ||
|
||
#[derive(Debug, Display)] | ||
#[display(fmt = "Failed to spawn a future")] | ||
|
||
pub struct SpawnError; | ||
|
||
impl std::error::Error for SpawnError {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use derive_more::Display; | ||
|
||
#[derive(Debug, Display)] | ||
#[display(fmt = "Operation timed out")] | ||
pub struct TimeoutError; | ||
|
||
impl std::error::Error for TimeoutError {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod regex; | ||
pub mod spawn; | ||
pub mod task; | ||
pub mod time; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use enumflags2::BitFlags; | ||
use regex::{Regex as NativeRegex, RegexBuilder}; | ||
|
||
use crate::common::regex::{RegExpCompat, RegExpError, RegExpFlags}; | ||
|
||
pub struct RegExp { | ||
inner: NativeRegex, | ||
} | ||
|
||
impl RegExp { | ||
pub fn new(pattern: &str, flags: BitFlags<RegExpFlags>) -> Result<Self, RegExpError> { | ||
let mut builder = RegexBuilder::new(pattern); | ||
|
||
if flags.contains(RegExpFlags::Multiline) { | ||
builder.multi_line(true); | ||
} | ||
|
||
if flags.contains(RegExpFlags::IgnoreCase) { | ||
builder.case_insensitive(true); | ||
} | ||
|
||
let inner = builder.build().map_err(|e| RegExpError { message: e.to_string() })?; | ||
|
||
Ok(Self { inner }) | ||
} | ||
} | ||
|
||
impl RegExpCompat for RegExp { | ||
fn captures(&self, message: &str) -> Option<Vec<String>> { | ||
self.inner.captures(message).map(|captures| { | ||
captures | ||
.iter() | ||
.flat_map(|capture| capture.map(|cap| cap.as_str().to_owned())) | ||
.collect() | ||
}) | ||
} | ||
|
||
fn test(&self, message: &str) -> bool { | ||
self.inner.is_match(message) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod regex; | ||
pub mod spawn; | ||
pub mod task; | ||
pub mod time; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use enumflags2::BitFlags; | ||
use js_sys::RegExp as JSRegExp; | ||
|
||
use crate::common::regex::{RegExpCompat, RegExpError, RegExpFlags}; | ||
|
||
pub struct RegExp { | ||
inner: JSRegExp, | ||
} | ||
|
||
impl RegExp { | ||
pub fn new(pattern: &str, flags: BitFlags<RegExpFlags>) -> Result<Self, RegExpError> { | ||
let mut flags: String = flags.into_iter().map(|flag| flag.as_str()).collect(); | ||
|
||
// Global flag is implied in `regex::Regex`, so we match that behavior for consistency. | ||
flags.push('g'); | ||
|
||
Ok(Self { | ||
inner: JSRegExp::new(pattern, &flags), | ||
}) | ||
} | ||
} | ||
|
||
impl RegExpCompat for RegExp { | ||
fn captures(&self, message: &str) -> Option<Vec<String>> { | ||
self.inner.exec(message).map(|matches| { | ||
// We keep the same number of captures as the number of groups in the regex pattern, | ||
// but we guarantee that the captures are always strings. | ||
matches | ||
.iter() | ||
.map(|match_value| match_value.try_into().ok().unwrap_or_default()) | ||
.collect() | ||
}) | ||
} | ||
|
||
fn test(&self, input: &str) -> bool { | ||
self.inner.test(input) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters