@@ -14,14 +14,30 @@ use tracing::{debug, info};
14
14
use crate :: util:: DSC_CONFIG_ROOT ;
15
15
16
16
#[ derive( Debug , Clone , PartialEq , Deserialize , Serialize , JsonSchema ) ]
17
- pub struct Include {
17
+ pub enum IncludeKind {
18
18
/// The path to the file to include. Path is relative to the file containing the include
19
19
/// and not allowed to reference parent directories. If a configuration document is used
20
20
/// instead of a file, then the path is relative to the current working directory.
21
21
#[ serde( rename = "configurationFile" ) ]
22
- pub configuration_file : String ,
22
+ FilePath ( String ) ,
23
+ #[ serde( rename = "configurationContent" ) ]
24
+ Content ( String ) ,
25
+ }
26
+
27
+ #[ derive( Debug , Clone , PartialEq , Deserialize , Serialize , JsonSchema ) ]
28
+ pub enum IncludeParametersKind {
23
29
#[ serde( rename = "parametersFile" ) ]
24
- pub parameters_file : Option < String > ,
30
+ FilePath ( String ) ,
31
+ #[ serde( rename = "parametersContent" ) ]
32
+ Content ( String ) ,
33
+ }
34
+
35
+ #[ derive( Debug , Clone , PartialEq , Deserialize , Serialize , JsonSchema ) ]
36
+ pub struct Include {
37
+ #[ serde( flatten) ]
38
+ pub configuration : IncludeKind ,
39
+ #[ serde( flatten) ]
40
+ pub parameters : Option < IncludeParametersKind > ,
25
41
}
26
42
27
43
/// Read the file specified in the Include input and return the content as a JSON string.
@@ -51,74 +67,104 @@ pub fn get_contents(input: &str) -> Result<(Option<String>, String), String> {
51
67
}
52
68
} ;
53
69
54
- let include_path = normalize_path ( Path :: new ( & include. configuration_file ) ) ?;
70
+ let config_json = match include. configuration {
71
+ IncludeKind :: FilePath ( file_path) => {
72
+ let include_path = normalize_path ( Path :: new ( & file_path) ) ?;
55
73
56
- // read the file specified in the Include input
57
- let mut buffer: Vec < u8 > = Vec :: new ( ) ;
58
- match File :: open ( & include_path) {
59
- Ok ( mut file) => {
60
- match file. read_to_end ( & mut buffer) {
61
- Ok ( _) => ( ) ,
74
+ // read the file specified in the Include input
75
+ let mut buffer: Vec < u8 > = Vec :: new ( ) ;
76
+ match File :: open ( & include_path) {
77
+ Ok ( mut file) => {
78
+ match file. read_to_end ( & mut buffer) {
79
+ Ok ( _) => ( ) ,
80
+ Err ( err) => {
81
+ return Err ( format ! ( "{} '{include_path:?}': {err}" , t!( "resolve.failedToReadFile" ) ) ) ;
82
+ }
83
+ }
84
+ } ,
62
85
Err ( err) => {
63
- return Err ( format ! ( "{} '{include_path:?}': {err}" , t!( "resolve.failedToReadFile " ) ) ) ;
86
+ return Err ( format ! ( "{} '{include_path:?}': {err}" , t!( "resolve.failedToOpenFile " ) ) ) ;
64
87
}
65
88
}
66
- } ,
67
- Err ( err) => {
68
- return Err ( format ! ( "{} '{include_path:?}': {err}" , t!( "resolve.failedToOpenFile" ) ) ) ;
69
- }
70
- }
71
- // convert the buffer to a string
72
- let include_content = match String :: from_utf8 ( buffer) {
73
- Ok ( input) => input,
74
- Err ( err) => {
75
- return Err ( format ! ( "{} '{include_path:?}': {err}" , t!( "resolve.invalidFileContent" ) ) ) ;
76
- }
77
- } ;
89
+ // convert the buffer to a string
90
+ let include_content = match String :: from_utf8 ( buffer) {
91
+ Ok ( input) => input,
92
+ Err ( err) => {
93
+ return Err ( format ! ( "{} '{include_path:?}': {err}" , t!( "resolve.invalidFileContent" ) ) ) ;
94
+ }
95
+ } ;
78
96
79
- // try to deserialize the Include content as YAML first
80
- let configuration: Configuration = match serde_yaml:: from_str ( & include_content) {
81
- Ok ( configuration) => configuration,
82
- Err ( _err) => {
83
- // if that fails, try to deserialize it as JSON
84
- match serde_json:: from_str ( & include_content) {
85
- Ok ( configuration) => configuration,
97
+ match parse_input_to_json ( & include_content) {
98
+ Ok ( json) => json,
86
99
Err ( err) => {
87
100
return Err ( format ! ( "{} '{include_path:?}': {err}" , t!( "resolve.invalidFile" ) ) ) ;
88
101
}
89
102
}
90
- }
91
- } ;
103
+ // // try to deserialize the Include content as YAML first
104
+ // let configuration: Configuration = match serde_yaml::from_str(&include_content) {
105
+ // Ok(configuration) => configuration,
106
+ // Err(_err) => {
107
+ // // if that fails, try to deserialize it as JSON
108
+ // match serde_json::from_str(&include_content) {
109
+ // Ok(configuration) => configuration,
110
+ // Err(err) => {
111
+ // return Err(format!("{} '{include_path:?}': {err}", t!("resolve.invalidFile")));
112
+ // }
113
+ // }
114
+ // }
115
+ // };
92
116
93
- // serialize the Configuration as JSON
94
- let config_json = match serde_json:: to_string ( & configuration) {
95
- Ok ( json) => json,
96
- Err ( err) => {
97
- return Err ( format ! ( "JSON: {err}" ) ) ;
117
+ // // serialize the Configuration as JSON
118
+ // match serde_json::to_string(&configuration) {
119
+ // Ok(json) => json,
120
+ // Err(err) => {
121
+ // return Err(format!("JSON: {err}"));
122
+ // }
123
+ // }
124
+ } ,
125
+ IncludeKind :: Content ( text) => {
126
+ match parse_input_to_json ( & text) {
127
+ Ok ( json) => json,
128
+ Err ( err) => {
129
+ return Err ( format ! ( "{}: {err}" , t!( "resolve.invalidFile" ) ) ) ;
130
+ }
131
+ }
98
132
}
99
133
} ;
100
134
101
- let parameters = if let Some ( parameters_file) = include. parameters_file {
102
- // combine the path with DSC_CONFIG_ROOT
103
- let parameters_file = normalize_path ( Path :: new ( & parameters_file) ) ?;
104
- info ! ( "{} '{parameters_file:?}'" , t!( "resolve.resolvingParameters" ) ) ;
105
- match std:: fs:: read_to_string ( & parameters_file) {
106
- Ok ( parameters) => {
107
- let parameters_json = match parse_input_to_json ( & parameters) {
108
- Ok ( json) => json,
109
- Err ( err) => {
110
- return Err ( format ! ( "{} '{parameters_file:?}': {err}" , t!( "resolve.failedParseParametersFile" ) ) ) ;
111
- }
112
- } ;
113
- Some ( parameters_json)
114
- } ,
115
- Err ( err) => {
116
- return Err ( format ! ( "{} '{parameters_file:?}': {err}" , t!( "resolve.failedResolveParametersFile" ) ) ) ;
135
+ let parameters = match include. parameters {
136
+ Some ( IncludeParametersKind :: FilePath ( file_path) ) => {
137
+ // combine the path with DSC_CONFIG_ROOT
138
+ let parameters_file = normalize_path ( Path :: new ( & file_path) ) ?;
139
+ info ! ( "{} '{parameters_file:?}'" , t!( "resolve.resolvingParameters" ) ) ;
140
+ match std:: fs:: read_to_string ( & parameters_file) {
141
+ Ok ( parameters) => {
142
+ let parameters_json = match parse_input_to_json ( & parameters) {
143
+ Ok ( json) => json,
144
+ Err ( err) => {
145
+ return Err ( format ! ( "{} '{parameters_file:?}': {err}" , t!( "resolve.failedParseParametersFile" ) ) ) ;
146
+ }
147
+ } ;
148
+ Some ( parameters_json)
149
+ } ,
150
+ Err ( err) => {
151
+ return Err ( format ! ( "{} '{parameters_file:?}': {err}" , t!( "resolve.failedResolveParametersFile" ) ) ) ;
152
+ }
117
153
}
154
+ } ,
155
+ Some ( IncludeParametersKind :: Content ( text) ) => {
156
+ let parameters_json = match parse_input_to_json ( & text) {
157
+ Ok ( json) => json,
158
+ Err ( err) => {
159
+ return Err ( format ! ( "{}: {err}" , t!( "resolve.invalidParametersContent" ) ) ) ;
160
+ }
161
+ } ;
162
+ Some ( parameters_json)
163
+ } ,
164
+ None => {
165
+ debug ! ( "{}" , t!( "resolve.noParameters" ) ) ;
166
+ None
118
167
}
119
- } else {
120
- debug ! ( "{}" , t!( "resolve.noParametersFile" ) ) ;
121
- None
122
168
} ;
123
169
124
170
Ok ( ( parameters, config_json) )
0 commit comments