Skip to content

Commit 45a43d4

Browse files
committed
bref
1 parent ac24249 commit 45a43d4

5 files changed

Lines changed: 40 additions & 51 deletions

File tree

src/config.rs

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,10 @@ use nodejs_semver::{Range, Version};
44

55
use crate::error::OrchestrionError;
66
use crate::function_query::FunctionQuery;
7+
use crate::{get_arr, get_str};
78

89
use yaml_rust2::{Yaml, YamlLoader};
910

10-
macro_rules! get_str {
11-
($property:expr, $name:expr) => {
12-
$property[$name]
13-
.as_str()
14-
.ok_or(format!("Invalid config: '{}' must be a string", $name))?
15-
};
16-
}
17-
18-
macro_rules! get_arr {
19-
($property:expr, $name:expr) => {
20-
$property[$name]
21-
.as_vec()
22-
.ok_or(format!("Invalid config: '{}' must be a array", $name))?
23-
};
24-
}
25-
2611
#[derive(Clone, Debug)]
2712
pub enum InstrumentationOperator {
2813
Callback,
@@ -64,28 +49,27 @@ pub struct InstrumentationConfig {
6449

6550
pub struct Config {
6651
pub instrumentations: Vec<InstrumentationConfig>,
67-
pub dc_module: String,
52+
pub diagnostic_channel_module: String,
6853
}
6954

7055
impl Config {
7156
pub fn from_yaml_data(yaml_str: &str) -> Result<Config, OrchestrionError> {
7257
let docs = YamlLoader::load_from_str(yaml_str)?;
7358
let doc = &docs[0];
7459

75-
let version = doc["version"]
76-
.as_i64()
77-
.ok_or("Invalid config: 'version' must be a number")?;
78-
if version != 1 {
79-
return Err("Invalid config version".into());
80-
}
60+
match doc["version"].as_i64() {
61+
Some(1) => 1,
62+
Some(_) => return Err("Invalid config version".into()),
63+
None => return Err("Invalid config: 'version' must be a number".into()),
64+
};
8165

8266
let dc_module = doc["dc_module"].as_str().unwrap_or("diagnostics_channel");
8367

8468
let configs = InstrumentationConfig::from_yaml(doc)?;
8569

8670
Ok(Config {
8771
instrumentations: configs,
88-
dc_module: dc_module.to_string(),
72+
diagnostic_channel_module: dc_module.to_string(),
8973
})
9074
}
9175
}

src/function_query.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
use crate::error::OrchestrionError;
2+
use crate::get_str;
23
use swc_core::ecma::ast::{FnDecl, FnExpr, Function};
34
use yaml_rust2::Yaml;
45

5-
macro_rules! get_str {
6-
($property:expr, $name:expr) => {
7-
$property[$name]
8-
.as_str()
9-
.ok_or(format!("Invalid config: '{}' must be a string", $name))?
10-
};
11-
}
12-
136
#[derive(Debug)]
147
pub enum FunctionType {
158
FunctionDeclaration,

src/instrumentation.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::config::InstrumentationConfig;
2+
use crate::ident;
23
use std::path::PathBuf;
34
use swc_core::common::{Span, SyntaxContext};
45
use swc_core::ecma::{
@@ -11,12 +12,6 @@ use swc_core::ecma::{
1112
};
1213
use swc_core::quote;
1314

14-
macro_rules! ident {
15-
($name:expr) => {
16-
Ident::new($name.into(), Span::default(), SyntaxContext::empty())
17-
};
18-
}
19-
2015
/// An [`Instrumentation`] instance represents a single instrumentation configuration, and implements
2116
/// SWC's [`VisitMut`] trait to insert tracing code into matching functions. You can use this
2217
/// wherever you would use a [`VisitMut`] instance, such as within an SWC plugin, for example.
@@ -121,7 +116,7 @@ impl Instrumentation {
121116
if ($ch.hasSubscribers) {
122117
$ch.start.publish($ctx);
123118
}
124-
} catch (tr_ch_err) {
119+
} catch (tr_ch_err) {
125120
if ($ch.hasSubscribers) {
126121
$ctx.error = tr_ch_err;
127122
try {
@@ -226,7 +221,7 @@ impl Instrumentation {
226221
.function_query
227222
.class
228223
.as_ref()
229-
.map_or(true, |class| node.ident.sym.as_ref() == class);
224+
.is_none_or(|class| node.ident.sym.as_ref() == class);
230225
true
231226
}
232227

src/lib.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,7 @@
66
//!
77
//! [`VisitMut`]: https://rustdoc.swc.rs/swc_core/ecma/visit/trait.VisitMut.html
88
9-
#![deny(clippy::all)]
10-
#![deny(clippy::pedantic)]
11-
#![deny(clippy::style)]
12-
#![deny(clippy::perf)]
13-
#![deny(clippy::complexity)]
14-
#![deny(clippy::correctness)]
15-
#![deny(clippy::unwrap_used)]
16-
9+
mod macros;
1710
use std::path::PathBuf;
1811
use std::str::FromStr;
1912

@@ -44,7 +37,7 @@ mod function_query;
4437
/// [`FromStr`]: https://doc.rust-lang.org/std/str/trait.FromStr.html
4538
pub struct Instrumentor {
4639
instrumentations: Vec<Instrumentation>,
47-
dc_module: String,
40+
diagnostic_channel_module: String,
4841
}
4942

5043
impl Instrumentor {
@@ -55,7 +48,7 @@ impl Instrumentor {
5548
.into_iter()
5649
.map(Instrumentation::new)
5750
.collect(),
58-
dc_module: config.dc_module,
51+
diagnostic_channel_module: config.diagnostic_channel_module,
5952
}
6053
}
6154

@@ -71,7 +64,7 @@ impl Instrumentor {
7164
.instrumentations
7265
.iter_mut()
7366
.filter(|instr| instr.matches(module_name, version, file_path));
74-
InstrumentationVisitor::new(instrumentations, self.dc_module.as_ref())
67+
InstrumentationVisitor::new(instrumentations, self.diagnostic_channel_module.as_ref())
7568
}
7669
}
7770

src/macros.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#[macro_export]
2+
macro_rules! get_str {
3+
($property:expr, $name:expr) => {
4+
$property[$name]
5+
.as_str()
6+
.ok_or(format!("Invalid config: '{}' must be a string", $name))?
7+
};
8+
}
9+
10+
#[macro_export]
11+
macro_rules! get_arr {
12+
($property:expr, $name:expr) => {
13+
$property[$name]
14+
.as_vec()
15+
.ok_or(format!("Invalid config: '{}' must be a array", $name))?
16+
};
17+
}
18+
19+
#[macro_export]
20+
macro_rules! ident {
21+
($name:expr) => {
22+
Ident::new($name.into(), Span::default(), SyntaxContext::empty())
23+
};
24+
}

0 commit comments

Comments
 (0)