From 1c0507ca00695031a51879498322280638f22e13 Mon Sep 17 00:00:00 2001 From: Louis Tricot Date: Mon, 31 Mar 2025 15:03:18 +0200 Subject: [PATCH] chore: small refactoring --- src/config.rs | 32 ++++++++------------------------ src/function_query.rs | 9 +-------- src/instrumentation.rs | 11 +++-------- src/lib.rs | 15 ++++----------- src/macros.rs | 24 ++++++++++++++++++++++++ 5 files changed, 40 insertions(+), 51 deletions(-) create mode 100644 src/macros.rs diff --git a/src/config.rs b/src/config.rs index b8e0ac9..646fb79 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,25 +4,10 @@ use nodejs_semver::{Range, Version}; use crate::error::OrchestrionError; use crate::function_query::FunctionQuery; +use crate::{get_arr, get_str}; use yaml_rust2::{Yaml, YamlLoader}; -macro_rules! get_str { - ($property:expr, $name:expr) => { - $property[$name] - .as_str() - .ok_or(format!("Invalid config: '{}' must be a string", $name))? - }; -} - -macro_rules! get_arr { - ($property:expr, $name:expr) => { - $property[$name] - .as_vec() - .ok_or(format!("Invalid config: '{}' must be a array", $name))? - }; -} - #[derive(Clone, Debug)] pub enum InstrumentationOperator { Callback, @@ -64,7 +49,7 @@ pub struct InstrumentationConfig { pub struct Config { pub instrumentations: Vec, - pub dc_module: String, + pub diagnostic_channel_module: String, } impl Config { @@ -72,12 +57,11 @@ impl Config { let docs = YamlLoader::load_from_str(yaml_str)?; let doc = &docs[0]; - let version = doc["version"] - .as_i64() - .ok_or("Invalid config: 'version' must be a number")?; - if version != 1 { - return Err("Invalid config version".into()); - } + match doc["version"].as_i64() { + Some(1) => 1, + Some(_) => return Err("Invalid config version".into()), + None => return Err("Invalid config: 'version' must be a number".into()), + }; let dc_module = doc["dc_module"].as_str().unwrap_or("diagnostics_channel"); @@ -85,7 +69,7 @@ impl Config { Ok(Config { instrumentations: configs, - dc_module: dc_module.to_string(), + diagnostic_channel_module: dc_module.to_string(), }) } } diff --git a/src/function_query.rs b/src/function_query.rs index a920052..4ab9798 100644 --- a/src/function_query.rs +++ b/src/function_query.rs @@ -1,15 +1,8 @@ use crate::error::OrchestrionError; +use crate::get_str; use swc_core::ecma::ast::{FnDecl, FnExpr, Function}; use yaml_rust2::Yaml; -macro_rules! get_str { - ($property:expr, $name:expr) => { - $property[$name] - .as_str() - .ok_or(format!("Invalid config: '{}' must be a string", $name))? - }; -} - #[derive(Debug)] pub enum FunctionType { FunctionDeclaration, diff --git a/src/instrumentation.rs b/src/instrumentation.rs index 13e34b8..656bbf3 100644 --- a/src/instrumentation.rs +++ b/src/instrumentation.rs @@ -1,4 +1,5 @@ use crate::config::InstrumentationConfig; +use crate::ident; use std::path::PathBuf; use swc_core::common::{Span, SyntaxContext}; use swc_core::ecma::{ @@ -11,12 +12,6 @@ use swc_core::ecma::{ }; use swc_core::quote; -macro_rules! ident { - ($name:expr) => { - Ident::new($name.into(), Span::default(), SyntaxContext::empty()) - }; -} - /// An [`Instrumentation`] instance represents a single instrumentation configuration, and implements /// SWC's [`VisitMut`] trait to insert tracing code into matching functions. You can use this /// wherever you would use a [`VisitMut`] instance, such as within an SWC plugin, for example. @@ -121,7 +116,7 @@ impl Instrumentation { if ($ch.hasSubscribers) { $ch.start.publish($ctx); } - } catch (tr_ch_err) { + } catch (tr_ch_err) { if ($ch.hasSubscribers) { $ctx.error = tr_ch_err; try { @@ -226,7 +221,7 @@ impl Instrumentation { .function_query .class .as_ref() - .map_or(true, |class| node.ident.sym.as_ref() == class); + .is_none_or(|class| node.ident.sym.as_ref() == class); true } diff --git a/src/lib.rs b/src/lib.rs index aa82f56..45df04d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,14 +6,7 @@ //! //! [`VisitMut`]: https://rustdoc.swc.rs/swc_core/ecma/visit/trait.VisitMut.html -#![deny(clippy::all)] -#![deny(clippy::pedantic)] -#![deny(clippy::style)] -#![deny(clippy::perf)] -#![deny(clippy::complexity)] -#![deny(clippy::correctness)] -#![deny(clippy::unwrap_used)] - +mod macros; use std::path::PathBuf; use std::str::FromStr; @@ -44,7 +37,7 @@ mod function_query; /// [`FromStr`]: https://doc.rust-lang.org/std/str/trait.FromStr.html pub struct Instrumentor { instrumentations: Vec, - dc_module: String, + diagnostic_channel_module: String, } impl Instrumentor { @@ -55,7 +48,7 @@ impl Instrumentor { .into_iter() .map(Instrumentation::new) .collect(), - dc_module: config.dc_module, + diagnostic_channel_module: config.diagnostic_channel_module, } } @@ -71,7 +64,7 @@ impl Instrumentor { .instrumentations .iter_mut() .filter(|instr| instr.matches(module_name, version, file_path)); - InstrumentationVisitor::new(instrumentations, self.dc_module.as_ref()) + InstrumentationVisitor::new(instrumentations, self.diagnostic_channel_module.as_ref()) } } diff --git a/src/macros.rs b/src/macros.rs new file mode 100644 index 0000000..b1a469a --- /dev/null +++ b/src/macros.rs @@ -0,0 +1,24 @@ +#[macro_export] +macro_rules! get_str { + ($property:expr, $name:expr) => { + $property[$name] + .as_str() + .ok_or(format!("Invalid config: '{}' must be a string", $name))? + }; +} + +#[macro_export] +macro_rules! get_arr { + ($property:expr, $name:expr) => { + $property[$name] + .as_vec() + .ok_or(format!("Invalid config: '{}' must be a array", $name))? + }; +} + +#[macro_export] +macro_rules! ident { + ($name:expr) => { + Ident::new($name.into(), Span::default(), SyntaxContext::empty()) + }; +}