Skip to content

Commit f3bd46e

Browse files
committed
feat: Add seperate EventMissingStrategy for observing events
1 parent 973277e commit f3bd46e

File tree

8 files changed

+74
-54
lines changed

8 files changed

+74
-54
lines changed

README.md

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ if let Err(err) = result {
3737
}
3838
```
3939

40-
*Note that `Ping` does not require authentication, so the call may succeed even if the API token is invalid.*
40+
*Note that `ping` does not require authentication, so the call may succeed even if the API token is invalid.*
4141

4242
If you want to verify the API token, call `verify_api_token`. If the token is invalid, the function will return an error:
4343

@@ -54,7 +54,7 @@ Call the `write_events` function and hand over a vector with one or more events.
5454

5555
Specify `source`, `subject`, `type` (using `ty`), and `data` according to the [CloudEvents](https://docs.eventsourcingdb.io/fundamentals/cloud-events/) format.
5656

57-
For `data` provide a json object using a `serde_json:Value`.
57+
For `data` provide a JSON object using a `serde_json:Value`.
5858

5959
The function returns the written events, including the fields added by the server:
6060

@@ -79,7 +79,7 @@ match result {
7979

8080
#### Using the `IsSubjectPristine` precondition
8181

82-
If you only want to write events in case a subject (such as `/books/42`) does not yet have any events, use the `IsSubjectPristine` Precondition to create a precondition and pass it in a vector as the second argument:
82+
If you only want to write events in case a subject (such as `/books/42`) does not yet have any events, use the `IsSubjectPristine` precondition to create a precondition and pass it in a vector as the second argument:
8383

8484
```rust
8585
let result = client.write_events(
@@ -96,12 +96,12 @@ match result {
9696

9797
#### Using the `IsSubjectOnEventId` precondition
9898

99-
If you only want to write events in case the last event of a subject (such as `/books/42`) has a specific ID (e.g., `0`), use the `IsSubjectOnEventID` Precondition to create a precondition and pass it in a vector as the second argument:
99+
If you only want to write events in case the last event of a subject (such as `/books/42`) has a specific ID (e.g., `0`), use the `IsSubjectOnEventId` precondition to create a precondition and pass it in a vector as the second argument:
100100

101101
```rust
102102
let result = client.write_events(
103103
vec![event.clone()],
104-
vec![Precondition::IsSubjectPristine {
104+
vec![Precondition::IsSubjectOnEventId {
105105
subject: "/books/42".to_string(),
106106
event_id: "0".to_string(),
107107
}],
@@ -123,7 +123,7 @@ The function returns a stream from which you can retrieve one event at a time:
123123
```rust
124124
let result = client
125125
.read_events("/books/42", Some(
126-
ReadEventsRequestOptions {
126+
ReadEventsOptions {
127127
recursive: false,
128128
from_latest_event: None,
129129
order: None,
@@ -150,7 +150,7 @@ If you want to read not only all the events of a subject, but also the events of
150150
```rust
151151
let result = client
152152
.read_events("/books/42", Some(
153-
ReadEventsRequestOptions {
153+
ReadEventsOptions {
154154
recursive: true,
155155
..Default::default(),
156156
}
@@ -167,7 +167,7 @@ By default, events are read in chronological order. To read in anti-chronologica
167167
```rust
168168
let result = client
169169
.read_events("/books/42", Some(
170-
ReadEventsRequestOptions {
170+
ReadEventsOptions {
171171
recursive: false,
172172
order: Some(Ordering::Antichronological)
173173
..Default::default(),
@@ -187,7 +187,7 @@ Specify the ID and whether to include or exclude it, for both the lower and uppe
187187
```rust
188188
let result = client
189189
.read_events("/books/42", Some(
190-
ReadEventsRequestOptions {
190+
ReadEventsOptions {
191191
recursive: false,
192192
lower_bound: Some(Bound {
193193
bound_type: BoundType::Inclusive,
@@ -212,13 +212,13 @@ Possible options are `ReadNothing`, which skips reading entirely, or `ReadyEvery
212212
```rust
213213
let result = client
214214
.read_events("/books/42", Some(
215-
ReadEventsRequestOptions {
215+
ReadEventsOptions {
216216
recursive: false,
217217
from_latest_event: Some(
218218
FromLatestEventOptions {
219219
subject: "/books/42",
220220
ty: "io.eventsourcingdb.library.book-borrowed",
221-
if_event_is_missing: EventMissingStrategy::ReadEverything,
221+
if_event_is_missing: ReadEventMissingStrategy::ReadEverything,
222222
}
223223
)
224224
..Default::default(),
@@ -256,11 +256,10 @@ To observe all events of a subject, call the `observe_events` function with the
256256

257257
The function returns a stream from which you can retrieve one event at a time:
258258

259-
260259
```rust
261260
let result = client
262261
.observe_events("/books/42", Some(
263-
ObserveEventsRequestOptions {
262+
ObserveEventsOptions {
264263
recursive: false,
265264
from_latest_event: None,
266265
lower_bound: None,
@@ -285,7 +284,7 @@ If you want to observe not only all the events of a subject, but also the events
285284
```rust
286285
let result = client
287286
.observe_events("/books/42", Some(
288-
ObserveEventsRequestOptions {
287+
ObserveEventsOptions {
289288
recursive: true,
290289
..Default::default(),
291290
}
@@ -303,8 +302,8 @@ Specify the ID and whether to include or exclude it:
303302

304303
```rust
305304
let result = client
306-
.read_events("/books/42", Some(
307-
ReadEventsRequestOptions {
305+
.observe_events("/books/42", Some(
306+
ObserveEventsOptions {
308307
recursive: false,
309308
lower_bound: Some(Bound {
310309
bound_type: BoundType::Inclusive,
@@ -324,14 +323,14 @@ Possible options are `WaitForEvent`, which waits for an event of the given type
324323

325324
```rust
326325
let result = client
327-
.read_events("/books/42", Some(
328-
ReadEventsRequestOptions {
326+
.observe_events("/books/42", Some(
327+
ObserveEventsOptions {
329328
recursive: false,
330329
from_latest_event: Some(
331330
ObserveFromLatestEventOptions {
332331
subject: "/books/42",
333332
ty: "io.eventsourcingdb.library.book-borrowed",
334-
if_event_is_missing: EventMissingStrategy::ObserveEverything,
333+
if_event_is_missing: ObserveEventMissingStrategy::ObserveEverything,
335334
}
336335
)
337336
..Default::default(),

examples/observing_events.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use eventsourcingdb::{
2-
client::{Client, request_options::ObserveEventsRequestOptions},
2+
client::{Client, request_options::ObserveEventsOptions},
33
container::Container,
44
};
55
use futures::StreamExt;
@@ -14,7 +14,7 @@ async fn main() {
1414
let result = client
1515
.observe_events(
1616
"/books/42",
17-
Some(ObserveEventsRequestOptions {
17+
Some(ObserveEventsOptions {
1818
recursive: false,
1919
from_latest_event: None,
2020
lower_bound: None,

examples/reading_events.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use eventsourcingdb::{
2-
client::{Client, request_options::ReadEventsRequestOptions},
2+
client::{Client, request_options::ReadEventsOptions},
33
container::Container,
44
};
55
use futures::StreamExt;
@@ -14,7 +14,7 @@ async fn main() {
1414
let result = client
1515
.read_events(
1616
"/books/42",
17-
Some(ReadEventsRequestOptions {
17+
Some(ReadEventsOptions {
1818
recursive: false,
1919
from_latest_event: None,
2020
order: None,

src/client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ impl Client {
203203
pub async fn read_events<'a>(
204204
&self,
205205
subject: &'a str,
206-
options: Option<request_options::ReadEventsRequestOptions<'a>>,
206+
options: Option<request_options::ReadEventsOptions<'a>>,
207207
) -> Result<impl Stream<Item = Result<Event, ClientError>>, ClientError> {
208208
let response = self
209209
.request_streaming(ReadEventsRequest { subject, options })
@@ -249,7 +249,7 @@ impl Client {
249249
pub async fn observe_events<'a>(
250250
&self,
251251
subject: &'a str,
252-
options: Option<request_options::ObserveEventsRequestOptions<'a>>,
252+
options: Option<request_options::ObserveEventsOptions<'a>>,
253253
) -> Result<impl Stream<Item = Result<Event, ClientError>>, ClientError> {
254254
let response = self
255255
.request_streaming(ObserveEventsRequest { subject, options })

src/client/client_request/observe_events.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
use reqwest::Method;
22
use serde::Serialize;
33

4-
use crate::{
5-
client::request_options::ObserveEventsRequestOptions, error::ClientError, event::Event,
6-
};
4+
use crate::{client::request_options::ObserveEventsOptions, error::ClientError, event::Event};
75

86
use super::{ClientRequest, StreamingRequest};
97

108
#[derive(Debug, Clone, Serialize)]
119
pub struct ObserveEventsRequest<'a> {
1210
pub subject: &'a str,
1311
#[serde(skip_serializing_if = "Option::is_none")]
14-
pub options: Option<ObserveEventsRequestOptions<'a>>,
12+
pub options: Option<ObserveEventsOptions<'a>>,
1513
}
1614

1715
impl ClientRequest for ObserveEventsRequest<'_> {

src/client/client_request/read_events.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
use reqwest::Method;
22
use serde::Serialize;
33

4-
use crate::{client::request_options::ReadEventsRequestOptions, error::ClientError, event::Event};
4+
use crate::{client::request_options::ReadEventsOptions, error::ClientError, event::Event};
55

66
use super::{ClientRequest, StreamingRequest};
77

88
#[derive(Debug, Clone, Serialize)]
99
pub struct ReadEventsRequest<'a> {
1010
pub subject: &'a str,
1111
#[serde(skip_serializing_if = "Option::is_none")]
12-
pub options: Option<ReadEventsRequestOptions<'a>>,
12+
pub options: Option<ReadEventsOptions<'a>>,
1313
}
1414

1515
impl ClientRequest for ReadEventsRequest<'_> {

src/client/request_options.rs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use serde::Serialize;
55
/// Options for reading events from the database
66
#[derive(Debug, Default, Clone, Serialize)]
77
#[serde(rename_all = "camelCase")]
8-
pub struct ReadEventsRequestOptions<'a> {
8+
pub struct ReadEventsOptions<'a> {
99
/// Start reading events from this start event
1010
#[serde(skip_serializing_if = "Option::is_none")]
11-
pub from_latest_event: Option<FromLatestEventOptions<'a>>,
11+
pub from_latest_event: Option<ReadFromLatestEventOptions<'a>>,
1212
/// Lower bound of events to read
1313
#[serde(skip_serializing_if = "Option::is_none")]
1414
pub lower_bound: Option<Bound<'a>>,
@@ -25,10 +25,10 @@ pub struct ReadEventsRequestOptions<'a> {
2525
/// Options for observing events from the database
2626
#[derive(Debug, Default, Clone, Serialize)]
2727
#[serde(rename_all = "camelCase")]
28-
pub struct ObserveEventsRequestOptions<'a> {
28+
pub struct ObserveEventsOptions<'a> {
2929
/// Start reading events from this start event
3030
#[serde(skip_serializing_if = "Option::is_none")]
31-
pub from_latest_event: Option<FromLatestEventOptions<'a>>,
31+
pub from_latest_event: Option<ObserveFromLatestEventOptions<'a>>,
3232
/// Lower bound of events to read
3333
#[serde(skip_serializing_if = "Option::is_none")]
3434
pub lower_bound: Option<Bound<'a>>,
@@ -67,25 +67,48 @@ pub struct Bound<'a> {
6767
pub id: &'a str,
6868
}
6969

70-
/// The strategy for handling missing events
70+
/// The strategy for handling missing events while reading
7171
#[derive(Debug, Clone, Serialize)]
7272
#[serde(rename_all = "kebab-case")]
73-
pub enum EventMissingStrategy {
73+
pub enum ReadEventMissingStrategy {
7474
/// Read all events if the required one is missing
7575
ReadEverything,
7676
/// Read no events if the required one is missing
7777
ReadNothing,
7878
}
7979

80-
/// Options for reading events from the start reading at
80+
/// The strategy for handling missing events while observing
81+
#[derive(Debug, Clone, Serialize)]
82+
#[serde(rename_all = "kebab-case")]
83+
pub enum ObserveEventMissingStrategy {
84+
/// Observe all events if the required one is missing
85+
ObserveEverything,
86+
/// Wait for the event until observing
87+
WaitForEvent,
88+
}
89+
90+
/// Options for reading events from the latest event of certain type or subject
8191
#[derive(Debug, Clone, Serialize)]
8292
#[serde(rename_all = "camelCase")]
83-
pub struct FromLatestEventOptions<'a> {
93+
pub struct ReadFromLatestEventOptions<'a> {
8494
/// The strategy for handling missing events
85-
pub if_event_is_missing: EventMissingStrategy,
95+
pub if_event_is_missing: ReadEventMissingStrategy,
8696
/// The subject the event should be on
8797
pub subject: &'a str,
8898
/// The type of the event to read from
8999
#[serde(rename = "type")]
90100
pub ty: &'a str,
91101
}
102+
103+
/// Options for observe events from the latest event of certain type or subject
104+
#[derive(Debug, Clone, Serialize)]
105+
#[serde(rename_all = "camelCase")]
106+
pub struct ObserveFromLatestEventOptions<'a> {
107+
/// The strategy for handling missing events
108+
pub if_event_is_missing: ObserveEventMissingStrategy,
109+
/// The subject the event should be on
110+
pub subject: &'a str,
111+
/// The type of the event to observe from
112+
#[serde(rename = "type")]
113+
pub ty: &'a str,
114+
}

0 commit comments

Comments
 (0)