zoe_relay/services/
mod.rs

1pub mod blob;
2pub mod messages;
3
4use crate::{Service, ServiceError, ServiceRouter};
5use async_trait::async_trait;
6pub use blob::{BlobService, BlobServiceError};
7pub use messages::{MessagesService, MessagesServiceError};
8use zoe_blob_store::BlobServiceImpl;
9use zoe_message_store::RedisMessageStorage;
10use zoe_wire_protocol::ConnectionInfo;
11use zoe_wire_protocol::StreamPair;
12pub use zoe_wire_protocol::ZoeServices;
13
14#[derive(Debug, thiserror::Error)]
15pub enum AllServiceError {
16    #[error("Blob service error: {0}")]
17    Blob(BlobServiceError),
18
19    #[error("Messages service error: {0}")]
20    Messages(MessagesServiceError),
21}
22
23pub enum Services {
24    Blob(BlobService),
25    Messages(MessagesService),
26}
27
28#[async_trait]
29impl Service for Services {
30    type Error = AllServiceError;
31
32    async fn run(self) -> Result<(), Self::Error> {
33        match self {
34            Services::Blob(service) => service.run().await.map_err(AllServiceError::Blob),
35            Services::Messages(service) => service.run().await.map_err(AllServiceError::Messages),
36        }
37    }
38}
39
40pub struct RelayServiceRouter {
41    blob_service: BlobServiceImpl,
42    message_service: RedisMessageStorage,
43}
44
45impl RelayServiceRouter {
46    pub fn new(blob_service: BlobServiceImpl, message_service: RedisMessageStorage) -> Self {
47        Self {
48            blob_service,
49            message_service,
50        }
51    }
52}
53
54#[async_trait]
55impl ServiceRouter for RelayServiceRouter {
56    type ServiceId = ZoeServices;
57    type Error = ServiceError;
58    type Service = Services;
59
60    async fn parse_service_id(&self, service_id: u8) -> Result<Self::ServiceId, Self::Error> {
61        ZoeServices::try_from(service_id).map_err(|_| ServiceError::InvalidServiceId(service_id))
62    }
63
64    async fn create_service(
65        &self,
66        service_id: &Self::ServiceId,
67        _connection_info: &ConnectionInfo,
68        streams: StreamPair,
69    ) -> Result<Self::Service, Self::Error> {
70        match service_id {
71            ZoeServices::Blob => Ok(Services::Blob(BlobService::new(
72                streams,
73                self.blob_service.clone(),
74            ))),
75            ZoeServices::Messages => Ok(Services::Messages(MessagesService::new(
76                streams,
77                self.message_service.clone(),
78            ))),
79        }
80    }
81}