Skip to content

Error with Option<Cow<str>> element when single tag #15

@mskrip

Description

@mskrip

Case

XML source file can contain element <Element/> or <Element>text</Element> or not contain the span.
The type for it should be defined as:

#[derive(Debug, PartialEq, XmlRead, XmlWrite)]
#[xml(tag = "Parent")]
pub struct Parent<'a> {
    #[xml(child = "Element")]
    pub element: Option<Element<'a>>,
}

#[derive(Debug, PartialEq, XmlRead, XmlWrite)]
#[xml(tag = "Element")]
pub struct Element<'a> {
    #[xml(text)]
    pub content: Option<Cow<'a, str>>,
}

Expected behavior

  1. <Element>text<Element/> => text is loaded into the instance
  2. <Element/> => parent->element->content should be None
  3. not specified => parent->element should be None

Or in case of defining the Element with flatten_text the case 2 and 3 can either have the same outcome or parent-element->text should be an empty/default value (empty string) for case 2.

Actual behavior

Case 1 and 3 work as expected. With case 2, however, I'm getting the following error:

UnexpectedToken { token: "ElementEnd { end: Empty, span: StrSpan(\"/>\" 2005..2007) }" }'

Same thing when using flatten_text.

Related issue

Having

#[derive(Debug, PartialEq, XmlRead, XmlWrite)]
#[xml(tag = "Element")]
pub struct Element<'a> {
    #[xml(text)]
    pub content: Option<Cow<'a, str>>,
}

Has error

   | #[derive(Debug, PartialEq, XmlRead, XmlWrite)]
   |                                     ^^^^^^^^ expected `str`, found reference
   |
   = note: expected reference `&str`
              found reference `&&std::option::Option<std::borrow::Cow<'_, str>>`

Although it says that Option<Cow<str>> should be supported. I can implement the XmlWrite myself, but that results in the issue mentioned above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions