1
#![doc(
2
    html_logo_url = "https://raw.githubusercontent.com/sevki/jetstream/main/logo/JetStream.png"
3
)]
4
#![doc(
5
    html_favicon_url = "https://raw.githubusercontent.com/sevki/jetstream/main/logo/JetStream.png"
6
)]
7
//! # JetStream Rpc
8
//! Defines Rpc primitives for JetStream.
9
//! Of note is the `Protocol` trait which is meant to be used with the `service` attribute macro.
10
#![cfg_attr(docsrs, feature(doc_cfg))]
11

            
12
use std::io::{self};
13
pub mod client;
14
pub mod context;
15
pub mod framer;
16
pub mod server;
17
use jetstream_wireformat::WireFormat;
18
// Re-export codecs
19
extern crate tokio_util;
20
pub use tokio_util::codec::{Decoder, Encoder, Framed};
21

            
22
pub use framer::*;
23

            
24
/// A trait representing a message that can be encoded and decoded.
25
#[cfg(not(target_arch = "wasm32"))]
26
pub trait Message: WireFormat + Sync {}
27

            
28
/// A trait representing a message that can be encoded and decoded.
29
/// WebAssembly doesn't fully support Send+Sync, so we don't require those.
30
#[cfg(target_arch = "wasm32")]
31
pub trait Message: WireFormat {}
32

            
33
#[repr(transparent)]
34
pub struct Tag(u16);
35

            
36
impl From<u16> for Tag {
37
    fn from(tag: u16) -> Self {
38
        Self(tag)
39
    }
40
}
41

            
42
/// Defines the request and response types for the JetStream protocol.
43
#[trait_variant::make(Send + Sync + Sized)]
44
pub trait Protocol: Send + Sync {
45
    type Request: Framer;
46
    type Response: Framer;
47
    type Error: std::error::Error + Send + Sync + 'static;
48
    const VERSION: &'static str;
49
    async fn rpc(
50
        &mut self,
51
        context: context::Context,
52
        frame: Frame<Self::Request>,
53
    ) -> Result<Frame<Self::Response>, Self::Error>;
54
}
55

            
56
#[derive(Debug, thiserror::Error)]
57
pub enum Error {
58
    #[error("io error: {0}")]
59
    Io(#[from] io::Error),
60
    #[error("generic error: {0}")]
61
    Generic(#[from] Box<dyn std::error::Error + Send + Sync>),
62
    #[error("{0}")]
63
    Custom(String),
64
    #[error("invalid response")]
65
    InvalidResponse,
66
}