|
473 | 473 | //! <!-- 8 ===================================================================================== --> |
474 | 474 | //! <tr> |
475 | 475 | //! <td> |
476 | | -//! An XML with different root tag names: |
| 476 | +//! An XML with different root tag names, as well as text / CDATA content: |
477 | 477 | //! |
478 | 478 | //! ```xml |
479 | 479 | //! <one field1="...">...</one> |
|
483 | 483 | //! <field2>...</field2> |
484 | 484 | //! </two> |
485 | 485 | //! ``` |
| 486 | +//! ```xml |
| 487 | +//! Text <![CDATA[or (mixed) |
| 488 | +//! CDATA]]> content |
| 489 | +//! ``` |
486 | 490 | //! </td> |
487 | 491 | //! <td> |
488 | 492 | //! |
489 | 493 | //! An enum where each variant have a name of the possible root tag. The name of |
490 | 494 | //! the enum itself does not matter. |
491 | 495 | //! |
| 496 | +//! If you need to get a textual content, mark a variant with `#[serde(rename = "$text")]`. |
| 497 | +//! |
492 | 498 | //! All these structs can be used to deserialize from any XML on the |
493 | 499 | //! left side depending on amount of information that you want to get: |
494 | 500 | //! |
|
503 | 509 | //! enum AnyName { |
504 | 510 | //! One { #[serde(rename = "@field1")] field1: T }, |
505 | 511 | //! Two { field2: U }, |
| 512 | +//! |
| 513 | +//! /// Use unit variant, if you do not care of a content. |
| 514 | +//! /// You can use tuple variant if you want to parse |
| 515 | +//! /// textual content as an xs:list. |
| 516 | +//! /// Struct variants are not supported and will return |
| 517 | +//! /// Err(Unsupported) |
| 518 | +//! #[serde(rename = "$text")] |
| 519 | +//! Text(String), |
506 | 520 | //! } |
507 | 521 | //! # assert_eq!(AnyName::One { field1: () }, quick_xml::de::from_str(r#"<one field1="...">...</one>"#).unwrap()); |
508 | 522 | //! # assert_eq!(AnyName::Two { field2: () }, quick_xml::de::from_str(r#"<two><field2>...</field2></two>"#).unwrap()); |
| 523 | +//! # assert_eq!(AnyName::Text("text".into()), quick_xml::de::from_str(r#"text"#).unwrap()); |
| 524 | +//! # // TODO: After #474 parse mixed content |
509 | 525 | //! ``` |
510 | 526 | //! ``` |
511 | 527 | //! # use pretty_assertions::assert_eq; |
|
523 | 539 | //! // `field1` content discarded |
524 | 540 | //! One, |
525 | 541 | //! Two(Two), |
| 542 | +//! #[serde(rename = "$text")] |
| 543 | +//! Text, |
526 | 544 | //! } |
527 | 545 | //! # assert_eq!(AnyName::One, quick_xml::de::from_str(r#"<one field1="...">...</one>"#).unwrap()); |
528 | 546 | //! # assert_eq!(AnyName::Two(Two { field2: () }), quick_xml::de::from_str(r#"<two><field2>...</field2></two>"#).unwrap()); |
| 547 | +//! # assert_eq!(AnyName::Text, quick_xml::de::from_str(r#"text"#).unwrap()); |
| 548 | +//! # // TODO: After #474 parse mixed content |
529 | 549 | //! ``` |
530 | 550 | //! ``` |
531 | 551 | //! # use pretty_assertions::assert_eq; |
|
535 | 555 | //! #[serde(rename_all = "snake_case")] |
536 | 556 | //! enum AnyName { |
537 | 557 | //! One, |
538 | | -//! // the <two> will be mapped to this |
| 558 | +//! // the <two> and textual content will be mapped to this |
539 | 559 | //! #[serde(other)] |
540 | 560 | //! Other, |
541 | 561 | //! } |
542 | 562 | //! # assert_eq!(AnyName::One, quick_xml::de::from_str(r#"<one field1="...">...</one>"#).unwrap()); |
543 | 563 | //! # assert_eq!(AnyName::Other, quick_xml::de::from_str(r#"<two><field2>...</field2></two>"#).unwrap()); |
| 564 | +//! # assert_eq!(AnyName::Other, quick_xml::de::from_str(r#"text"#).unwrap()); |
| 565 | +//! # // TODO: After #474 parse mixed content |
544 | 566 | //! ``` |
545 | 567 | //! <div style="background:rgba(120,145,255,0.45);padding:0.75em;"> |
546 | 568 | //! |
|
569 | 591 | //! <two>...</two> |
570 | 592 | //! </any-tag> |
571 | 593 | //! ``` |
| 594 | +//! ```xml |
| 595 | +//! <any-tag field="..."> |
| 596 | +//! Text <![CDATA[or (mixed) |
| 597 | +//! CDATA]]> content |
| 598 | +//! </any-tag> |
| 599 | +//! ``` |
572 | 600 | //! </td> |
573 | 601 | //! <td> |
574 | 602 | //! |
575 | 603 | //! A structure with a field which type is an `enum`. |
576 | 604 | //! |
| 605 | +//! If you need to get a textual content, mark a variant with `#[serde(rename = "$text")]`. |
| 606 | +//! |
577 | 607 | //! Names of the enum, struct, and struct field with `Choice` type does not matter: |
578 | 608 | //! |
579 | 609 | //! ``` |
|
586 | 616 | //! enum Choice { |
587 | 617 | //! One, |
588 | 618 | //! Two, |
| 619 | +//! |
| 620 | +//! /// Use unit variant, if you do not care of a content. |
| 621 | +//! /// You can use tuple variant if you want to parse |
| 622 | +//! /// textual content as an xs:list. |
| 623 | +//! /// Struct variants are not supported and will return |
| 624 | +//! /// Err(Unsupported) |
| 625 | +//! #[serde(rename = "$text")] |
| 626 | +//! Text(String), |
589 | 627 | //! } |
590 | 628 | //! # #[derive(Debug, PartialEq)] |
591 | 629 | //! #[derive(Deserialize)] |
|
604 | 642 | //! # AnyName { field: (), any_name: Choice::Two }, |
605 | 643 | //! # quick_xml::de::from_str(r#"<any-tag field="..."><two>...</two></any-tag>"#).unwrap(), |
606 | 644 | //! # ); |
| 645 | +//! # assert_eq!( |
| 646 | +//! # AnyName { field: (), any_name: Choice::Text("text".into()) }, |
| 647 | +//! # // TODO: After #474 parse mixed content |
| 648 | +//! # quick_xml::de::from_str(r#"<any-tag field="...">text</any-tag>"#).unwrap(), |
| 649 | +//! # ); |
607 | 650 | //! ``` |
608 | 651 | //! </td> |
609 | 652 | //! </tr> |
|
1023 | 1066 | //! # ); |
1024 | 1067 | //! ``` |
1025 | 1068 | //! ```ignore |
1026 | | -//! // FIXME: Custom("unknown variant `text`, expected |
1027 | | -//! // one of `one`, `two`, `$value`") |
| 1069 | +//! // FIXME: #474 |
1028 | 1070 | //! # use pretty_assertions::assert_eq; |
1029 | 1071 | //! # use serde::Deserialize; |
1030 | 1072 | //! # #[derive(Debug, PartialEq)] |
|
1033 | 1075 | //! enum Choice { |
1034 | 1076 | //! One, |
1035 | 1077 | //! Two, |
1036 | | -//! #[serde(rename = "$value")] |
| 1078 | +//! #[serde(rename = "$text")] |
1037 | 1079 | //! Other(String), |
1038 | 1080 | //! } |
1039 | 1081 | //! type AnyName = Vec<Choice>; |
|
1202 | 1244 | //! You MUST specify `#[serde(rename = "$value")]` on that field: |
1203 | 1245 | //! |
1204 | 1246 | //! ```ignore |
1205 | | -//! // FIXME: Custom("unknown variant `text`, expected |
1206 | | -//! // one of `one`, `two`, `$value`") |
| 1247 | +//! // FIXME: #474 |
1207 | 1248 | //! # use pretty_assertions::assert_eq; |
1208 | 1249 | //! # use serde::Deserialize; |
1209 | 1250 | //! # #[derive(Debug, PartialEq)] |
|
1212 | 1253 | //! enum Choice { |
1213 | 1254 | //! One, |
1214 | 1255 | //! Two, |
1215 | | -//! #[serde(rename = "$value")] |
| 1256 | +//! #[serde(rename = "$text")] |
1216 | 1257 | //! Other(String), |
1217 | 1258 | //! } |
1218 | 1259 | //! # #[derive(Debug, PartialEq)] |
|
1248 | 1289 | //! # ); |
1249 | 1290 | //! ``` |
1250 | 1291 | //! ```ignore |
1251 | | -//! // FIXME: Custom("unknown variant `text`, expected |
1252 | | -//! // one of `one`, `two`, `$value`") |
| 1292 | +//! // FIXME: #474 |
1253 | 1293 | //! # use pretty_assertions::assert_eq; |
1254 | 1294 | //! # use serde::Deserialize; |
1255 | 1295 | //! # #[derive(Debug, PartialEq)] |
|
1258 | 1298 | //! enum Choice { |
1259 | 1299 | //! One, |
1260 | 1300 | //! Two, |
1261 | | -//! #[serde(rename = "$value")] |
| 1301 | +//! #[serde(rename = "$text")] |
1262 | 1302 | //! Other(String), |
1263 | 1303 | //! } |
1264 | 1304 | //! # #[derive(Debug, PartialEq)] |
@@ -2433,8 +2473,7 @@ where |
2433 | 2473 | where |
2434 | 2474 | V: Visitor<'de>, |
2435 | 2475 | { |
2436 | | - let value = visitor.visit_enum(var::EnumAccess::new(self))?; |
2437 | | - Ok(value) |
| 2476 | + visitor.visit_enum(var::EnumAccess::new(self)) |
2438 | 2477 | } |
2439 | 2478 |
|
2440 | 2479 | fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, DeError> |
|
0 commit comments