Skip to content

Commit

Permalink
add vpn and refactor some code
Browse files Browse the repository at this point in the history
  • Loading branch information
asm-flairstech committed May 21, 2024
1 parent 2f30578 commit 0d35c7c
Show file tree
Hide file tree
Showing 22 changed files with 755 additions and 190 deletions.
280 changes: 157 additions & 123 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ edition = "2021"
[dependencies]
chrono = "0"
dotenv = "0"
humantime = "2"
image = "0"
lazy_static = "1"
log = "0"
pyo3 = { version = "0", features = ["auto-initialize", "abi3"] }
rand = "0"
Expand Down
6 changes: 4 additions & 2 deletions rustmix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ license = "MIT"
chrono = "0"
crossbeam = { version = "0", optional = true }
csv = "1"
dirs = "5"
dotenv = "0"
execute = "0"
fake = { version = "2", features = [
"derive",
"uuid",
Expand Down Expand Up @@ -42,7 +44,7 @@ pyo3 = { version = "0", features = [
rand = "0"
randua = "0"
rayon = { version = "1", optional = true }
regex = { version = "1", optional = true }
regex = "1"
reqwest = { version = "0", features = [
"blocking",
"cookies",
Expand Down Expand Up @@ -77,7 +79,7 @@ metal = ["kalosm?/metal"]
cublas = ["kalosm?/cublas"]
mkl = ["kalosm?/mkl"]
language = ["kalosm/language"]
mail = ["dep:html-entities", "dep:lettre", "dep:once_cell", "dep:regex"]
mail = ["dep:html-entities", "dep:lettre", "dep:once_cell"]
threading = ["dep:crossbeam", "dep:rayon"]
log4rs = ["dep:log4rs"]
slog = [
Expand Down
30 changes: 23 additions & 7 deletions rustmix/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ impl<E: StdError + ?Sized> ErrorEx for E {
}

#[derive(Error, Debug)]
#[error("Operation is cancelled")]
pub struct CancelledError;
#[error("Operation is canceled")]
pub struct CanceledError;

#[derive(Error, Debug)]
#[error("Operation is not supported")]
pub struct NotSupportedError;

#[derive(Error, Debug)]
#[error("Invalid operation. {0}")]
pub struct InvalidOperationError(pub &'static str);
pub struct InvalidOperationError(pub String);

#[derive(Error, Debug)]
#[error("Operation timed out")]
Expand Down Expand Up @@ -84,16 +84,16 @@ pub struct InvalidInputError;
pub struct NotConfirmError;

#[derive(Error, Debug)]
#[error("Argument is null or empty: {0}")]
pub struct ArgumentIsNullOrEmptyError(pub &'static str);
#[error("Argument '{0}' is null or empty")]
pub struct ArgumentIsNullOrEmptyError(pub String);

#[derive(Error, Debug)]
#[error("No content")]
pub struct NoContentError;

#[derive(Error, Debug)]
#[error("HtmlElement not found: {0}")]
pub struct ElementNotFoundError(pub &'static str);
#[error("HtmlElement '{0}' not found")]
pub struct ElementNotFoundError(pub String);

#[derive(Error, Debug)]
#[error("Invalid HTTP response")]
Expand All @@ -102,3 +102,19 @@ pub struct InvalidResponseError;
#[derive(Error, Debug)]
#[error("Not implemented")]
pub struct NotImplementedError;

#[derive(Error, Debug)]
#[error("Error {0}. {1}")]
pub struct CommandError(pub i32, pub String);

#[derive(Error, Debug)]
#[error("Invalid command response")]
pub struct InvalidCommandResponseError;

#[derive(Error, Debug)]
#[error("VPN error. {0}")]
pub struct VPNError(pub String);

#[derive(Error, Debug)]
#[error("Unknown VPN response {0}")]
pub struct UnknownVPNResponseError(pub String);
1 change: 1 addition & 0 deletions rustmix/src/io/directory.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub use dirs::*;
use std::fs::{self, DirEntry};
pub use std::path::{Path, PathBuf};

Expand Down
2 changes: 2 additions & 0 deletions rustmix/src/io/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use crate::Result;

const LINES_BUFFER_DEFAULT: usize = 1000;

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum FileOpenOptions {
#[default]
Default,
New,
Truncate,
Expand Down
4 changes: 3 additions & 1 deletion rustmix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub mod python;
pub mod random;
pub mod string;
pub mod threading;
pub mod vpn;
pub mod web;

use lazy_static::lazy_static;
Expand All @@ -34,9 +35,10 @@ lazy_static! {

pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum LogLevel {
Off,
#[default]
Default,
Trace,
Debug,
Expand Down
8 changes: 2 additions & 6 deletions rustmix/src/random/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,15 @@ use fake::{faker::boolean::raw as f_boolean, locales, uuid, Fake};
use rand::{thread_rng, Rng};
use std::ops::Range;

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum UuidVersion {
V1,
V3,
#[default]
V4,
V5,
}

impl Default for UuidVersion {
fn default() -> Self {
UuidVersion::V4
}
}

pub fn alphanum() -> char {
let mut rng = rand::thread_rng();
let num = rng.gen_range(0..62);
Expand Down
30 changes: 30 additions & 0 deletions rustmix/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ pub trait StringEx {
fn trim_end_many(&self, ch: &[char]) -> &str;
fn prefix(&self, ch: char) -> String;
fn suffix(&self, ch: char) -> String;
fn find_first(&self, predicate: impl Fn(char) -> bool) -> Option<(char, usize)>;
fn find_last(&self, predicate: impl Fn(char) -> bool) -> Option<(char, usize)>;
}

impl StringEx for str {
Expand Down Expand Up @@ -143,4 +145,32 @@ impl StringEx for str {
self.to_owned()
}
}

fn find_first(&self, predicate: impl Fn(char) -> bool) -> Option<(char, usize)> {
if self.len() == 0 {
return None;
}

for (i, c) in self.chars().enumerate() {
if predicate(c) {
return Some((c, i));
}
}

None
}

fn find_last(&self, predicate: impl Fn(char) -> bool) -> Option<(char, usize)> {
if self.len() == 0 {
return None;
}

for (i, c) in self.chars().rev().enumerate() {
if predicate(c) {
return Some((c, self.len() - i));
}
}

None
}
}
4 changes: 2 additions & 2 deletions rustmix/src/threading/consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl<T: StaticTaskItem> Consumer<T> {

pub fn start<H: TaskDelegation<Consumer<T>, T>>(&self, handler: &H) -> Result<()> {
if self.is_cancelled() {
return Err(CancelledError.into());
return Err(CanceledError.into());
}

if self.is_completed() && self.is_empty() {
Expand Down Expand Up @@ -323,7 +323,7 @@ impl<T: StaticTaskItem> Consumer<T> {

pub fn enqueue(&self, item: T) -> Result<()> {
if self.is_cancelled() {
return Err(CancelledError.into());
return Err(CanceledError.into());
}

if self.is_completed() {
Expand Down
4 changes: 2 additions & 2 deletions rustmix/src/threading/injector_consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl<T: StaticTaskItem> InjectorWorker<T> {

pub fn start<H: TaskDelegation<InjectorWorker<T>, T>>(&self, handler: &H) -> Result<()> {
if self.is_cancelled() {
return Err(CancelledError.into());
return Err(CanceledError.into());
}

if self.is_completed() && self.is_empty() {
Expand Down Expand Up @@ -350,7 +350,7 @@ impl<T: StaticTaskItem> InjectorWorker<T> {

pub fn enqueue(&self, item: T) -> Result<()> {
if self.is_cancelled() {
return Err(CancelledError.into());
return Err(CanceledError.into());
}

if self.is_completed() {
Expand Down
26 changes: 13 additions & 13 deletions rustmix/src/threading/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use tokio::{
};

use crate::{
error::{CancelledError, TimedoutError},
error::{CanceledError, TimedoutError},
Result,
};

Expand Down Expand Up @@ -58,7 +58,7 @@ impl fmt::Display for TaskResult {
}
}

#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum QueueBehavior {
#[default]
FIFO,
Expand Down Expand Up @@ -100,12 +100,12 @@ fn wait<TPC: AwaitableConsumer<T>, T: StaticTaskItem>(
match finished.wait_while(|| !this.is_cancelled() && !this.is_finished()) {
Ok(_) => {
if this.is_cancelled() {
Err(CancelledError.into())
Err(CanceledError.into())
} else {
Ok(())
}
}
Err(_) => Err(CancelledError.into()),
Err(_) => Err(CanceledError.into()),
}
}

Expand All @@ -122,7 +122,7 @@ async fn wait_async<TPC: AwaitableConsumer<T>, T: StaticTaskItem>(
}

if this.is_cancelled() {
return Err(CancelledError.into());
return Err(CanceledError.into());
}

Ok(())
Expand All @@ -136,12 +136,12 @@ fn wait_until<TPC: AwaitableConsumer<T>, T: StaticTaskItem>(
match finished.wait_while(|| !this.is_cancelled() && !this.is_finished() && !cond(this)) {
Ok(_) => {
if this.is_cancelled() {
Err(CancelledError.into())
Err(CanceledError.into())
} else {
Ok(())
}
}
Err(_) => Err(CancelledError.into()),
Err(_) => Err(CanceledError.into()),
}
}

Expand All @@ -162,7 +162,7 @@ async fn wait_until_async<
}

if this.is_cancelled() {
return Err(CancelledError.into());
return Err(CanceledError.into());
}

Ok(())
Expand All @@ -180,7 +180,7 @@ fn wait_for<TPC: AwaitableConsumer<T>, T: StaticTaskItem>(
match finished.wait_timeout_while(|| !this.is_cancelled() && !this.is_finished(), timeout) {
Ok(_) => {
if this.is_cancelled() {
Err(CancelledError.into())
Err(CanceledError.into())
} else {
Ok(())
}
Expand All @@ -202,7 +202,7 @@ async fn wait_for_async<TPC: AwaitableConsumer<T>, T: StaticTaskItem>(
match result {
Ok(_) => {
if this.is_cancelled() {
Err(CancelledError.into())
Err(CanceledError.into())
} else {
Ok(())
}
Expand All @@ -226,7 +226,7 @@ fn wait_for_until<TPC: AwaitableConsumer<T>, T: StaticTaskItem>(
) {
Ok(_) => {
if this.is_cancelled() {
Err(CancelledError.into())
Err(CanceledError.into())
} else {
Ok(())
}
Expand All @@ -253,7 +253,7 @@ async fn wait_for_until_async<

while !cond(this).await {
if this.is_cancelled() {
return Err(CancelledError.into());
return Err(CanceledError.into());
}

if time::Instant::now().duration_since(start) > timeout {
Expand All @@ -263,7 +263,7 @@ async fn wait_for_until_async<
match time::timeout(PEEK_TIMEOUT_DEF, finished.notified()).await {
Ok(_) => {
if this.is_cancelled() {
return Err(CancelledError.into());
return Err(CanceledError.into());
}

return Ok(());
Expand Down
4 changes: 2 additions & 2 deletions rustmix/src/threading/producer_consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl<T: StaticTaskItem> ProducerConsumer<T> {

pub fn start<H: TaskDelegation<ProducerConsumer<T>, T>>(&self, handler: &H) -> Result<()> {
if self.is_cancelled() {
return Err(CancelledError.into());
return Err(CanceledError.into());
}

if self.is_completed() && self.is_empty() {
Expand Down Expand Up @@ -338,7 +338,7 @@ impl<T: StaticTaskItem> ProducerConsumer<T> {

pub fn enqueue(&self, item: T) -> Result<()> {
if self.is_cancelled() {
return Err(CancelledError.into());
return Err(CanceledError.into());
}

if self.is_completed() {
Expand Down
Loading

0 comments on commit 0d35c7c

Please sign in to comment.