Skip to content

Commit 53151ee

Browse files
authored
refactor: introduce utils crate and remove external regex dependency (#266)
Create a new `utils` crate to centralize utility functions and remove the heavy `regex` dependency, replacing it with a custom lightweight placeholder parser. Also integrates the `colorex` module. Changes: - **New Crate `utils`**: - Added `utils/src/colorex.rs`: Custom color formatting logic (moved from `colorex` crate or inline). - Added `utils/src/formatx.rs`: Implements `count_placeholders` to replace regex usage for `{}` counting. - Updated workspace `Cargo.toml` to include `utils`. - **Dependency Removal**: - Removed `regex` from `front/parser/Cargo.toml`. - Replaced `regex::Regex` usage in `front/parser/src/parser/io.rs` with `utils::formatx::count_placeholders`. - **Integration**: - Updated `front/error/Cargo.toml` and `Cargo.toml` to depend on `utils`. - Updated `src/main.rs`, `src/lib.rs`, and `front/error/src/error.rs` to use `utils::colorex`. - **Cleanup**: - Removed placeholder `lib.rs` content in `utils`. This change significantly reduces compilation time and binary size by eliminating the regex engine dependency. Signed-off-by: LunaStev <luna@lunastev.org>
1 parent 8f9b0be commit 53151ee

File tree

12 files changed

+154
-20
lines changed

12 files changed

+154
-20
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ path = "src/lib.rs"
1313
# linker = "clang"
1414

1515
[dependencies]
16-
colorex = "0.1.0"
16+
utils = { path = "utils" }
1717
lexer = { path = "front/lexer" }
1818
parser = { path = "front/parser" }
1919
error = { path = "front/error" }
@@ -25,5 +25,6 @@ members = [
2525
"front/parser",
2626
"llvm_temporary",
2727
"front/error",
28+
"utils",
2829
".",
2930
]

front/error/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
colorex = "0.1.1"
7+
utils = { path = "../../utils" }

front/error/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ impl WaveError {
178178

179179
/// Display error in Rust-style format
180180
pub fn display(&self) {
181-
use colorex::Colorize;
181+
use utils::colorex::*;
182182

183183
let severity_str = match self.severity {
184184
ErrorSeverity::Error => "error".color("255,71,71").bold(),

front/lexer/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@ name = "lexer"
33
version = "0.1.0"
44
edition = "2021"
55

6-
[dependencies]

front/parser/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ edition = "2021"
66
[dependencies]
77
lexer = { path = "../lexer" }
88
error = { path = "../error" }
9-
regex = "1.11.1"
9+
utils = { path = "../../utils" }

front/parser/src/parser/io.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::iter::Peekable;
22
use std::slice::Iter;
3-
use regex::Regex;
3+
use utils::formatx::*;
44
use lexer::Token;
55
use lexer::token::TokenType;
66
use crate::ast::{ASTNode, StatementNode};
@@ -25,10 +25,7 @@ pub fn parse_println(tokens: &mut Peekable<Iter<Token>>) -> Option<ASTNode> {
2525
return None;
2626
};
2727

28-
let placeholder_count = Regex::new(r"\{[^}]*\}")
29-
.unwrap()
30-
.find_iter(&content)
31-
.count();
28+
let placeholder_count = count_placeholders(&content);
3229

3330
if placeholder_count == 0 {
3431
if tokens.peek()?.token_type != TokenType::Rparen {
@@ -110,10 +107,7 @@ pub fn parse_print(tokens: &mut Peekable<Iter<Token>>) -> Option<ASTNode> {
110107
return None;
111108
};
112109

113-
let placeholder_count = Regex::new(r"\{[^}]*\}")
114-
.unwrap()
115-
.find_iter(&content)
116-
.count();
110+
let placeholder_count = count_placeholders(&content);
117111

118112
if placeholder_count == 0 {
119113
// No format → Print just a string
@@ -195,10 +189,7 @@ pub fn parse_input(tokens: &mut Peekable<Iter<Token>>) -> Option<ASTNode> {
195189
return None;
196190
};
197191

198-
let placeholder_count = Regex::new(r"\{[^}]*\}")
199-
.unwrap()
200-
.find_iter(&content)
201-
.count();
192+
let placeholder_count = count_placeholders(&content);
202193

203194
let mut args = Vec::new();
204195
while let Some(Token {

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub mod runner;
44
pub mod version;
55

66
use crate::version::get_os_pretty_name;
7-
use colorex::Colorize;
7+
use utils::colorex::*;
88
use std::path::Path;
99

1010
use commands::DebugFlags;

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::path::Path;
22
use std::{env, process};
33

4-
use colorex::Colorize;
4+
use utils::colorex::*;
55
use wavec::commands::{handle_build, handle_run, DebugFlags};
66
use wavec::errors::CliError;
77
use wavec::version_wave;

utils/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
name = "utils"
3+
version = "0.1.0"
4+
edition = "2021"

utils/src/colorex.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
pub struct Color(u8, u8, u8);
2+
3+
impl Color {
4+
pub fn from_rgb(rgb: &str) -> Result<Color, &'static str> {
5+
let parts: Vec<&str> = rgb.split(',').collect();
6+
if parts.len() == 3 {
7+
let r = parts[0].parse::<u8>().map_err(|_| "Invalid RGB format")?;
8+
let g = parts[1].parse::<u8>().map_err(|_| "Invalid RGB format")?;
9+
let b = parts[2].parse::<u8>().map_err(|_| "Invalid RGB format")?;
10+
Ok(Color(r, g, b))
11+
} else {
12+
Err("Invalid RGB format")
13+
}
14+
}
15+
16+
pub fn from_hex(hex: &str) -> Result<Color, &'static str> {
17+
if hex.len() != 7 || !hex.starts_with('#') {
18+
return Err("Invalid HEX format");
19+
}
20+
21+
let r = u8::from_str_radix(&hex[1..3], 16).map_err(|_| "Invalid HEX value")?;
22+
let g = u8::from_str_radix(&hex[3..5], 16).map_err(|_| "Invalid HEX value")?;
23+
let b = u8::from_str_radix(&hex[5..7], 16).map_err(|_| "Invalid HEX value")?;
24+
25+
Ok(Color(r, g, b))
26+
}
27+
}
28+
29+
pub trait Colorize {
30+
fn color(self, color: &str) -> String;
31+
fn bg_color(self, color: &str) -> String;
32+
fn bold(self) -> String;
33+
fn italic(self) -> String;
34+
fn underline(self) -> String;
35+
fn strikethrough(self) -> String;
36+
fn dim(self) -> String;
37+
fn invert(self) -> String;
38+
}
39+
40+
impl Colorize for &str {
41+
fn color(self, color: &str) -> String {
42+
let color = if color.starts_with('#') {
43+
Color::from_hex(color)
44+
} else {
45+
Color::from_rgb(color)
46+
};
47+
48+
match color {
49+
Ok(c) => format!("\x1b[38;2;{};{};{}m{}\x1b[0m", c.0, c.1, c.2, self),
50+
Err(_) => self.to_string(),
51+
}
52+
}
53+
54+
fn bg_color(self, color: &str) -> String {
55+
let color = if color.starts_with('#') {
56+
Color::from_hex(color)
57+
} else {
58+
Color::from_rgb(color)
59+
};
60+
61+
match color {
62+
Ok(c) => format!("\x1b[48;2;{};{};{}m{}\x1b[0m", c.0, c.1, c.2, self),
63+
Err(_) => self.to_string(),
64+
}
65+
}
66+
67+
fn bold(self) -> String {
68+
format!("\x1b[1m{}\x1b[0m", self)
69+
}
70+
71+
fn italic(self) -> String {
72+
format!("\x1b[3m{}\x1b[0m", self)
73+
}
74+
75+
fn underline(self) -> String {
76+
format!("\x1b[4m{}\x1b[0m", self)
77+
}
78+
79+
fn strikethrough(self) -> String {
80+
format!("\x1b[9m{}\x1b[0m", self)
81+
}
82+
83+
fn dim(self) -> String {
84+
format!("\x1b[2m{}\x1b[0m", self)
85+
}
86+
87+
fn invert(self) -> String {
88+
format!("\x1b[7m{}\x1b[0m", self)
89+
}
90+
}
91+
92+

0 commit comments

Comments
 (0)