Skip to content

Commit 6858566

Browse files
committed
major fixes
Signed-off-by: [email protected] <[email protected]>
1 parent 016f79f commit 6858566

File tree

13 files changed

+110
-26
lines changed

13 files changed

+110
-26
lines changed

src/event/builder.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::Event;
1+
use super::{DisplayError, Event};
22
use snafu::Snafu;
33

44
/// Trait to implement a builder for [`Event`]:
@@ -41,7 +41,8 @@ pub enum Error {
4141
))]
4242
ParseTimeError {
4343
attribute_name: &'static str,
44-
source: chrono::ParseError,
44+
#[snafu(source(from(chrono::ParseError, DisplayError)))]
45+
source: DisplayError<chrono::ParseError>,
4546
},
4647
#[snafu(display(
4748
"Error while setting attribute '{}' with uri/uriref type: {}",
@@ -50,6 +51,7 @@ pub enum Error {
5051
))]
5152
ParseUrlError {
5253
attribute_name: &'static str,
53-
source: url::ParseError,
54+
#[snafu(source(from(url::ParseError, DisplayError)))]
55+
source: DisplayError<url::ParseError>,
5456
},
5557
}

src/event/displayerr.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use core::fmt::{self, Debug, Display};
2+
use snafu::Snafu;
3+
4+
#[derive(Clone)]
5+
pub struct DisplayError<T>(pub T);
6+
7+
impl<T> Debug for DisplayError<T>
8+
where
9+
T: Debug,
10+
{
11+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12+
self.0.fmt(f)
13+
}
14+
}
15+
16+
impl<T> Display for DisplayError<T>
17+
where
18+
T: Display,
19+
{
20+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21+
self.0.fmt(f)
22+
}
23+
}
24+
25+
impl<T> snafu::Error for DisplayError<T> where T: Display + Debug {}

src/event/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
mod attributes;
44
mod builder;
55
mod data;
6+
mod displayerr;
7+
mod event;
68
mod extensions;
79
#[macro_use]
810
mod format;
@@ -15,6 +17,8 @@ pub use attributes::{AttributeValue, AttributesReader, AttributesWriter};
1517
pub use builder::Error as EventBuilderError;
1618
pub use builder::EventBuilder;
1719
pub use data::Data;
20+
pub use displayerr::DisplayError;
21+
pub use event::Event;
1822
pub use extensions::ExtensionValue;
1923
pub(crate) use message::EventBinarySerializer;
2024
pub(crate) use message::EventStructuredSerializer;

src/event/spec_version.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use super::{v03, v10};
2+
use serde::export::Formatter; // double check
3+
use snafu::Snafu;
24
use std::convert::TryFrom;
35
use std::fmt;
46
use std::fmt::Formatter;
@@ -60,7 +62,7 @@ impl fmt::Display for UnknownSpecVersion {
6062
}
6163
}
6264

63-
impl std::error::Error for UnknownSpecVersion {}
65+
impl snafu::Error for UnknownSpecVersion {}
6466

6567
impl TryFrom<&str> for SpecVersion {
6668
type Error = UnknownSpecVersion;

src/event/types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use chrono::{DateTime, Utc};
2+
use snafu;
23
use std::prelude::v1::*;
34
use url::Url;
45

src/event/v03/attributes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl Default for Attributes {
162162
datacontenttype: None,
163163
schemaurl: None,
164164
subject: None,
165-
time: Some(Utc::now()),
165+
time: Some(chrono::offset::Utc::now()),
166166
}
167167
}
168168
}

src/event/v03/builder.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::Attributes as AttributesV03;
22
use crate::event::{
3-
Attributes, Data, Event, EventBuilderError, ExtensionValue, TryIntoTime, TryIntoUrl,
3+
Attributes, Data, DisplayError, Event, EventBuilderError, ExtensionValue, TryIntoTime,
4+
TryIntoUrl,
45
};
56
use crate::message::MessageAttributeValue;
67
use chrono::{DateTime, Utc};
@@ -36,7 +37,7 @@ impl EventBuilder {
3637
Err(e) => {
3738
self.error = Some(EventBuilderError::ParseUrlError {
3839
attribute_name: "source",
39-
source: e,
40+
source: DisplayError(e),
4041
})
4142
}
4243
};
@@ -59,7 +60,7 @@ impl EventBuilder {
5960
Err(e) => {
6061
self.error = Some(EventBuilderError::ParseTimeError {
6162
attribute_name: "time",
62-
source: e,
63+
source: DisplayError(e),
6364
})
6465
}
6566
};
@@ -99,7 +100,7 @@ impl EventBuilder {
99100
Err(e) => {
100101
self.error = Some(EventBuilderError::ParseUrlError {
101102
attribute_name: "schemaurl",
102-
source: e,
103+
source: DisplayError(e),
103104
})
104105
}
105106
};

src/event/v10/builder.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::Attributes as AttributesV10;
22
use crate::event::{
3-
Attributes, Data, Event, EventBuilderError, ExtensionValue, TryIntoTime, TryIntoUrl,
3+
Attributes, Data, DisplayError, Event, EventBuilderError, ExtensionValue, TryIntoTime,
4+
TryIntoUrl,
45
};
56
use crate::message::MessageAttributeValue;
67
use chrono::{DateTime, Utc};
@@ -36,7 +37,7 @@ impl EventBuilder {
3637
Err(e) => {
3738
self.error = Some(EventBuilderError::ParseUrlError {
3839
attribute_name: "source",
39-
source: e,
40+
source: DisplayError(e),
4041
})
4142
}
4243
};
@@ -59,7 +60,7 @@ impl EventBuilder {
5960
Err(e) => {
6061
self.error = Some(EventBuilderError::ParseTimeError {
6162
attribute_name: "time",
62-
source: e,
63+
source: DisplayError(e),
6364
})
6465
}
6566
};
@@ -99,7 +100,7 @@ impl EventBuilder {
99100
Err(e) => {
100101
self.error = Some(EventBuilderError::ParseUrlError {
101102
attribute_name: "dataschema",
102-
source: e,
103+
source: DisplayError(e),
103104
})
104105
}
105106
};

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#![deny(broken_intra_doc_links)]
4242
#![no_std]
4343

44+
extern crate core_error;
4445
extern crate no_std_compat as std;
4546

4647
extern crate serde;

src/message/deserializer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{BinarySerializer, Encoding, Error, Result, StructuredSerializer};
1+
use super::{BinarySerializer, Encoding, Result, StructuredSerializer};
22
use crate::event::{EventBinarySerializer, EventStructuredSerializer};
33
use crate::Event;
44

@@ -46,7 +46,7 @@ where
4646
match self.encoding() {
4747
Encoding::BINARY => BinaryDeserializer::into_event(self),
4848
Encoding::STRUCTURED => StructuredDeserializer::into_event(self),
49-
_ => Err(Error::WrongEncoding {}),
49+
_ => Err(super::error::Error::WrongEncoding {}),
5050
}
5151
}
5252

0 commit comments

Comments
 (0)