@@ -2,57 +2,34 @@ use std::path::PathBuf;
22
33use nodejs_semver:: { Range , Version } ;
44
5- use crate :: error:: OrchestrionError ;
65use 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 ) ]
2710pub 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
3421impl 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 ) ]
5633pub 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
9355impl 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- }
0 commit comments