@@ -89,7 +89,6 @@ impl XmlNode {
89
89
let mut msg_guard = msg. write ( ) . await ;
90
90
91
91
if !msg_guard. contains ( & self . config . property ) {
92
- // If property doesn't exist, just pass through
93
92
drop ( msg_guard) ;
94
93
return self . fan_out_one ( Envelope { port : 0 , msg } , CancellationToken :: new ( ) ) . await ;
95
94
}
@@ -98,23 +97,35 @@ impl XmlNode {
98
97
99
98
if let Some ( value) = property_value {
100
99
let result = match value {
101
- // String input: parse XML to object
102
100
Variant :: String ( xml_string) => {
103
- let options = Xml2jsOptions :: default ( ) ; //msg_guard.get("options").cloned();
101
+ let mut options = if let Some ( options_var) = msg_guard. get ( "options" ) {
102
+ self . extract_known_options_properties ( options_var)
103
+ } else {
104
+ self . default_options . clone ( )
105
+ } ;
106
+ if let Some ( attr) = & self . config . attr {
107
+ options. attrkey = attr. clone ( ) ;
108
+ }
109
+ if let Some ( chr) = & self . config . chr {
110
+ options. charkey = chr. clone ( ) ;
111
+ }
104
112
self . parse_xml_to_object ( xml_string, & options)
105
113
}
106
-
107
- // Object input: convert to XML string
108
114
Variant :: Object ( _) => {
109
- if let Some ( options_var) = msg_guard. get ( "options" ) {
110
- let options = self . extract_known_options_properties ( options_var) ;
111
- self . convert_object_to_xml ( & value, & options)
115
+ let mut options = if let Some ( options_var) = msg_guard. get ( "options" ) {
116
+ self . extract_known_options_properties ( options_var)
112
117
} else {
113
- self . convert_object_to_xml ( & value, & self . default_options )
118
+ self . default_options . clone ( )
119
+ } ;
120
+ if let Some ( attr) = & self . config . attr {
121
+ options. attrkey = attr. clone ( ) ;
122
+ }
123
+ if let Some ( chr) = & self . config . chr {
124
+ options. charkey = chr. clone ( ) ;
114
125
}
126
+ self . convert_object_to_xml ( & value, & options)
115
127
}
116
128
_ => {
117
- // For other types, issue a warning and pass through
118
129
log:: warn!( "XML node expects string or object input, got {value:?}" ) ;
119
130
Ok ( value. clone ( ) )
120
131
}
@@ -222,92 +233,56 @@ impl XmlNode {
222
233
options : & Xml2jsOptions ,
223
234
) -> crate :: Result < ( ) > {
224
235
for ( key, value) in obj {
225
- let mut start_elem = BytesStart :: new ( key) ;
226
-
227
- // Check if this object has attributes or character data
228
- if let Variant :: Object ( inner_obj) = value {
229
- let mut text_content = None ;
230
-
231
- // Extract attributes
232
- if let Some ( Variant :: Object ( attrs) ) = inner_obj. get ( & options. attrkey ) {
233
- for ( attr_name, attr_value) in attrs {
234
- let attr_val = self . variant_to_xml_text ( attr_value) ;
235
- start_elem. push_attribute ( ( attr_name. as_bytes ( ) , attr_val. as_bytes ( ) ) ) ;
236
+ match value {
237
+ Variant :: Array ( arr) => {
238
+ for item in arr {
239
+ let mut single = BTreeMap :: new ( ) ;
240
+ single. insert ( key. clone ( ) , item. clone ( ) ) ;
241
+ self . write_object_to_xml ( writer, & single, options) ?;
236
242
}
237
243
}
238
-
239
- // Extract text content
240
- if let Some ( text_var) = inner_obj. get ( & options. charkey ) {
241
- text_content = Some ( self . variant_to_xml_text ( text_var) ) ;
242
- }
243
-
244
- writer
245
- . write_event ( Event :: Start ( start_elem. clone ( ) ) )
246
- . map_err ( |e| crate :: EdgelinkError :: InvalidOperation ( format ! ( "XML write error: {e}" ) ) ) ?;
247
-
248
- // Write text content if present
249
- if let Some ( text) = text_content {
244
+ Variant :: Object ( inner_obj) => {
245
+ let mut start_elem = BytesStart :: new ( key) ;
246
+ if let Some ( Variant :: Object ( attrs) ) = inner_obj. get ( & options. attrkey ) {
247
+ for ( attr_name, attr_value) in attrs {
248
+ let attr_val = self . variant_to_xml_text ( attr_value) ;
249
+ start_elem. push_attribute ( ( attr_name. as_bytes ( ) , attr_val. as_bytes ( ) ) ) ;
250
+ }
251
+ }
250
252
writer
251
- . write_event ( Event :: Text ( BytesText :: new ( & text ) ) )
253
+ . write_event ( Event :: Start ( start_elem . clone ( ) ) )
252
254
. map_err ( |e| crate :: EdgelinkError :: InvalidOperation ( format ! ( "XML write error: {e}" ) ) ) ?;
253
- }
254
-
255
- // Write child elements (skip attribute and character keys)
256
- for ( child_key, child_value) in inner_obj {
257
- if * child_key != options. attrkey && * child_key != options. charkey {
258
- match child_value {
259
- Variant :: Array ( arr) => {
260
- // Multiple elements with same name
261
- for item in arr {
262
- if let Variant :: Object ( _item_obj) = item {
263
- let child_obj = BTreeMap :: from ( [ ( child_key. clone ( ) , item. clone ( ) ) ] ) ;
264
- self . write_object_to_xml ( writer, & child_obj, options) ?;
265
- }
266
- }
267
- }
268
- Variant :: Object ( _) => {
269
- let child_obj = BTreeMap :: from ( [ ( child_key. clone ( ) , child_value. clone ( ) ) ] ) ;
270
- self . write_object_to_xml ( writer, & child_obj, options) ?;
271
- }
272
- _ => {
273
- // Write as text element
274
- writer. write_event ( Event :: Start ( BytesStart :: new ( child_key) ) ) . map_err ( |e| {
275
- crate :: EdgelinkError :: InvalidOperation ( format ! ( "XML write error: {e}" ) )
276
- } ) ?;
277
-
278
- let text = self . variant_to_xml_text ( child_value) ;
279
- writer. write_event ( Event :: Text ( BytesText :: new ( & text) ) ) . map_err ( |e| {
280
- crate :: EdgelinkError :: InvalidOperation ( format ! ( "XML write error: {e}" ) )
281
- } ) ?;
282
-
283
- writer. write_event ( Event :: End ( BytesEnd :: new ( child_key) ) ) . map_err ( |e| {
284
- crate :: EdgelinkError :: InvalidOperation ( format ! ( "XML write error: {e}" ) )
285
- } ) ?;
286
- }
255
+ if let Some ( text_var) = inner_obj. get ( & options. charkey ) {
256
+ let text = self . variant_to_xml_text ( text_var) ;
257
+ writer
258
+ . write_event ( Event :: Text ( BytesText :: new ( & text) ) )
259
+ . map_err ( |e| crate :: EdgelinkError :: InvalidOperation ( format ! ( "XML write error: {e}" ) ) ) ?;
260
+ }
261
+ for ( child_key, child_value) in inner_obj {
262
+ if * child_key != options. attrkey && * child_key != options. charkey {
263
+ let mut single = BTreeMap :: new ( ) ;
264
+ single. insert ( child_key. clone ( ) , child_value. clone ( ) ) ;
265
+ self . write_object_to_xml ( writer, & single, options) ?;
287
266
}
288
267
}
268
+ writer
269
+ . write_event ( Event :: End ( BytesEnd :: new ( key) ) )
270
+ . map_err ( |e| crate :: EdgelinkError :: InvalidOperation ( format ! ( "XML write error: {e}" ) ) ) ?;
271
+ }
272
+ _ => {
273
+ writer
274
+ . write_event ( Event :: Start ( BytesStart :: new ( key) ) )
275
+ . map_err ( |e| crate :: EdgelinkError :: InvalidOperation ( format ! ( "XML write error: {e}" ) ) ) ?;
276
+ let text = self . variant_to_xml_text ( value) ;
277
+ writer
278
+ . write_event ( Event :: Text ( BytesText :: new ( & text) ) )
279
+ . map_err ( |e| crate :: EdgelinkError :: InvalidOperation ( format ! ( "XML write error: {e}" ) ) ) ?;
280
+ writer
281
+ . write_event ( Event :: End ( BytesEnd :: new ( key) ) )
282
+ . map_err ( |e| crate :: EdgelinkError :: InvalidOperation ( format ! ( "XML write error: {e}" ) ) ) ?;
289
283
}
290
-
291
- writer
292
- . write_event ( Event :: End ( BytesEnd :: new ( key) ) )
293
- . map_err ( |e| crate :: EdgelinkError :: InvalidOperation ( format ! ( "XML write error: {e}" ) ) ) ?;
294
- } else {
295
- // Simple value
296
- writer
297
- . write_event ( Event :: Start ( start_elem. clone ( ) ) )
298
- . map_err ( |e| crate :: EdgelinkError :: InvalidOperation ( format ! ( "XML write error: {e}" ) ) ) ?;
299
-
300
- let text = self . variant_to_xml_text ( value) ;
301
- writer
302
- . write_event ( Event :: Text ( BytesText :: new ( & text) ) )
303
- . map_err ( |e| crate :: EdgelinkError :: InvalidOperation ( format ! ( "XML write error: {e}" ) ) ) ?;
304
-
305
- writer
306
- . write_event ( Event :: End ( BytesEnd :: new ( key) ) )
307
- . map_err ( |e| crate :: EdgelinkError :: InvalidOperation ( format ! ( "XML write error: {e}" ) ) ) ?;
308
284
}
309
285
}
310
-
311
286
Ok ( ( ) )
312
287
}
313
288
@@ -325,16 +300,16 @@ impl XmlNode {
325
300
let mut opts = Xml2jsOptions :: default ( ) ;
326
301
if let Some ( opts_obj) = options_var. as_object ( ) {
327
302
if let Some ( Variant :: Bool ( ea) ) = opts_obj. get ( "explicit_array" ) {
328
- opts. explicit_array = ea . clone ( ) ;
303
+ opts. explicit_array = * ea ;
329
304
}
330
305
if let Some ( Variant :: Bool ( er) ) = opts_obj. get ( "explicit_root" ) {
331
- opts. explicit_array = er . clone ( ) ;
306
+ opts. explicit_root = * er ;
332
307
}
333
308
if let Some ( Variant :: Bool ( ma) ) = opts_obj. get ( "merge_attrs" ) {
334
- opts. merge_attrs = ma . clone ( ) ;
309
+ opts. merge_attrs = * ma ;
335
310
}
336
311
if let Some ( Variant :: Bool ( ec) ) = opts_obj. get ( "explicit_charkey" ) {
337
- opts. explicit_charkey = ec . clone ( ) ;
312
+ opts. explicit_charkey = * ec ;
338
313
}
339
314
if let Some ( Variant :: String ( ak) ) = opts_obj. get ( "attrkey" ) {
340
315
opts. attrkey = ak. clone ( ) ;
@@ -452,7 +427,7 @@ fn xml_to_variant(xml_string: &str, options: &Xml2jsOptions) -> crate::Result<Va
452
427
}
453
428
}
454
429
455
- fn attributes_to_map ( e : & BytesStart , options : & Xml2jsOptions ) -> crate :: Result < VariantObjectMap > {
430
+ fn attributes_to_map ( e : & BytesStart , _options : & Xml2jsOptions ) -> crate :: Result < VariantObjectMap > {
456
431
let mut attrs = VariantObjectMap :: new ( ) ;
457
432
for attr in e. attributes ( ) . flatten ( ) {
458
433
let key = String :: from_utf8_lossy ( attr. key . as_ref ( ) ) . to_string ( ) ;
0 commit comments