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;
pub const ERROR_KEY: Key = Key::for_path::<WireError>(ERROR_PATH);
pub const ERROR_PATH: &str = "error";
#[derive(Serialize, Deserialize, Schema, Debug, PartialEq)]
pub struct FrameTooLong {
pub len: u32,
pub max: u32,
}
#[derive(Serialize, Deserialize, Schema, Debug, PartialEq)]
pub struct FrameTooShort {
pub len: u32,
}
#[derive(Serialize, Deserialize, Schema, Debug, PartialEq)]
pub enum WireError {
FrameTooLong(FrameTooLong),
FrameTooShort(FrameTooShort),
DeserFailed,
SerFailed,
UnknownKey,
FailedToSpawn,
KeyTooSmall,
}
impl core::fmt::Display for WireError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
WireError::FrameTooLong(e) => write!(f, "The frame exceeded the buffering capabilities of the server: {} > {}", e.len, e.max),
WireError::FrameTooShort(e) => write!(f, "The frame was shorter than the minimum frame size and was rejected: {}", e.len),
WireError::DeserFailed => f.write_str("Deserialization of a message failed"),
WireError::SerFailed => f.write_str("Serialization of a message failed, usually due to a lack of space to buffer the serialized form"),
WireError::UnknownKey => f.write_str("The key associated with this request was unknown"),
WireError::FailedToSpawn => f.write_str("The server was unable to spawn the associated handler, typically due to an exhaustion of resources"),
WireError::KeyTooSmall => f.write_str("The provided key is below the minimum key size calculated to avoid hash collisions, and was rejected to avoid potential misunderstanding"),
}
}
}
impl core::error::Error for WireError {}
#[cfg(not(feature = "use-std"))]
#[derive(Serialize, Schema, Debug, PartialEq, Copy, Clone)]
pub enum SchemaData<'a> {
Type(&'a NamedType),
Endpoint {
path: &'a str,
request_key: Key,
response_key: Key,
},
Topic {
path: &'a str,
key: Key,
direction: TopicDirection,
},
}
#[cfg(feature = "use-std")]
#[derive(Serialize, Deserialize, Schema, Debug, PartialEq, Clone)]
pub enum OwnedSchemaData {
Type(OwnedNamedType),
Endpoint {
path: String,
request_key: Key,
response_key: Key,
},
Topic {
path: String,
key: Key,
direction: TopicDirection,
},
}
#[derive(Serialize, Deserialize, Schema, Debug, PartialEq, Copy, Clone)]
pub struct SchemaTotals {
pub types_sent: u32,
pub endpoints_sent: u32,
pub topics_in_sent: u32,
pub topics_out_sent: u32,
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 |
| ------- | --------- | ---- | --- |
}