1use crate::error::ApiError;
17use serde::{Deserialize, Serialize, de::DeserializeOwned};
18
19#[derive(Debug, Clone, Serialize, Deserialize)]
34pub struct Request {
35 pub method: String,
36
37 #[serde(alias = "param")]
38 pub params: Option<ciborium::Value>,
39}
40impl Request {
41 pub fn new<M: Into<String>, C: Serialize, P: Into<Option<C>>>(method: M, params: P) -> Self {
43 let method = method.into();
44 let params = params
45 .into()
46 .map(|x| ciborium::Value::serialized(&x).unwrap());
47
48 Self { method, params }
49 }
50
51 pub fn extract_params<T: DeserializeOwned>(self) -> Result<T, ApiError> {
53 let value = self.params.unwrap_or(ciborium::Value::Null);
54 value.deserialized().map_err(ApiError::invalid_params)
55 }
56}
57
58#[derive(Debug, Clone, Serialize, Deserialize)]
73#[serde(rename_all = "kebab-case", tag = "status", content = "payload")]
74pub enum Response {
75 Ok(ciborium::Value),
76 Err(ApiError),
77}
78impl Response {
79 pub fn new<T: Serialize>(result: Result<T, ApiError>) -> Self {
85 match result {
86 Ok(val) => Self::Ok(ciborium::Value::serialized(&val).unwrap()),
87 Err(err) => Self::Err(err),
88 }
89 }
90
91 pub fn into_result<T: DeserializeOwned>(self) -> Result<T, ApiError> {
93 match self {
94 Self::Ok(val) => Ok(val
95 .deserialized()
96 .map_err(|err| ApiError::bad_response("TypeError", format!("{:?}", err)))?),
97 Self::Err(err) => Err(err),
98 }
99 }
100}
101
102#[derive(Debug)]
104pub struct MessageProto<T> {
105 pub(crate) inner: T,
106 pub(crate) size_limit: usize,
107}
108impl<T> MessageProto<T> {
109 pub const DEFAULT_SIZE_LIMIT: usize = 128 * 1024;
110
111 pub fn new(inner: T, size_limit: usize) -> Self {
113 Self { inner, size_limit }
114 }
115
116 pub fn set_size_limit(&mut self, new: usize) -> usize {
118 std::mem::replace(&mut self.size_limit, new)
119 }
120
121 pub fn size_limit(&self) -> usize {
123 self.size_limit
124 }
125
126 pub fn into_inner(self) -> T {
128 self.inner
129 }
130}
131impl<T> AsRef<T> for MessageProto<T> {
132 fn as_ref(&self) -> &T {
133 &self.inner
134 }
135}
136impl<T> AsMut<T> for MessageProto<T> {
137 fn as_mut(&mut self) -> &mut T {
138 &mut self.inner
139 }
140}
141impl<T> From<T> for MessageProto<T> {
142 fn from(value: T) -> Self {
143 Self::new(value, Self::DEFAULT_SIZE_LIMIT)
144 }
145}
146
147#[derive(Debug, thiserror::Error)]
148pub enum Error {
149 #[error("{0}")]
150 Io(std::io::Error),
151
152 #[error("{0}")]
153 CborSer(ciborium::ser::Error<std::io::Error>),
154
155 #[error("{0}")]
156 CborDe(ciborium::de::Error<std::io::Error>),
157
158 #[error("message too long: {0} bytes")]
159 MessageTooLong(usize),
160}
161impl From<std::io::Error> for Error {
162 fn from(value: std::io::Error) -> Self {
163 Self::Io(value)
164 }
165}
166impl From<ciborium::de::Error<std::io::Error>> for Error {
167 fn from(value: ciborium::de::Error<std::io::Error>) -> Self {
168 Self::CborDe(value)
169 }
170}
171impl From<ciborium::ser::Error<std::io::Error>> for Error {
172 fn from(value: ciborium::ser::Error<std::io::Error>) -> Self {
173 Self::CborSer(value)
174 }
175}