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
extern crate tokio_util;
13
mod any_server;
14
mod call;
15
pub mod client;
16
mod constants;
17
pub mod context;
18
mod error;
19
pub mod framer;
20
mod mux;
21
mod router;
22
pub mod server;
23
mod tag;
24
mod version;
25
pub use any_server::AnyServer;
26
pub use call::*;
27
pub use constants::*;
28
pub use error::*;
29
pub use jetstream_error::IntoError;
30
use jetstream_wireformat::WireFormat;
31
pub use mux::*;
32
pub use router::*;
33
use std::str::FromStr;
34
pub use tag::*;
35
pub use tokio_util::codec::{Decoder, Encoder, Framed};
36
pub use version::*;
37

            
38
pub enum Encoding {
39
    JetStream,
40
    Json,
41
    Xml,
42
}
43

            
44
#[derive(Debug, thiserror::Error)]
45
pub enum EncodingError {
46
    #[error("Invalid encoding")]
47
    InvalidEncoding,
48
}
49

            
50
impl FromStr for Encoding {
51
    type Err = EncodingError;
52

            
53
    fn from_str(s: &str) -> Result<Self, Self::Err> {
54
        match s {
55
            MIMETYPE_JSON => Ok(Encoding::Json),
56
            MIMETYPE_XML => Ok(Encoding::Xml),
57
            MIMETYPE_JETSTREAM => Ok(Encoding::JetStream),
58
            _ => Err(EncodingError::InvalidEncoding),
59
        }
60
    }
61
}
62

            
63
pub use constants::HEADER_KEY_JETSTREAM_PROTO;
64
pub use framer::*;
65

            
66
/// A trait representing a message that can be encoded and decoded.
67
#[cfg(native)]
68
pub trait Message: WireFormat + Sync {}
69

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

            
75
/// Defines the request and response types for the JetStream protocol.
76
#[trait_variant::make(Send + Sync + Sized)]
77
pub trait Protocol: Send + Sync {
78
    type Request: Framer;
79
    type Response: Framer;
80
    // r[impl jetstream.error.v2.into-error]
81
    type Error: IntoError;
82
    const VERSION: &'static str;
83
    const NAME: &'static str;
84
}
85

            
86
// const _: () = {
87
//     let _: HashMap<String, Box<dyn Protocol>> = HashMap::new();
88

            
89
//     ()
90
// };