cronback_api_model/
payload.rs

1use std::collections::HashMap;
2
3#[cfg(feature = "dto")]
4use dto::{FromProto, IntoProto};
5use serde::{Deserialize, Serialize};
6use serde_with::{serde_as, skip_serializing_none};
7#[cfg(feature = "validation")]
8use validator::Validate;
9
10#[serde_as]
11#[skip_serializing_none]
12#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
13#[cfg_attr(
14    feature = "dto",
15    derive(IntoProto, FromProto),
16    proto(target = "proto::common::Payload")
17)]
18#[cfg_attr(feature = "validation", derive(Validate))]
19#[cfg_attr(
20    feature = "server",
21    derive(Default),
22    serde(default),
23    serde(deny_unknown_fields)
24)]
25pub struct Payload {
26    #[cfg_attr(
27        feature = "validation",
28        validate(length(
29            max = 30,
30            message = "Max number of headers reached (>=30)"
31        ))
32    )]
33    pub headers: HashMap<String, String>,
34    #[cfg_attr(feature = "server", serde(default = "default_content_type"))]
35    pub content_type: String,
36    #[cfg_attr(
37        feature = "validation",
38        validate(length(
39            min = 0,
40            max = 102400,
41            message = "Payload body size limit reached (>=102400)"
42        ))
43    )]
44    #[cfg_attr(feature = "dto", from_proto(map = "string_from_bytes"))]
45    pub body: String,
46}
47
48#[cfg(feature = "server")]
49fn default_content_type() -> String {
50    "application/json; charset=utf-8".to_owned()
51}
52
53#[cfg(feature = "dto")]
54fn string_from_bytes(input: Vec<u8>) -> String {
55    String::from_utf8(input).unwrap()
56}