1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use crate::event_store::client::streams::append_req::options::ExpectedStreamRevision;
use crate::private::Sealed;
use crate::{EventData, ExpectedRevision};
use eventstore_macros::options;

options! {
    #[derive(Clone)]
    /// Options of the append to stream command.
    pub struct AppendToStreamOptions {
        pub(crate) version: ExpectedStreamRevision,
    }
}

impl Default for AppendToStreamOptions {
    fn default() -> Self {
        Self {
            version: ExpectedStreamRevision::Any(()),
            common_operation_options: Default::default(),
        }
    }
}

impl AppendToStreamOptions {
    /// Asks the server to check that the stream receiving the event is at
    /// the given expected version. Default: `ExpectedVersion::Any`.
    pub fn expected_revision(self, version: ExpectedRevision) -> Self {
        let version = match version {
            ExpectedRevision::Any => ExpectedStreamRevision::Any(()),
            ExpectedRevision::StreamExists => ExpectedStreamRevision::StreamExists(()),
            ExpectedRevision::NoStream => ExpectedStreamRevision::NoStream(()),
            ExpectedRevision::Exact(version) => ExpectedStreamRevision::Revision(version),
        };

        Self { version, ..self }
    }
}

pub struct Streaming<I>(pub I);

pub trait ToEvents: Sealed {
    type Events: Iterator<Item = EventData> + Send + 'static;
    fn into_events(self) -> Self::Events;
}

impl ToEvents for EventData {
    type Events = std::option::IntoIter<EventData>;

    fn into_events(self) -> Self::Events {
        Some(self).into_iter()
    }
}

impl ToEvents for Vec<EventData> {
    type Events = std::vec::IntoIter<EventData>;

    fn into_events(self) -> Self::Events {
        self.into_iter()
    }
}

impl<I> ToEvents for Streaming<I>
where
    I: Iterator<Item = EventData> + Send + 'static,
{
    type Events = I;

    fn into_events(self) -> Self::Events {
        self.0
    }
}