2018-03-01 10:01:35 +01:00
|
|
|
// Copyright (c) 2017-2018 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
2017-10-31 21:17:24 +00:00
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
|
|
macro_rules! get_attr {
|
2018-12-18 15:32:05 +01:00
|
|
|
($elem:ident, $attr:tt, $type:tt) => {
|
2024-06-21 16:27:43 +02:00
|
|
|
get_attr!(
|
|
|
|
|
$elem,
|
|
|
|
|
$attr,
|
|
|
|
|
$type,
|
|
|
|
|
value,
|
|
|
|
|
value.parse().map_err(xso::error::Error::text_parse_error)?
|
|
|
|
|
)
|
2018-12-18 15:32:05 +01:00
|
|
|
};
|
2019-02-24 20:26:40 +01:00
|
|
|
($elem:ident, $attr:tt, Option, $value:ident, $func:expr) => {
|
2017-10-31 21:17:24 +00:00
|
|
|
match $elem.attr($attr) {
|
|
|
|
|
Some($value) => Some($func),
|
|
|
|
|
None => None,
|
|
|
|
|
}
|
2018-12-18 15:32:05 +01:00
|
|
|
};
|
2019-02-24 20:26:40 +01:00
|
|
|
($elem:ident, $attr:tt, Required, $value:ident, $func:expr) => {
|
2017-10-31 21:17:24 +00:00
|
|
|
match $elem.attr($attr) {
|
|
|
|
|
Some($value) => $func,
|
2018-12-18 15:32:05 +01:00
|
|
|
None => {
|
2024-06-21 16:27:43 +02:00
|
|
|
return Err(xso::error::Error::Other(
|
|
|
|
|
concat!("Required attribute '", $attr, "' missing.").into(),
|
|
|
|
|
)
|
|
|
|
|
.into());
|
2019-02-28 02:26:10 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2019-02-24 20:26:40 +01:00
|
|
|
($elem:ident, $attr:tt, Default, $value:ident, $func:expr) => {
|
2017-10-31 21:17:24 +00:00
|
|
|
match $elem.attr($attr) {
|
|
|
|
|
Some($value) => $func,
|
2024-12-19 19:37:37 +01:00
|
|
|
None => ::core::default::Default::default(),
|
2017-10-31 21:17:24 +00:00
|
|
|
}
|
2018-12-18 15:32:05 +01:00
|
|
|
};
|
2017-10-31 21:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
macro_rules! generate_attribute {
|
2022-03-22 15:58:54 +01:00
|
|
|
($(#[$meta:meta])* $elem:ident, $name:tt, {$($(#[$a_meta:meta])* $a:ident => $b:tt),+$(,)?}) => (
|
2018-05-15 00:18:15 +02:00
|
|
|
$(#[$meta])*
|
2017-10-31 21:17:24 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
|
pub enum $elem {
|
|
|
|
|
$(
|
2018-05-15 00:18:15 +02:00
|
|
|
$(#[$a_meta])*
|
2017-10-31 21:17:24 +00:00
|
|
|
$a
|
|
|
|
|
),+
|
|
|
|
|
}
|
2025-01-25 20:35:47 +01:00
|
|
|
impl ::core::str::FromStr for $elem {
|
2024-06-21 16:27:43 +02:00
|
|
|
type Err = xso::error::Error;
|
|
|
|
|
fn from_str(s: &str) -> Result<$elem, xso::error::Error> {
|
2017-10-31 21:17:24 +00:00
|
|
|
Ok(match s {
|
|
|
|
|
$($b => $elem::$a),+,
|
2024-06-21 16:27:43 +02:00
|
|
|
_ => return Err(xso::error::Error::Other(concat!("Unknown value for '", $name, "' attribute.")).into()),
|
2017-10-31 21:17:24 +00:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-26 13:12:33 +02:00
|
|
|
impl ::xso::FromXmlText for $elem {
|
|
|
|
|
fn from_xml_text(s: String) -> Result<$elem, xso::error::Error> {
|
|
|
|
|
s.parse().map_err(xso::error::Error::text_parse_error)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-19 19:37:37 +01:00
|
|
|
impl core::fmt::Display for $elem {
|
|
|
|
|
fn fmt(&self, fmt: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
|
2019-10-18 13:05:33 +02:00
|
|
|
write!(fmt, "{}", match self {
|
|
|
|
|
$($elem::$a => $b),+
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-09 17:01:42 +02:00
|
|
|
impl ::xso::AsXmlText for $elem {
|
2024-12-19 19:37:37 +01:00
|
|
|
fn as_xml_text(&self) -> Result<::alloc::borrow::Cow<'_, str>, xso::error::Error> {
|
2024-07-09 17:01:42 +02:00
|
|
|
match self {
|
|
|
|
|
$(
|
2024-12-19 19:37:37 +01:00
|
|
|
$elem::$a => Ok(::alloc::borrow::Cow::Borrowed($b))
|
2024-07-09 17:01:42 +02:00
|
|
|
),+
|
|
|
|
|
}
|
2024-06-26 13:12:33 +02:00
|
|
|
}
|
|
|
|
|
}
|
2018-05-14 16:12:56 +02:00
|
|
|
impl ::minidom::IntoAttributeValue for $elem {
|
2017-10-31 21:17:24 +00:00
|
|
|
fn into_attribute_value(self) -> Option<String> {
|
|
|
|
|
Some(String::from(match self {
|
|
|
|
|
$($elem::$a => $b),+
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
2022-03-22 15:58:54 +01:00
|
|
|
($(#[$meta:meta])* $elem:ident, $name:tt, {$($(#[$a_meta:meta])* $a:ident => $b:tt),+$(,)?}, Default = $default:ident) => (
|
2018-05-15 00:18:15 +02:00
|
|
|
$(#[$meta])*
|
2017-10-31 21:17:24 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
|
pub enum $elem {
|
|
|
|
|
$(
|
2018-05-15 00:18:15 +02:00
|
|
|
$(#[$a_meta])*
|
2017-10-31 21:17:24 +00:00
|
|
|
$a
|
|
|
|
|
),+
|
|
|
|
|
}
|
2025-01-25 20:35:47 +01:00
|
|
|
impl ::core::str::FromStr for $elem {
|
2024-06-21 16:27:43 +02:00
|
|
|
type Err = xso::error::Error;
|
|
|
|
|
fn from_str(s: &str) -> Result<$elem, xso::error::Error> {
|
2017-10-31 21:17:24 +00:00
|
|
|
Ok(match s {
|
|
|
|
|
$($b => $elem::$a),+,
|
2024-06-21 16:27:43 +02:00
|
|
|
_ => return Err(xso::error::Error::Other(concat!("Unknown value for '", $name, "' attribute.")).into()),
|
2017-10-31 21:17:24 +00:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-29 16:34:27 +02:00
|
|
|
impl ::xso::FromXmlText for $elem {
|
|
|
|
|
fn from_xml_text(s: String) -> Result<$elem, xso::error::Error> {
|
|
|
|
|
s.parse().map_err(xso::error::Error::text_parse_error)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-09 17:01:42 +02:00
|
|
|
impl ::xso::AsXmlText for $elem {
|
2024-12-19 19:37:37 +01:00
|
|
|
fn as_xml_text(&self) -> Result<alloc::borrow::Cow<'_, str>, xso::error::Error> {
|
|
|
|
|
Ok(alloc::borrow::Cow::Borrowed(match self {
|
2024-06-29 16:34:27 +02:00
|
|
|
$($elem::$a => $b),+
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[allow(unreachable_patterns)]
|
2024-12-19 19:37:37 +01:00
|
|
|
fn as_optional_xml_text(&self) -> Result<Option<alloc::borrow::Cow<'_, str>>, xso::error::Error> {
|
|
|
|
|
Ok(Some(alloc::borrow::Cow::Borrowed(match self {
|
2024-06-29 16:34:27 +02:00
|
|
|
$elem::$default => return Ok(None),
|
|
|
|
|
$($elem::$a => $b),+
|
|
|
|
|
})))
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-14 16:12:56 +02:00
|
|
|
impl ::minidom::IntoAttributeValue for $elem {
|
2017-10-31 21:17:24 +00:00
|
|
|
#[allow(unreachable_patterns)]
|
|
|
|
|
fn into_attribute_value(self) -> Option<String> {
|
|
|
|
|
Some(String::from(match self {
|
|
|
|
|
$elem::$default => return None,
|
|
|
|
|
$($elem::$a => $b),+
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-19 19:37:37 +01:00
|
|
|
impl ::core::default::Default for $elem {
|
2017-10-31 21:17:24 +00:00
|
|
|
fn default() -> $elem {
|
|
|
|
|
$elem::$default
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
2019-01-12 22:50:22 +01:00
|
|
|
($(#[$meta:meta])* $elem:ident, $name:tt, ($(#[$meta_symbol:meta])* $symbol:ident => $value:tt)) => (
|
|
|
|
|
$(#[$meta])*
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
|
pub enum $elem {
|
|
|
|
|
$(#[$meta_symbol])*
|
|
|
|
|
$symbol,
|
|
|
|
|
/// Value when absent.
|
|
|
|
|
None,
|
|
|
|
|
}
|
2025-01-25 20:35:47 +01:00
|
|
|
impl ::core::str::FromStr for $elem {
|
2024-06-21 16:27:43 +02:00
|
|
|
type Err = xso::error::Error;
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, xso::error::Error> {
|
2019-01-12 22:50:22 +01:00
|
|
|
Ok(match s {
|
|
|
|
|
$value => $elem::$symbol,
|
2024-06-21 16:27:43 +02:00
|
|
|
_ => return Err(xso::error::Error::Other(concat!("Unknown value for '", $name, "' attribute."))),
|
2019-01-12 22:50:22 +01:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
impl ::minidom::IntoAttributeValue for $elem {
|
|
|
|
|
fn into_attribute_value(self) -> Option<String> {
|
|
|
|
|
match self {
|
|
|
|
|
$elem::$symbol => Some(String::from($value)),
|
|
|
|
|
$elem::None => None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-19 19:37:37 +01:00
|
|
|
impl ::core::default::Default for $elem {
|
2019-01-12 22:50:22 +01:00
|
|
|
fn default() -> $elem {
|
|
|
|
|
$elem::None
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-08 18:30:14 +02:00
|
|
|
impl ::xso::FromXmlText for $elem {
|
|
|
|
|
fn from_xml_text(s: String) -> Result<$elem, xso::error::Error> {
|
|
|
|
|
s.parse().map_err(xso::error::Error::text_parse_error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
impl ::xso::AsXmlText for $elem {
|
2024-12-19 19:37:37 +01:00
|
|
|
fn as_xml_text(&self) -> Result<::alloc::borrow::Cow<'_, str>, xso::error::Error> {
|
|
|
|
|
Ok(::alloc::borrow::Cow::Borrowed($value))
|
2024-08-08 18:30:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[allow(unreachable_patterns)]
|
2024-12-19 19:37:37 +01:00
|
|
|
fn as_optional_xml_text(&self) -> Result<Option<alloc::borrow::Cow<'_, str>>, xso::error::Error> {
|
|
|
|
|
Ok(Some(alloc::borrow::Cow::Borrowed(match self {
|
2024-08-08 18:30:14 +02:00
|
|
|
$elem::$symbol => $value,
|
|
|
|
|
$elem::None => return Ok(None),
|
|
|
|
|
})))
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-12 22:50:22 +01:00
|
|
|
);
|
2019-02-28 03:10:21 +01:00
|
|
|
($(#[$meta:meta])* $elem:ident, $name:tt, $type:tt, Default = $default:expr) => (
|
|
|
|
|
$(#[$meta])*
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
|
pub struct $elem(pub $type);
|
2025-01-25 20:35:47 +01:00
|
|
|
impl ::core::str::FromStr for $elem {
|
2024-06-21 16:27:43 +02:00
|
|
|
type Err = xso::error::Error;
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, xso::error::Error> {
|
|
|
|
|
Ok($elem($type::from_str(s).map_err(xso::error::Error::text_parse_error)?))
|
2019-02-28 03:10:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
impl ::minidom::IntoAttributeValue for $elem {
|
|
|
|
|
fn into_attribute_value(self) -> Option<String> {
|
|
|
|
|
match self {
|
|
|
|
|
$elem($default) => None,
|
|
|
|
|
$elem(value) => Some(format!("{}", value)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-19 19:37:37 +01:00
|
|
|
impl ::core::default::Default for $elem {
|
2019-02-28 03:10:21 +01:00
|
|
|
fn default() -> $elem {
|
|
|
|
|
$elem($default)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-04 17:07:58 +02:00
|
|
|
impl ::xso::FromXmlText for $elem {
|
|
|
|
|
fn from_xml_text(s: String) -> Result<$elem, xso::error::Error> {
|
|
|
|
|
s.parse().map_err(xso::error::Error::text_parse_error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
impl ::xso::AsXmlText for $elem {
|
2024-12-19 19:37:37 +01:00
|
|
|
fn as_xml_text(&self) -> Result<::alloc::borrow::Cow<'_, str>, xso::error::Error> {
|
|
|
|
|
Ok(::alloc::borrow::Cow::Owned(format!("{}", self.0)))
|
2024-08-04 17:07:58 +02:00
|
|
|
}
|
|
|
|
|
|
2024-12-19 19:37:37 +01:00
|
|
|
fn as_optional_xml_text(&self) -> Result<Option<::alloc::borrow::Cow<'_, str>>, xso::error::Error> {
|
2024-08-04 17:07:58 +02:00
|
|
|
match self.0 {
|
|
|
|
|
$default => Ok(None),
|
2024-12-19 19:37:37 +01:00
|
|
|
_ => Ok(Some(::alloc::borrow::Cow::Owned(format!("{}", self.0)))),
|
2024-08-04 17:07:58 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-28 03:10:21 +01:00
|
|
|
);
|
2017-10-31 21:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
2017-11-24 05:44:58 +00:00
|
|
|
macro_rules! generate_attribute_enum {
|
2022-03-22 15:58:54 +01:00
|
|
|
($(#[$meta:meta])* $elem:ident, $name:tt, $ns:ident, $attr:tt, {$($(#[$enum_meta:meta])* $enum:ident => $enum_name:tt),+$(,)?}) => (
|
2017-11-24 05:44:58 +00:00
|
|
|
$(#[$meta])*
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
|
pub enum $elem {
|
|
|
|
|
$(
|
2018-12-29 18:29:11 +01:00
|
|
|
$(#[$enum_meta])*
|
|
|
|
|
$enum
|
2017-11-24 05:44:58 +00:00
|
|
|
),+
|
|
|
|
|
}
|
2024-12-19 19:37:37 +01:00
|
|
|
impl ::core::convert::TryFrom<minidom::Element> for $elem {
|
2024-06-21 16:27:43 +02:00
|
|
|
type Error = xso::error::FromElementError;
|
2024-07-24 20:28:22 +02:00
|
|
|
fn try_from(elem: minidom::Element) -> Result<$elem, xso::error::FromElementError> {
|
2017-11-24 05:44:58 +00:00
|
|
|
check_ns_only!(elem, $name, $ns);
|
|
|
|
|
check_no_children!(elem, $name);
|
|
|
|
|
check_no_unknown_attributes!(elem, $name, [$attr]);
|
2019-02-24 20:48:19 +01:00
|
|
|
Ok(match get_attr!(elem, $attr, Required) {
|
2017-11-24 05:44:58 +00:00
|
|
|
$($enum_name => $elem::$enum,)+
|
2024-06-21 16:27:43 +02:00
|
|
|
_ => return Err(xso::error::Error::Other(concat!("Invalid ", $name, " ", $attr, " value.")).into()),
|
2017-11-24 05:44:58 +00:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-03 14:41:33 +02:00
|
|
|
|
|
|
|
|
impl ::xso::FromXml for $elem {
|
|
|
|
|
type Builder = ::xso::minidom_compat::FromEventsViaElement<$elem>;
|
|
|
|
|
|
|
|
|
|
fn from_events(
|
|
|
|
|
qname: ::xso::exports::rxml::QName,
|
|
|
|
|
attrs: ::xso::exports::rxml::AttrMap,
|
|
|
|
|
) -> Result<Self::Builder, ::xso::error::FromEventsError> {
|
|
|
|
|
if qname.0 != crate::ns::$ns || qname.1 != $name {
|
|
|
|
|
return Err(::xso::error::FromEventsError::Mismatch {
|
|
|
|
|
name: qname,
|
|
|
|
|
attrs,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
Self::Builder::new(qname, attrs)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-24 20:28:22 +02:00
|
|
|
impl From<$elem> for minidom::Element {
|
|
|
|
|
fn from(elem: $elem) -> minidom::Element {
|
|
|
|
|
minidom::Element::builder($name, crate::ns::$ns)
|
2018-12-29 18:29:11 +01:00
|
|
|
.attr($attr, match elem {
|
|
|
|
|
$($elem::$enum => $enum_name,)+
|
|
|
|
|
})
|
|
|
|
|
.build()
|
2017-11-24 05:44:58 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-08-03 14:41:33 +02:00
|
|
|
|
|
|
|
|
impl ::xso::AsXml for $elem {
|
|
|
|
|
type ItemIter<'x> = ::xso::minidom_compat::AsItemsViaElement<'x>;
|
|
|
|
|
|
|
|
|
|
fn as_xml_iter(&self) -> Result<Self::ItemIter<'_>, ::xso::error::Error> {
|
|
|
|
|
::xso::minidom_compat::AsItemsViaElement::new(self.clone())
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-24 05:44:58 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-31 21:17:24 +00:00
|
|
|
macro_rules! check_self {
|
2018-12-18 15:32:05 +01:00
|
|
|
($elem:ident, $name:tt, $ns:ident) => {
|
2017-10-31 21:17:24 +00:00
|
|
|
check_self!($elem, $name, $ns, $name);
|
2018-12-18 15:32:05 +01:00
|
|
|
};
|
2024-03-02 09:19:49 +01:00
|
|
|
($elem:ident, $name:tt, $ns:ident, $pretty_name:tt) => {
|
|
|
|
|
if !$elem.is($name, crate::ns::$ns) {
|
2024-06-21 16:27:43 +02:00
|
|
|
return Err(xso::error::FromElementError::Mismatch($elem));
|
2024-03-02 09:19:49 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-31 21:17:24 +00:00
|
|
|
macro_rules! check_ns_only {
|
2018-12-18 15:32:05 +01:00
|
|
|
($elem:ident, $name:tt, $ns:ident) => {
|
2018-12-18 15:27:30 +01:00
|
|
|
if !$elem.has_ns(crate::ns::$ns) {
|
2024-06-21 16:27:43 +02:00
|
|
|
return Err(xso::error::Error::Other(
|
|
|
|
|
concat!("This is not a ", $name, " element.").into(),
|
|
|
|
|
)
|
|
|
|
|
.into());
|
2017-10-31 21:17:24 +00:00
|
|
|
}
|
2018-12-18 15:32:05 +01:00
|
|
|
};
|
2017-10-31 21:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
macro_rules! check_no_children {
|
2018-12-18 15:32:05 +01:00
|
|
|
($elem:ident, $name:tt) => {
|
2019-01-12 22:00:46 +01:00
|
|
|
#[cfg(not(feature = "disable-validation"))]
|
2017-10-31 21:17:24 +00:00
|
|
|
for _ in $elem.children() {
|
2024-06-21 16:27:43 +02:00
|
|
|
return Err(xso::error::Error::Other(
|
|
|
|
|
concat!("Unknown child in ", $name, " element.").into(),
|
|
|
|
|
)
|
|
|
|
|
.into());
|
2017-10-31 21:17:24 +00:00
|
|
|
}
|
2018-12-18 15:32:05 +01:00
|
|
|
};
|
2017-10-31 21:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
macro_rules! check_no_attributes {
|
2018-12-18 15:32:05 +01:00
|
|
|
($elem:ident, $name:tt) => {
|
2019-01-12 22:00:46 +01:00
|
|
|
#[cfg(not(feature = "disable-validation"))]
|
2018-05-12 20:25:59 +02:00
|
|
|
for _ in $elem.attrs() {
|
2024-06-21 16:27:43 +02:00
|
|
|
return Err(xso::error::Error::Other(
|
|
|
|
|
concat!("Unknown attribute in ", $name, " element.").into(),
|
|
|
|
|
)
|
|
|
|
|
.into());
|
2018-05-12 20:25:59 +02:00
|
|
|
}
|
2018-12-18 15:32:05 +01:00
|
|
|
};
|
2017-10-31 21:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
macro_rules! check_no_unknown_attributes {
|
|
|
|
|
($elem:ident, $name:tt, [$($attr:tt),*]) => (
|
2019-01-12 22:00:46 +01:00
|
|
|
#[cfg(not(feature = "disable-validation"))]
|
2017-10-31 21:17:24 +00:00
|
|
|
for (_attr, _) in $elem.attrs() {
|
|
|
|
|
$(
|
2018-12-29 18:29:11 +01:00
|
|
|
if _attr == $attr {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-10-31 21:17:24 +00:00
|
|
|
)*
|
2024-06-21 16:27:43 +02:00
|
|
|
return Err(xso::error::Error::Other(concat!("Unknown attribute in ", $name, " element.")).into());
|
2017-10-31 21:17:24 +00:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
macro_rules! generate_id {
|
2018-07-02 13:47:32 +02:00
|
|
|
($(#[$meta:meta])* $elem:ident) => (
|
|
|
|
|
$(#[$meta])*
|
2017-10-31 21:17:24 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
|
pub struct $elem(pub String);
|
2025-01-25 20:35:47 +01:00
|
|
|
impl ::core::str::FromStr for $elem {
|
2024-06-21 16:27:43 +02:00
|
|
|
type Err = xso::error::Error;
|
|
|
|
|
fn from_str(s: &str) -> Result<$elem, xso::error::Error> {
|
2017-10-31 21:17:24 +00:00
|
|
|
// TODO: add a way to parse that differently when needed.
|
|
|
|
|
Ok($elem(String::from(s)))
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-26 13:12:33 +02:00
|
|
|
impl ::xso::FromXmlText for $elem {
|
|
|
|
|
fn from_xml_text(s: String) -> Result<$elem, xso::error::Error> {
|
|
|
|
|
Ok(Self(s))
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-09 17:01:42 +02:00
|
|
|
impl ::xso::AsXmlText for $elem {
|
2024-12-19 19:37:37 +01:00
|
|
|
fn as_xml_text(&self) ->Result<::alloc::borrow::Cow<'_, str>, xso::error::Error> {
|
|
|
|
|
Ok(::alloc::borrow::Cow::Borrowed(self.0.as_str()))
|
2024-06-26 13:12:33 +02:00
|
|
|
}
|
|
|
|
|
}
|
2018-05-14 16:12:56 +02:00
|
|
|
impl ::minidom::IntoAttributeValue for $elem {
|
2017-10-31 21:17:24 +00:00
|
|
|
fn into_attribute_value(self) -> Option<String> {
|
|
|
|
|
Some(self.0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
macro_rules! generate_elem_id {
|
2024-06-29 16:14:12 +02:00
|
|
|
($(#[$meta:meta])* $elem:ident, $name:literal, $ns:ident) => (
|
2019-09-05 15:34:21 +02:00
|
|
|
generate_elem_id!($(#[$meta])* $elem, $name, $ns, String);
|
2025-01-25 20:35:47 +01:00
|
|
|
impl ::core::str::FromStr for $elem {
|
2024-06-21 16:27:43 +02:00
|
|
|
type Err = xso::error::Error;
|
|
|
|
|
fn from_str(s: &str) -> Result<$elem, xso::error::Error> {
|
2017-10-31 21:17:24 +00:00
|
|
|
// TODO: add a way to parse that differently when needed.
|
|
|
|
|
Ok($elem(String::from(s)))
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-05 15:34:21 +02:00
|
|
|
);
|
2024-06-29 16:14:12 +02:00
|
|
|
($(#[$meta:meta])* $elem:ident, $name:literal, $ns:ident, $type:ty) => (
|
2019-09-05 15:34:21 +02:00
|
|
|
$(#[$meta])*
|
2024-06-29 16:14:12 +02:00
|
|
|
#[derive(xso::FromXml, xso::AsXml, Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
|
#[xml(namespace = crate::ns::$ns, name = $name)]
|
|
|
|
|
pub struct $elem(#[xml(text)] pub $type);
|
2017-10-31 21:17:24 +00:00
|
|
|
);
|
|
|
|
|
}
|
2017-11-16 20:17:11 +00:00
|
|
|
|
2018-12-18 15:43:49 +01:00
|
|
|
#[cfg(test)]
|
2018-10-26 14:26:16 +02:00
|
|
|
macro_rules! assert_size (
|
|
|
|
|
($t:ty, $sz:expr) => (
|
2024-12-19 19:37:37 +01:00
|
|
|
assert_eq!(::core::mem::size_of::<$t>(), $sz);
|
2018-10-26 14:26:16 +02:00
|
|
|
);
|
|
|
|
|
);
|