postcard_rpc/
standard_icd.rs

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//! These are items you can use for your error path and error key.
//!
//! This is used by [`define_dispatch!()`][crate::define_dispatch] as well.

use crate::{endpoints, topics, Key, TopicDirection};
use postcard_schema::Schema;
use serde::{Deserialize, Serialize};

#[cfg(not(feature = "use-std"))]
use postcard_schema::schema::NamedType;

#[cfg(feature = "use-std")]
use postcard_schema::schema::owned::OwnedNamedType;

/// The calculated Key for the type [`WireError`] and the path [`ERROR_PATH`]
pub const ERROR_KEY: Key = Key::for_path::<WireError>(ERROR_PATH);

/// The path string used for the error type
pub const ERROR_PATH: &str = "error";

/// The given frame was too long
#[derive(Serialize, Deserialize, Schema, Debug, PartialEq)]
pub struct FrameTooLong {
    /// The length of the too-long frame
    pub len: u32,
    /// The maximum frame length supported
    pub max: u32,
}

/// The given frame was too short
#[derive(Serialize, Deserialize, Schema, Debug, PartialEq)]
pub struct FrameTooShort {
    /// The length of the too-short frame
    pub len: u32,
}

/// A protocol error that is handled outside of the normal request type, usually
/// indicating a protocol-level error
#[derive(Serialize, Deserialize, Schema, Debug, PartialEq)]
pub enum WireError {
    /// The frame exceeded the buffering capabilities of the server
    FrameTooLong(FrameTooLong),
    /// The frame was shorter than the minimum frame size and was rejected
    FrameTooShort(FrameTooShort),
    /// Deserialization of a message failed
    DeserFailed,
    /// Serialization of a message failed, usually due to a lack of space to
    /// buffer the serialized form
    SerFailed,
    /// The key associated with this request was unknown
    UnknownKey,
    /// The server was unable to spawn the associated handler, typically due
    /// to an exhaustion of resources
    FailedToSpawn,
    /// The provided key is below the minimum key size calculated to avoid hash
    /// collisions, and was rejected to avoid potential misunderstanding
    KeyTooSmall,
}

/// A single element of schema information
#[cfg(not(feature = "use-std"))]
#[derive(Serialize, Schema, Debug, PartialEq, Copy, Clone)]
pub enum SchemaData<'a> {
    /// A single Type
    Type(&'a NamedType),
    /// A single Endpoint
    Endpoint {
        /// The path of the endpoint
        path: &'a str,
        /// The key of the Request type + path
        request_key: Key,
        /// The key of the Response type + path
        response_key: Key,
    },
    /// A single Topic
    Topic {
        /// The path of the topic
        path: &'a str,
        /// The key of the Message type + path
        key: Key,
        /// The direction of the Topic
        direction: TopicDirection,
    },
}

/// A single element of schema information
#[cfg(feature = "use-std")]
#[derive(Serialize, Deserialize, Schema, Debug, PartialEq, Clone)]
pub enum OwnedSchemaData {
    /// A single Type
    Type(OwnedNamedType),
    /// A single Endpoint
    Endpoint {
        /// The path of the endpoint
        path: String,
        /// The key of the Request type + path
        request_key: Key,
        /// The key of the Response type + path
        response_key: Key,
    },
    /// A single Topic
    Topic {
        /// The path of the topic
        path: String,
        /// The key of the Message type + path
        key: Key,
        /// The direction of the Topic
        direction: TopicDirection,
    },
}

/// A summary of all messages sent when streaming schema data
#[derive(Serialize, Deserialize, Schema, Debug, PartialEq, Copy, Clone)]
pub struct SchemaTotals {
    /// A count of the number of (Owned)SchemaData::Type messages sent
    pub types_sent: u32,
    /// A count of the number of (Owned)SchemaData::Endpoint messages sent
    pub endpoints_sent: u32,
    /// A count of the number of (Owned)SchemaData::Topic messages sent
    pub topics_in_sent: u32,
    /// A count of the number of (Owned)SchemaData::Topic messages sent
    pub topics_out_sent: u32,
    /// A count of the number of messages (any of the above) that failed to send
    pub errors: u32,
}

endpoints! {
    list = STANDARD_ICD_ENDPOINTS;
    omit_std = true;
    | EndpointTy            | RequestTy     | ResponseTy    | Path                       |
    | ----------            | ---------     | ----------    | ----                       |
    | PingEndpoint          | u32           | u32           | "postcard-rpc/ping"        |
    | GetAllSchemasEndpoint | ()            | SchemaTotals  | "postcard-rpc/schemas/get" |
}

topics! {
    list = STANDARD_ICD_TOPICS_OUT;
    direction = crate::TopicDirection::ToClient;
    omit_std = true;
    | TopicTy               | MessageTy         | Path                          | Cfg                           |
    | -------               | ---------         | ----                          | ---                           |
    | GetAllSchemaDataTopic | SchemaData<'a>    | "postcard-rpc/schema/data"    | cfg(not(feature = "use-std")) |
    | GetAllSchemaDataTopic | OwnedSchemaData   | "postcard-rpc/schema/data"    | cfg(feature = "use-std")      |
    | LoggingTopic          | str               | "postcard-rpc/logging"        | cfg(not(feature = "use-std")) |
    | LoggingTopic          | String            | "postcard-rpc/logging"        | cfg(feature = "use-std")      |
}

topics! {
    list = STANDARD_ICD_TOPICS_IN;
    direction = crate::TopicDirection::ToServer;
    omit_std = true;
    | TopicTy           | MessageTy         | Path                          | Cfg                           |
    | -------           | ---------         | ----                          | ---                           |
}