1use axum::response::{IntoResponse, Response};
2
3use crate::{Codec, ContentType};
4
5crate::macros::__private_encode_trait! {
6 }
12
13#[derive(Debug, thiserror::Error)]
18#[non_exhaustive]
19pub enum Error {
20 #[cfg(feature = "json")]
21 #[error(transparent)]
22 Json(#[from] serde_json::Error),
23 #[cfg(feature = "form")]
24 #[error(transparent)]
25 Form(#[from] serde_urlencoded::ser::Error),
26 #[cfg(feature = "msgpack")]
27 #[error(transparent)]
28 MsgPack(#[from] rmp_serde::encode::Error),
29 #[cfg(feature = "cbor")]
30 #[error(transparent)]
31 Cbor(#[from] ciborium::ser::Error<std::io::Error>),
32 #[cfg(feature = "bincode")]
33 #[error(transparent)]
34 Bincode(#[from] bincode::error::EncodeError),
35 #[cfg(feature = "yaml")]
36 #[error(transparent)]
37 Yaml(#[from] serde_yaml::Error),
38 #[cfg(feature = "toml")]
39 #[error(transparent)]
40 Toml(#[from] toml::ser::Error),
41}
42
43impl IntoResponse for Error {
44 fn into_response(self) -> Response {
45 use axum::http::StatusCode;
46
47 #[cfg(debug_assertions)]
48 return (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()).into_response();
49 #[cfg(not(debug_assertions))]
50 StatusCode::INTERNAL_SERVER_ERROR.into_response()
51 }
52}
53
54#[cfg(feature = "serde")]
55impl<T> Codec<T>
56where
57 T: serde::Serialize,
58{
59 #[cfg(feature = "json")]
65 #[inline]
66 pub fn to_json(&self) -> Result<Vec<u8>, serde_json::Error> {
67 serde_json::to_vec(&self.0)
68 }
69
70 #[cfg(feature = "form")]
76 #[inline]
77 pub fn to_form(&self) -> Result<Vec<u8>, serde_urlencoded::ser::Error> {
78 serde_urlencoded::to_string(&self.0).map(String::into_bytes)
79 }
80
81 #[cfg(feature = "msgpack")]
87 #[inline]
88 pub fn to_msgpack(&self) -> Result<Vec<u8>, rmp_serde::encode::Error> {
89 rmp_serde::to_vec_named(&self.0)
90 }
91
92 #[cfg(feature = "cbor")]
98 #[inline]
99 pub fn to_cbor(&self) -> Result<Vec<u8>, ciborium::ser::Error<std::io::Error>> {
100 let mut buf = Vec::new();
101 ciborium::into_writer(&self.0, &mut buf)?;
102 Ok(buf)
103 }
104
105 #[cfg(feature = "yaml")]
111 #[inline]
112 pub fn to_yaml(&self) -> Result<String, serde_yaml::Error> {
113 serde_yaml::to_string(&self.0)
114 }
115
116 #[cfg(feature = "toml")]
122 #[inline]
123 pub fn to_toml(&self) -> Result<String, toml::ser::Error> {
124 toml::to_string(&self.0)
125 }
126}
127
128impl<T> Codec<T> {
129 #[cfg(feature = "bincode")]
135 #[inline]
136 pub fn to_bincode(&self) -> Result<Vec<u8>, bincode::error::EncodeError>
137 where
138 T: bincode::Encode,
139 {
140 bincode::encode_to_vec(&self.0, bincode::config::standard())
141 }
142
143 #[cfg(feature = "bitcode")]
149 #[inline]
150 pub fn to_bitcode(&self) -> Vec<u8>
151 where
152 T: bitcode::Encode,
153 {
154 bitcode::encode(&self.0)
155 }
156
157 pub fn to_bytes(&self, content_type: ContentType) -> Result<Vec<u8>, Error>
163 where
164 T: CodecEncode,
165 {
166 Ok(match content_type {
167 #[cfg(feature = "json")]
168 ContentType::Json => self.to_json()?,
169 #[cfg(feature = "form")]
170 ContentType::Form => self.to_form()?,
171 #[cfg(feature = "msgpack")]
172 ContentType::MsgPack => self.to_msgpack()?,
173 #[cfg(feature = "bincode")]
174 ContentType::Bincode => self.to_bincode()?,
175 #[cfg(feature = "bitcode")]
176 ContentType::Bitcode => self.to_bitcode(),
177 #[cfg(feature = "cbor")]
178 ContentType::Cbor => self.to_cbor()?,
179 #[cfg(feature = "yaml")]
180 ContentType::Yaml => self.to_yaml()?.into_bytes(),
181 #[cfg(feature = "toml")]
182 ContentType::Toml => self.to_toml()?.into_bytes(),
183 })
184 }
185}