cloudevents/event/
types.rs

1use chrono::{DateTime, Utc};
2use url::Url;
3
4/// Trait to define conversion to [`Url`]
5pub trait TryIntoUrl {
6    fn into_url(self) -> Result<Url, url::ParseError>;
7}
8
9impl TryIntoUrl for Url {
10    fn into_url(self) -> Result<Url, url::ParseError> {
11        Ok(self)
12    }
13}
14
15impl TryIntoUrl for &str {
16    fn into_url(self) -> Result<Url, url::ParseError> {
17        Url::parse(self)
18    }
19}
20
21impl TryIntoUrl for String {
22    fn into_url(self) -> Result<Url, url::ParseError> {
23        self.as_str().into_url()
24    }
25}
26
27/// Trait to define conversion to [`DateTime`]
28pub trait TryIntoTime {
29    fn into_time(self) -> Result<DateTime<Utc>, chrono::ParseError>;
30}
31
32impl TryIntoTime for DateTime<Utc> {
33    fn into_time(self) -> Result<DateTime<Utc>, chrono::ParseError> {
34        Ok(self)
35    }
36}
37
38impl TryIntoTime for &str {
39    fn into_time(self) -> Result<DateTime<Utc>, chrono::ParseError> {
40        Ok(DateTime::<Utc>::from(DateTime::parse_from_rfc3339(self)?))
41    }
42}
43
44impl TryIntoTime for String {
45    fn into_time(self) -> Result<DateTime<Utc>, chrono::ParseError> {
46        self.as_str().into_time()
47    }
48}
49
50/// The URI-reference type.
51///
52/// The URI reference can be a URI, or just a relative path.
53///
54/// As the [`url::Url`] type can only represent an absolute URL, we are falling back to a string
55/// here.
56///
57/// Also see:
58/// * <https://github.com/cloudevents/spec/blob/v1.0.1/spec.md#type-system>
59/// * <https://tools.ietf.org/html/rfc3986#section-4.1>
60pub type UriReference = String;