Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tuple struct causes proc-macro panic #191

Open
Mammux opened this issue Aug 12, 2024 · 2 comments
Open

Tuple struct causes proc-macro panic #191

Mammux opened this issue Aug 12, 2024 · 2 comments

Comments

@Mammux
Copy link

Mammux commented Aug 12, 2024

use yaserde_derive::YaDeserialize;
#[derive(YaDeserialize)]
pub struct WrappedString(pub String);

causes:

error: proc-macro derive panicked
2 | #[derive(YaDeserialize)]
| ^^^^^^^^^^^^^
|
= help: message: called Option::unwrap() on a None value

@OvermindDL1
Copy link

Same, still happening, relevant part of backtrace:

  18:     0x79f818cc9b93 - yaserde_derive::common::field::YaSerdeField::renamed_label_without_namespace::h389779400a5143d4
  19:     0x79f818cc9f6a - yaserde_derive::common::field::YaSerdeField::get_visitor_ident::h68e638260011394b
  20:     0x79f818cb066b - yaserde_derive::de::expand_struct::parse::{{closure}}::{{closure}}::h717fce7aebfe5022

@AMCON-Gerst
Copy link

I had the same issue.
We made a workaround with a self implemented serialize and deserialize method.

use std::fmt::{Debug, Display};
use std::io::{Read, Write};
use xml::attribute::OwnedAttribute;
use xml::namespace::Namespace;
use xml::reader::XmlEvent;
use yaserde::de::Deserializer;
use yaserde::ser::Serializer;
use yaserde::{YaDeserialize, YaSerialize};

#[derive(PartialEq, Debug, Default)]
pub struct TestNumber(pub u32);

impl YaSerialize for TestNumber {
    fn serialize<W: Write>(&self, writer: &mut Serializer<W>) -> Result<(), String> {
        // if serializing an attribute, we don't have a start and end element
        let mut start_written = false;

        if let Some(start_event_name) = writer.get_start_event_name() {
            writer
                .write(xml::writer::XmlEvent::start_element(start_event_name.as_str()))
                .map_err(|e| e.to_string())?;

            start_written = true;
        }

        writer.write(xml::writer::XmlEvent::characters(self.0.to_string().as_str()))
            .map_err(|e| e.to_string())?;

        if start_written {
            writer
                .write(xml::writer::XmlEvent::end_element())
                .map_err(|e| e.to_string())?;
        }

        Ok(())
    }

    fn serialize_attributes(&self, attributes: Vec<OwnedAttribute>, namespace: Namespace) -> Result<(Vec<OwnedAttribute>, Namespace), String> {
        Ok((attributes, namespace))
    }
}

impl YaDeserialize for TestNumber {
    fn deserialize<R: Read>(reader: &mut Deserializer<R>) -> Result<Self, String> {
        let mut result = Self::default();

        match reader.next_event()? {
            XmlEvent::Characters(text) => result.0 = text.parse::<u32>().map_err(|e| e.to_string())?,
            _ => return Err("expected only characters".to_string())
        }

        Ok(result)
    }
}

#[derive(PartialEq, Debug, Default)]
pub struct TestTestNumber(pub TestNumber);


impl YaSerialize for TestTestNumber {
    fn serialize<W: Write>(&self, writer: &mut Serializer<W>) -> Result<(), String> {
        self.0.serialize(writer)
    }

    fn serialize_attributes(&self, attributes: Vec<OwnedAttribute>, namespace: Namespace) -> Result<(Vec<OwnedAttribute>, Namespace), String> {
        Ok((attributes, namespace))
    }
}

impl YaDeserialize for TestTestNumber {
    fn deserialize<R: Read>(reader: &mut Deserializer<R>) -> Result<Self, String> {
        Ok(TestTestNumber(TestNumber::deserialize(reader)?))
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants