mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
121 lines
4.1 KiB
Rust
121 lines
4.1 KiB
Rust
// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
||
//
|
||
// 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/.
|
||
|
||
use std::str::FromStr;
|
||
|
||
use minidom::{IntoAttributeValue, IntoElements, ElementEmitter};
|
||
use chrono::{DateTime as ChronoDateTime, FixedOffset};
|
||
|
||
use error::Error;
|
||
|
||
/// Implements the DateTime profile of XEP-0082, which represents a
|
||
/// non-recurring moment in time, with an accuracy of seconds or fraction of
|
||
/// seconds, and includes a timezone.
|
||
#[derive(Debug, Clone, PartialEq)]
|
||
pub struct DateTime(ChronoDateTime<FixedOffset>);
|
||
|
||
impl FromStr for DateTime {
|
||
type Err = Error;
|
||
|
||
fn from_str(s: &str) -> Result<DateTime, Error> {
|
||
Ok(DateTime(ChronoDateTime::parse_from_rfc3339(s)?))
|
||
}
|
||
}
|
||
|
||
impl IntoAttributeValue for DateTime {
|
||
fn into_attribute_value(self) -> Option<String> {
|
||
Some(self.0.to_rfc3339())
|
||
}
|
||
}
|
||
|
||
impl IntoElements for DateTime {
|
||
fn into_elements(self, emitter: &mut ElementEmitter) {
|
||
emitter.append_text_node(self.0.to_rfc3339())
|
||
}
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
use chrono::{Datelike, Timelike};
|
||
use std::error::Error as StdError;
|
||
|
||
#[test]
|
||
fn test_size() {
|
||
assert_size!(DateTime, 16);
|
||
}
|
||
|
||
#[test]
|
||
fn test_simple() {
|
||
let date: DateTime = "2002-09-10T23:08:25Z".parse().unwrap();
|
||
assert_eq!(date.0.year(), 2002);
|
||
assert_eq!(date.0.month(), 9);
|
||
assert_eq!(date.0.day(), 10);
|
||
assert_eq!(date.0.hour(), 23);
|
||
assert_eq!(date.0.minute(), 08);
|
||
assert_eq!(date.0.second(), 25);
|
||
assert_eq!(date.0.nanosecond(), 0);
|
||
assert_eq!(date.0.timezone(), FixedOffset::east(0));
|
||
}
|
||
|
||
#[test]
|
||
fn test_invalid_date() {
|
||
// There is no thirteenth month.
|
||
let error = DateTime::from_str("2017-13-01T12:23:34Z").unwrap_err();
|
||
let message = match error {
|
||
Error::ChronoParseError(string) => string,
|
||
_ => panic!(),
|
||
};
|
||
assert_eq!(message.description(), "input is out of range");
|
||
|
||
// Timezone ≥24:00 aren’t allowed.
|
||
let error = DateTime::from_str("2017-05-27T12:11:02+25:00").unwrap_err();
|
||
let message = match error {
|
||
Error::ChronoParseError(string) => string,
|
||
_ => panic!(),
|
||
};
|
||
assert_eq!(message.description(), "input is out of range");
|
||
|
||
// Timezone without the : separator aren’t allowed.
|
||
let error = DateTime::from_str("2017-05-27T12:11:02+0100").unwrap_err();
|
||
let message = match error {
|
||
Error::ChronoParseError(string) => string,
|
||
_ => panic!(),
|
||
};
|
||
assert_eq!(message.description(), "input contains invalid characters");
|
||
|
||
// No seconds, error message could be improved.
|
||
let error = DateTime::from_str("2017-05-27T12:11+01:00").unwrap_err();
|
||
let message = match error {
|
||
Error::ChronoParseError(string) => string,
|
||
_ => panic!(),
|
||
};
|
||
assert_eq!(message.description(), "input contains invalid characters");
|
||
|
||
// TODO: maybe we’ll want to support this one, as per XEP-0082 §4.
|
||
let error = DateTime::from_str("20170527T12:11:02+01:00").unwrap_err();
|
||
let message = match error {
|
||
Error::ChronoParseError(string) => string,
|
||
_ => panic!(),
|
||
};
|
||
assert_eq!(message.description(), "input contains invalid characters");
|
||
|
||
// No timezone.
|
||
let error = DateTime::from_str("2017-05-27T12:11:02").unwrap_err();
|
||
let message = match error {
|
||
Error::ChronoParseError(string) => string,
|
||
_ => panic!(),
|
||
};
|
||
assert_eq!(message.description(), "premature end of input");
|
||
}
|
||
|
||
#[test]
|
||
fn test_serialise() {
|
||
let date = DateTime(ChronoDateTime::parse_from_rfc3339("2017-05-21T20:19:55+01:00").unwrap());
|
||
let attr = date.into_attribute_value();
|
||
assert_eq!(attr, Some(String::from("2017-05-21T20:19:55+01:00")));
|
||
}
|
||
}
|