Skip to content

Commit acdb5ab

Browse files
committed
remove default yaml usage, relying on serde instead
1 parent 5268efe commit acdb5ab

7 files changed

Lines changed: 65 additions & 217 deletions

File tree

Cargo.lock

Lines changed: 26 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ rust-version = "1.84.1"
1313
lto = true
1414

1515
[dependencies]
16-
nodejs-semver = "4.1.0"
16+
nodejs-semver = { version = "4.1.0", features = ["serde"] }
17+
serde = { version = "1.0.219", features = ["derive"] }
1718
swc = "21.0.0"
1819
swc_core = { version = "22.4.0", features = ["ecma_plugin_transform","ecma_quote"] }
1920
swc_ecma_parser = "11.1.2"
2021
swc_ecma_visit = { version = "8.0.0", features = ["path"] }
21-
yaml-rust2 = "0.10.0"
2222

2323
[dev-dependencies]
2424
assert_cmd = "2.0.16"
25+
serde_yaml = "0.9.34"

src/config.rs

Lines changed: 14 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,34 @@ use std::path::PathBuf;
22

33
use nodejs_semver::{Range, Version};
44

5-
use crate::error::OrchestrionError;
65
use crate::function_query::FunctionQuery;
76

8-
use yaml_rust2::{Yaml, YamlLoader};
7+
use serde::Deserialize;
98

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-
26-
#[derive(Clone, Debug)]
9+
#[derive(Clone, Debug, Deserialize)]
2710
pub enum InstrumentationOperator {
11+
#[serde(rename = "traceCallback")]
2812
Callback,
13+
#[serde(rename = "tracePromise")]
2914
Promise,
15+
#[serde(rename = "traceSync")]
3016
Sync,
17+
#[serde(rename = "traceAsync")]
3118
Async,
3219
}
3320

3421
impl InstrumentationOperator {
35-
pub fn as_str(&self) -> &'static str {
22+
pub fn as_str(&self) -> &str {
3623
match self {
3724
InstrumentationOperator::Callback => "traceCallback",
3825
InstrumentationOperator::Promise => "tracePromise",
3926
InstrumentationOperator::Sync => "traceSync",
4027
InstrumentationOperator::Async => "traceAsync",
4128
}
4229
}
43-
44-
pub fn from_str(s: &str) -> Option<InstrumentationOperator> {
45-
match s {
46-
"traceCallback" => Some(InstrumentationOperator::Callback),
47-
"tracePromise" => Some(InstrumentationOperator::Promise),
48-
"traceSync" => Some(InstrumentationOperator::Sync),
49-
"traceAsync" => Some(InstrumentationOperator::Async),
50-
_ => None,
51-
}
52-
}
5330
}
5431

55-
#[derive(Debug)]
32+
#[derive(Deserialize, Debug, Clone)]
5633
pub struct InstrumentationConfig {
5734
pub module_name: String,
5835
pub version_range: Range,
@@ -62,49 +39,20 @@ pub struct InstrumentationConfig {
6239
pub channel_name: String,
6340
}
6441

65-
pub struct Config {
42+
#[derive(Deserialize, Clone)]
43+
pub struct OrchestrionConfig {
6644
pub instrumentations: Vec<InstrumentationConfig>,
45+
#[serde(default = "OrchestrionConfig::dc_module_default")]
6746
pub dc_module: String,
6847
}
6948

70-
impl Config {
71-
pub fn from_yaml_data(yaml_str: &str) -> Result<Config, OrchestrionError> {
72-
let docs = YamlLoader::load_from_str(yaml_str)?;
73-
let doc = &docs[0];
74-
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-
}
81-
82-
let dc_module = doc["dc_module"].as_str().unwrap_or("diagnostics_channel");
83-
84-
let configs = InstrumentationConfig::from_yaml(doc)?;
85-
86-
Ok(Config {
87-
instrumentations: configs,
88-
dc_module: dc_module.to_string(),
89-
})
49+
impl OrchestrionConfig {
50+
fn dc_module_default() -> String {
51+
"diagnostics_channel".to_string()
9052
}
9153
}
9254

9355
impl InstrumentationConfig {
94-
pub fn from_yaml(doc: &Yaml) -> Result<Vec<InstrumentationConfig>, OrchestrionError> {
95-
let instrumentations = get_arr!(doc, "instrumentations");
96-
let mut configs = Vec::new();
97-
98-
for instr in instrumentations {
99-
instr
100-
.as_hash()
101-
.ok_or("Invalid config: 'instrumentations' must be a array of objects")?;
102-
configs.push(instr.try_into()?);
103-
}
104-
105-
Ok(configs)
106-
}
107-
10856
pub fn matches(&self, module_name: &str, version: &str, file_path: &PathBuf) -> bool {
10957
let version: Version = match version.parse() {
11058
Ok(v) => v,
@@ -115,32 +63,3 @@ impl InstrumentationConfig {
11563
&& self.file_path == *file_path
11664
}
11765
}
118-
119-
impl TryFrom<&Yaml> for InstrumentationConfig {
120-
type Error = OrchestrionError;
121-
122-
fn try_from(instr: &Yaml) -> Result<Self, Self::Error> {
123-
let module_name = get_str!(instr, "module_name");
124-
let version_range = get_str!(instr, "version_range");
125-
let version_range: Range = version_range
126-
.parse()
127-
.map_err(|_| format!("Invalid version range: {version_range}"))?;
128-
let file_path = PathBuf::from(get_str!(instr, "file_path"));
129-
if instr["function_query"].as_hash().is_none() {
130-
return Err("Invalid config: 'function_query' must be a object".into());
131-
}
132-
let function_query = (&instr["function_query"]).try_into()?;
133-
let operator = InstrumentationOperator::from_str(get_str!(instr, "operator"))
134-
.unwrap_or(InstrumentationOperator::Sync);
135-
let channel_name = get_str!(instr, "channel_name");
136-
137-
Ok(InstrumentationConfig {
138-
module_name: module_name.to_string(),
139-
version_range,
140-
file_path,
141-
function_query,
142-
operator,
143-
channel_name: channel_name.to_string(),
144-
})
145-
}
146-
}

src/error.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ use std::fmt::{self, Display, Formatter};
22

33
#[derive(Debug)]
44
pub enum OrchestrionError {
5-
InvalidVersionRange(String),
65
IoError(std::io::Error),
7-
YamlParseError(yaml_rust2::ScanError),
86
StrError(String),
97
}
108

@@ -14,12 +12,6 @@ impl From<std::io::Error> for OrchestrionError {
1412
}
1513
}
1614

17-
impl From<yaml_rust2::ScanError> for OrchestrionError {
18-
fn from(e: yaml_rust2::ScanError) -> Self {
19-
OrchestrionError::YamlParseError(e)
20-
}
21-
}
22-
2315
impl From<String> for OrchestrionError {
2416
fn from(s: String) -> Self {
2517
OrchestrionError::StrError(s)
@@ -35,9 +27,7 @@ impl From<&str> for OrchestrionError {
3527
impl Display for OrchestrionError {
3628
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
3729
match self {
38-
OrchestrionError::InvalidVersionRange(s) => write!(f, "Invalid version range: {s}"),
3930
OrchestrionError::IoError(e) => write!(f, "IO error: {e}"),
40-
OrchestrionError::YamlParseError(e) => write!(f, "YAML parse error: {e}"),
4131
OrchestrionError::StrError(s) => write!(f, "String error: {s}"),
4232
}
4333
}

0 commit comments

Comments
 (0)