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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use std::pin::Pin;

use actix::Message;
use futures::{Future, Stream};

use crate::event::RecordedEvent;

use self::error::{EventBusError, EventNotificationError};

pub mod error;

pub trait EventBus: std::fmt::Debug + Default + Send + std::marker::Unpin + 'static {
    fn bus_name() -> &'static str;

    // fn prepare<F: Future<Output = ()> + 'static>(&mut self, resolver: F) -> BoxFuture<'static, ()>;

    fn create_stream(&mut self) -> BoxedStream;
}

pub type BoxedStream = Pin<
    Box<
        dyn Future<
                Output = Pin<Box<dyn Stream<Item = Result<EventBusMessage, EventBusError>> + Send>>,
            > + Send,
    >,
>;

#[derive(Debug, Message)]
#[rtype(result = "()")]
pub enum EventBusMessage {
    Notification(EventNotification),
    Events(String, Vec<RecordedEvent>),
    Unkown,
}

/// Notification produced by the `EventBus` which contains events/streams related informations
#[derive(Clone, Debug, Message)]
#[rtype(result = "()")]
pub struct EventNotification {
    pub stream_id: i32,
    pub stream_uuid: String,
    pub first_stream_version: u32,
    pub last_stream_version: u32,
}

impl<'a> TryFrom<&'a str> for EventNotification {
    type Error = EventNotificationError;

    fn try_from(value: &'a str) -> Result<Self, Self::Error> {
        let mut through = value.splitn(4, ',');

        let stream_uuid = if let Ok(stream_uuid) = through
            .next()
            .ok_or(EventNotificationError::ParsingError {
                field: "stream_uuid",
            })?
            .parse::<String>()
        {
            if stream_uuid.is_empty() {
                return Err(EventNotificationError::InvalidStreamUUID);
            }
            stream_uuid
        } else {
            return Err(EventNotificationError::ParsingError {
                field: "stream_uuid",
            });
        };

        let stream_id = through
            .next()
            .ok_or(EventNotificationError::ParsingError { field: "stream_id" })?
            .parse::<i32>()
            .or(Err(EventNotificationError::ParsingError {
                field: "stream_id",
            }))?;

        let first_stream_version = through
            .next()
            .ok_or(EventNotificationError::ParsingError {
                field: "first_stream_version",
            })?
            .parse::<u32>()
            .or(Err(EventNotificationError::ParsingError {
                field: "first_stream_version",
            }))?;

        let last_stream_version = through
            .next()
            .ok_or(EventNotificationError::ParsingError {
                field: "last_stream_version",
            })?
            .parse::<u32>()
            .or(Err(EventNotificationError::ParsingError {
                field: "last_stream_version",
            }))?;

        Ok(Self {
            stream_uuid,
            stream_id,
            first_stream_version,
            last_stream_version,
        })
    }
}