Skip to content

Commit

Permalink
Merge branch 'main' into feat/configurable-timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
jkomyno authored Oct 16, 2024
2 parents e866d88 + 08713a9 commit b51d908
Show file tree
Hide file tree
Showing 37 changed files with 617 additions and 201 deletions.
115 changes: 107 additions & 8 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ tokio = { version = "1", features = [
"time",
] }
chrono = { version = "0.4.38", features = ["serde"] }
derive_more = "0.99.17"
user-facing-errors = { path = "./libs/user-facing-errors" }
uuid = { version = "1", features = ["serde", "v4", "v7", "js"] }
indoc = "2.0.1"
Expand Down
3 changes: 3 additions & 0 deletions libs/crosstarget-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
derive_more.workspace = true
enumflags2.workspace = true
futures = "0.3"

[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand All @@ -17,3 +19,4 @@ pin-project = "1"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tokio.workspace = true
regex.workspace = true
23 changes: 0 additions & 23 deletions libs/crosstarget-utils/src/common.rs

This file was deleted.

3 changes: 3 additions & 0 deletions libs/crosstarget-utils/src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod regex;
pub mod spawn;
pub mod timeout;
37 changes: 37 additions & 0 deletions libs/crosstarget-utils/src/common/regex.rs
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;
}
8 changes: 8 additions & 0 deletions libs/crosstarget-utils/src/common/spawn.rs
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 {}
7 changes: 7 additions & 0 deletions libs/crosstarget-utils/src/common/timeout.rs
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 {}
3 changes: 2 additions & 1 deletion libs/crosstarget-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ mod native;
#[cfg(not(target_arch = "wasm32"))]
pub use crate::native::*;

pub use common::SpawnError;
pub use crate::common::regex::RegExpCompat;
pub use crate::common::spawn::SpawnError;
1 change: 1 addition & 0 deletions libs/crosstarget-utils/src/native/mod.rs
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;
41 changes: 41 additions & 0 deletions libs/crosstarget-utils/src/native/regex.rs
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)
}
}
Loading

0 comments on commit b51d908

Please sign in to comment.