Provide more context in DuplicateValue errors

This commit is contained in:
Dirkjan Ochtman 2024-06-07 13:46:47 +02:00
parent 775bad82a4
commit dfc5894edf
6 changed files with 26 additions and 25 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "instant-xml-macros"
version = "0.4.2"
version = "0.5.0"
edition = "2021"
rust-version = "1.61"
workspace = ".."

View File

@ -94,7 +94,7 @@ fn deserialize_scalar_enum(
use ::instant_xml::Error;
if into.is_some() {
return Err(Error::DuplicateValue);
return Err(Error::DuplicateValue(field));
}
let cow_str = match deserializer.take_str()? {

View File

@ -1,6 +1,6 @@
[package]
name = "instant-xml"
version = "0.4.0"
version = "0.5.0"
edition = "2021"
rust-version = "1.61"
workspace = ".."
@ -12,7 +12,7 @@ readme = "../README.md"
[dependencies]
chrono = { version = "0.4.23", optional = true }
macros = { package = "instant-xml-macros", version = "0.4.0", path = "../instant-xml-macros" }
macros = { package = "instant-xml-macros", version = "0.5.0", path = "../instant-xml-macros" }
thiserror = "1.0.29"
xmlparser = "0.13.3"

View File

@ -363,11 +363,11 @@ impl<'xml> Iterator for Context<'xml> {
pub fn borrow_cow_str<'a, 'xml: 'a>(
into: &mut CowStrAccumulator<'xml, 'a>,
_: &'static str,
field: &'static str,
deserializer: &mut Deserializer<'_, 'xml>,
) -> Result<(), Error> {
if into.inner.is_some() {
return Err(Error::DuplicateValue);
return Err(Error::DuplicateValue(field));
}
match deserializer.take_str()? {
@ -381,11 +381,11 @@ pub fn borrow_cow_str<'a, 'xml: 'a>(
pub fn borrow_cow_slice_u8<'xml>(
into: &mut Option<Cow<'xml, [u8]>>,
_: &'static str,
field: &'static str,
deserializer: &mut Deserializer<'_, 'xml>,
) -> Result<(), Error> {
if into.is_some() {
return Err(Error::DuplicateValue);
return Err(Error::DuplicateValue(field));
}
if let Some(value) = deserializer.take_str()? {

View File

@ -13,11 +13,12 @@ use crate::{Accumulate, Deserializer, Error, FromXml, Id, Kind, Serializer, ToXm
// Deserializer
pub fn from_xml_str<T: FromStr>(
deserializer: &mut Deserializer<'_, '_>,
into: &mut Option<T>,
field: &'static str,
deserializer: &mut Deserializer<'_, '_>,
) -> Result<(), Error> {
if into.is_some() {
return Err(Error::DuplicateValue);
return Err(Error::DuplicateValue(field));
}
let value = match deserializer.take_str()? {
@ -54,7 +55,7 @@ impl<'xml, T: FromStr> FromXml<'xml> for FromXmlStr<T> {
deserializer: &mut Deserializer<'_, 'xml>,
) -> Result<(), Error> {
if into.is_some() {
return Err(Error::DuplicateValue);
return Err(Error::DuplicateValue(field));
}
let value = match deserializer.take_str()? {
@ -93,7 +94,7 @@ impl<'xml> FromXml<'xml> for bool {
deserializer: &mut Deserializer<'cx, 'xml>,
) -> Result<(), Error> {
if into.is_some() {
return Err(Error::DuplicateValue);
return Err(Error::DuplicateValue(field));
}
let value = match deserializer.take_str()? {
@ -189,7 +190,7 @@ macro_rules! from_xml_for_number {
deserializer: &mut Deserializer<'cx, 'xml>,
) -> Result<(), Error> {
if into.is_some() {
return Err(Error::DuplicateValue);
return Err(Error::DuplicateValue(field));
}
let mut value = None;
@ -235,7 +236,7 @@ impl<'xml> FromXml<'xml> for char {
deserializer: &mut Deserializer<'cx, 'xml>,
) -> Result<(), Error> {
if into.is_some() {
return Err(Error::DuplicateValue);
return Err(Error::DuplicateValue(field));
}
let mut value = None;
@ -262,11 +263,11 @@ impl<'xml> FromXml<'xml> for String {
fn deserialize<'cx>(
into: &mut Self::Accumulator,
_: &'static str,
field: &'static str,
deserializer: &mut Deserializer<'cx, 'xml>,
) -> Result<(), Error> {
if into.is_some() {
return Err(Error::DuplicateValue);
return Err(Error::DuplicateValue(field));
}
match deserializer.take_str()? {
@ -292,11 +293,11 @@ impl<'xml, 'a> FromXml<'xml> for Cow<'a, str> {
fn deserialize(
into: &mut Self::Accumulator,
_: &'static str,
field: &'static str,
deserializer: &mut Deserializer<'_, 'xml>,
) -> Result<(), Error> {
if into.inner.is_some() {
return Err(Error::DuplicateValue);
return Err(Error::DuplicateValue(field));
}
let value = match deserializer.take_str()? {
@ -595,11 +596,11 @@ impl<'xml> FromXml<'xml> for DateTime<Utc> {
fn deserialize<'cx>(
into: &mut Self::Accumulator,
_: &'static str,
field: &'static str,
deserializer: &mut Deserializer<'cx, 'xml>,
) -> Result<(), Error> {
if into.is_some() {
return Err(Error::DuplicateValue);
return Err(Error::DuplicateValue(field));
}
let value = match deserializer.take_str()? {
@ -657,11 +658,11 @@ impl<'xml> FromXml<'xml> for NaiveDate {
fn deserialize<'cx>(
into: &mut Self::Accumulator,
_: &'static str,
field: &'static str,
deserializer: &mut Deserializer<'cx, 'xml>,
) -> Result<(), Error> {
if into.is_some() {
return Err(Error::DuplicateValue);
return Err(Error::DuplicateValue(field));
}
let value = match deserializer.take_str()? {
@ -729,7 +730,7 @@ impl<'xml> FromXml<'xml> for IpAddr {
deserializer: &mut Deserializer<'cx, 'xml>,
) -> Result<(), Error> {
if into.is_some() {
return Err(Error::DuplicateValue);
return Err(Error::DuplicateValue(field));
}
let mut value = None;

View File

@ -151,8 +151,8 @@ pub enum Error {
UnexpectedState(&'static str),
#[error("expected scalar, found {0}")]
ExpectedScalar(String),
#[error("duplicate value")]
DuplicateValue,
#[error("duplicate value for {0}")]
DuplicateValue(&'static str),
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]