Skip to content

Commit 065dabc

Browse files
committed
refactor: Further DRY console_log.rs with macro-based callback generation
Reduced code from 138 to 100 lines (27% reduction) by: - Replacing individual console_* functions with a single console_callback - Using console_method! macro to generate all 5 console methods - Eliminating redundant ConsoleLevel enum and implementation The macro approach ensures consistency across all console methods while keeping the code concise and maintainable.
1 parent caf963a commit 065dabc

1 file changed

Lines changed: 23 additions & 60 deletions

File tree

src/rules/console_log.rs

Lines changed: 23 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -42,75 +42,38 @@ fn format_console_args(scope: &mut v8::HandleScope, args: v8::FunctionCallbackAr
4242
log_parts.join(" ")
4343
}
4444

45-
/// Log level for console methods
46-
#[derive(Debug, Clone, Copy)]
47-
enum ConsoleLevel {
48-
Debug,
49-
Info,
50-
Warn,
51-
Error,
52-
}
53-
54-
impl ConsoleLevel {
55-
fn log(&self, message: &str) {
56-
match self {
57-
ConsoleLevel::Debug => debug!(target: "httpjail::rules::js", "{}", message),
58-
ConsoleLevel::Info => info!(target: "httpjail::rules::js", "{}", message),
59-
ConsoleLevel::Warn => warn!(target: "httpjail::rules::js", "{}", message),
60-
ConsoleLevel::Error => tracing::error!(target: "httpjail::rules::js", "{}", message),
61-
}
62-
}
63-
}
64-
65-
/// console.debug() callback
66-
fn console_debug(
67-
scope: &mut v8::HandleScope,
68-
args: v8::FunctionCallbackArguments,
69-
_retval: v8::ReturnValue,
70-
) {
71-
let message = format_console_args(scope, args);
72-
ConsoleLevel::Debug.log(&message);
73-
}
74-
75-
/// console.log() callback
76-
fn console_log(
77-
scope: &mut v8::HandleScope,
78-
args: v8::FunctionCallbackArguments,
79-
_retval: v8::ReturnValue,
80-
) {
81-
let message = format_console_args(scope, args);
82-
ConsoleLevel::Info.log(&message);
83-
}
84-
85-
/// console.info() callback
86-
fn console_info(
45+
/// Generic console callback that logs at a specific level
46+
fn console_callback(
8747
scope: &mut v8::HandleScope,
8848
args: v8::FunctionCallbackArguments,
8949
_retval: v8::ReturnValue,
50+
log_fn: fn(&str),
9051
) {
9152
let message = format_console_args(scope, args);
92-
ConsoleLevel::Info.log(&message);
53+
log_fn(&message);
9354
}
9455

95-
/// console.warn() callback
96-
fn console_warn(
97-
scope: &mut v8::HandleScope,
98-
args: v8::FunctionCallbackArguments,
99-
_retval: v8::ReturnValue,
100-
) {
101-
let message = format_console_args(scope, args);
102-
ConsoleLevel::Warn.log(&message);
56+
/// Macro to generate console method callbacks for each log level
57+
macro_rules! console_method {
58+
($name:ident, $log_macro:path) => {
59+
fn $name(
60+
scope: &mut v8::HandleScope,
61+
args: v8::FunctionCallbackArguments,
62+
retval: v8::ReturnValue,
63+
) {
64+
console_callback(scope, args, retval, |msg| {
65+
$log_macro!(target: "httpjail::rules::js", "{}", msg)
66+
});
67+
}
68+
};
10369
}
10470

105-
/// console.error() callback
106-
fn console_error(
107-
scope: &mut v8::HandleScope,
108-
args: v8::FunctionCallbackArguments,
109-
_retval: v8::ReturnValue,
110-
) {
111-
let message = format_console_args(scope, args);
112-
ConsoleLevel::Error.log(&message);
113-
}
71+
// Generate console.debug, console.log, console.info, console.warn, console.error
72+
console_method!(console_debug, debug);
73+
console_method!(console_log, info);
74+
console_method!(console_info, info);
75+
console_method!(console_warn, warn);
76+
console_method!(console_error, tracing::error);
11477

11578
/// Set up console object with debug, log, info, warn, error methods
11679
pub fn setup_console(context_scope: &mut v8::ContextScope<v8::HandleScope>) {

0 commit comments

Comments
 (0)