parsers: add xso text trait implementations to types

This allows them to be used as attribute in xso-proc derived structs.
This commit is contained in:
Jonas Schäfer 2024-06-26 13:12:33 +02:00
commit 4e9c4883a3
2 changed files with 36 additions and 1 deletions

View file

@ -4,9 +4,12 @@
// 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/.
use std::str::FromStr;
use xso::{error::Error, FromXmlText, IntoXmlText};
use chrono::{DateTime as ChronoDateTime, FixedOffset};
use minidom::{IntoAttributeValue, Node};
use std::str::FromStr;
/// Implements the DateTime profile of XEP-0082, which represents a
/// non-recurring moment in time, with an accuracy of seconds or fraction of
@ -39,6 +42,18 @@ impl FromStr for DateTime {
}
}
impl FromXmlText for DateTime {
fn from_xml_text(s: String) -> Result<Self, Error> {
s.parse().map_err(Error::text_parse_error)
}
}
impl IntoXmlText for DateTime {
fn into_xml_text(self) -> Result<String, Error> {
Ok(self.0.to_rfc3339())
}
}
impl IntoAttributeValue for DateTime {
fn into_attribute_value(self) -> Option<String> {
Some(self.0.to_rfc3339())