Skip to content

Commit 2b3253b

Browse files
committed
chore(client): restructure event module to avoid allowing clippy warnings
1 parent ce372f7 commit 2b3253b

File tree

6 files changed

+86
-79
lines changed

6 files changed

+86
-79
lines changed

src/event.rs

Lines changed: 8 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,14 @@
11
//! This module holds all event types that are send between the client and the database.
22
3-
// Allow module inception here, since "event" is the expected as the name for both modules.
4-
// Renaming would be possible, but would probably lead to more confusion.
5-
#[allow(clippy::module_inception)]
6-
mod event;
7-
mod event_candidate;
8-
mod management_event;
3+
mod event_types;
4+
mod trace_info;
95

10-
pub use event::Event;
11-
pub use event_candidate::EventCandidate;
12-
pub use management_event::ManagementEvent;
13-
use serde::{Deserialize, Serialize};
6+
// Reexport relevant types to flatten the module graph for consumers and
7+
// keep private encapsulation of implementation details.
8+
pub use event_types::event::Event;
9+
pub use event_types::event_candidate::EventCandidate;
10+
pub use event_types::management_event::ManagementEvent;
11+
pub use trace_info::TraceInfo;
1412

1513
#[cfg(feature="cloudevents")]
1614
use crate::error::EventError;
17-
18-
/// Represents the trace information of an event.
19-
/// This is used for distributed tracing.
20-
/// It can either be a traceparent or a traceparent and tracestate.
21-
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
22-
#[serde(untagged)]
23-
pub enum TraceInfo {
24-
// LEAVE ORDER AS IS
25-
// This is important for deserialization as the traceparent is always present
26-
27-
/// The traceparent and tracestate of the event.
28-
/// This is used for distributed tracing.
29-
WithState {
30-
/// The traceparent of the event.
31-
/// This is used for distributed tracing.
32-
traceparent: String,
33-
/// The tracestate of the event.
34-
/// This is used for distributed tracing.
35-
tracestate: String,
36-
},
37-
/// The traceparent of the event.
38-
/// This is used for distributed tracing.
39-
Traceparent {
40-
/// The traceparent of the event.
41-
/// This is used for distributed tracing.
42-
traceparent: String,
43-
},
44-
}
45-
46-
impl TraceInfo {
47-
/// Get the traceparent of the event.
48-
#[must_use]
49-
pub fn traceparent(&self) -> &str {
50-
match self {
51-
Self::Traceparent { traceparent } | Self::WithState { traceparent, .. } => traceparent,
52-
}
53-
}
54-
/// Get the tracestate of the event.
55-
#[must_use]
56-
pub fn tracestate(&self) -> Option<&str> {
57-
match self {
58-
Self::Traceparent { .. } => None,
59-
Self::WithState { tracestate, .. } => Some(tracestate),
60-
}
61-
}
62-
63-
/// Create a new `TraceInfo` from a cloudevent.
64-
/// This will return None if the cloudevent does not contain a traceparent or tracestate.
65-
///
66-
/// # Errors
67-
/// If the cloudevent contains a tracestate but no traceparent, an error will be returned.
68-
#[cfg(feature="cloudevents")]
69-
pub fn from_cloudevent(event: &cloudevents::Event) -> Result<Option<Self>, EventError> {
70-
let traceparent = event.extension("traceparent").map(ToString::to_string);
71-
let tracestate = event.extension("tracestate").map(ToString::to_string);
72-
73-
match (traceparent, tracestate) {
74-
(Some(traceparent), Some(tracestate)) => Ok(Some(Self::WithState {
75-
traceparent,
76-
tracestate,
77-
})),
78-
(Some(traceparent), None) => Ok(Some(Self::Traceparent { traceparent })),
79-
(None, None) => Ok(None),
80-
(None, Some(_)) => Err(EventError::InvalidCloudevent),
81-
}
82-
}
83-
}

src/event/event_types.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//! This module holds all possible event types this sdk works with.
2+
3+
pub mod event;
4+
pub mod event_candidate;
5+
pub mod management_event;

src/event/event.rs renamed to src/event/event_types/event.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use chrono::{DateTime, Utc};
22
use serde::{Deserialize, Serialize};
33
use serde_json::Value;
44

5-
use super::{EventCandidate, TraceInfo};
5+
use crate::event::{trace_info::TraceInfo, EventCandidate};
6+
67

78
/// Represents an event that has been received from the DB.
89
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]

src/event/event_candidate.rs renamed to src/event/event_types/event_candidate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use serde::{Deserialize, Serialize};
22
use serde_json::Value;
33
use typed_builder::TypedBuilder;
4+
use crate::event::trace_info::TraceInfo;
45

5-
use super::TraceInfo;
66
#[cfg(feature = "cloudevents")]
77
use crate::error::EventError;
88

File renamed without changes.

src/event/trace_info.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//! This module holds supporting traits for the "Tracing" feature of eventsourcingdb.
2+
3+
use serde::{Deserialize, Serialize};
4+
5+
/// Represents the trace information of an event.
6+
/// This is used for distributed tracing.
7+
/// It can either be a traceparent or a traceparent and tracestate.
8+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
9+
#[serde(untagged)]
10+
pub enum TraceInfo {
11+
// LEAVE ORDER AS IS
12+
// This is important for deserialization as the traceparent is always present
13+
14+
/// The traceparent and tracestate of the event.
15+
/// This is used for distributed tracing.
16+
WithState {
17+
/// The traceparent of the event.
18+
/// This is used for distributed tracing.
19+
traceparent: String,
20+
/// The tracestate of the event.
21+
/// This is used for distributed tracing.
22+
tracestate: String,
23+
},
24+
/// The traceparent of the event.
25+
/// This is used for distributed tracing.
26+
Traceparent {
27+
/// The traceparent of the event.
28+
/// This is used for distributed tracing.
29+
traceparent: String,
30+
},
31+
}
32+
33+
impl TraceInfo {
34+
/// Get the traceparent of the event.
35+
#[must_use]
36+
pub fn traceparent(&self) -> &str {
37+
match self {
38+
Self::Traceparent { traceparent } | Self::WithState { traceparent, .. } => traceparent,
39+
}
40+
}
41+
/// Get the tracestate of the event.
42+
#[must_use]
43+
pub fn tracestate(&self) -> Option<&str> {
44+
match self {
45+
Self::Traceparent { .. } => None,
46+
Self::WithState { tracestate, .. } => Some(tracestate),
47+
}
48+
}
49+
50+
/// Create a new `TraceInfo` from a cloudevent.
51+
/// This will return None if the cloudevent does not contain a traceparent or tracestate.
52+
///
53+
/// # Errors
54+
/// If the cloudevent contains a tracestate but no traceparent, an error will be returned.
55+
#[cfg(feature="cloudevents")]
56+
pub fn from_cloudevent(event: &cloudevents::Event) -> Result<Option<Self>, EventError> {
57+
let traceparent = event.extension("traceparent").map(ToString::to_string);
58+
let tracestate = event.extension("tracestate").map(ToString::to_string);
59+
60+
match (traceparent, tracestate) {
61+
(Some(traceparent), Some(tracestate)) => Ok(Some(Self::WithState {
62+
traceparent,
63+
tracestate,
64+
})),
65+
(Some(traceparent), None) => Ok(Some(Self::Traceparent { traceparent })),
66+
(None, None) => Ok(None),
67+
(None, Some(_)) => Err(EventError::InvalidCloudevent),
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)