actix_msgpack/
msgpack_response_builder.rs

1use crate::MsgPackError;
2use actix_web::{http::header::ContentType, web::Bytes, HttpResponse, HttpResponseBuilder};
3use mime::APPLICATION_MSGPACK;
4use serde::Serialize;
5
6pub trait MsgPackResponseBuilder {
7	/// MessagePack Responder
8	fn msgpack<T: Serialize>(&mut self, value: T) -> HttpResponse;
9}
10
11impl MsgPackResponseBuilder for HttpResponseBuilder {
12	fn msgpack<T: Serialize>(&mut self, value: T) -> HttpResponse {
13		match rmp_serde::to_vec_named(&value) {
14			Ok(body) => {
15				self.insert_header(ContentType(APPLICATION_MSGPACK));
16				self.body(Bytes::from(body))
17			},
18			Err(err) => HttpResponse::from_error(MsgPackError::Serialize(err)),
19		}
20	}
21}