1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#[cfg(feature = "serdejson")]
use base64::{decode, encode, DecodeError};
#[cfg(feature = "serdevalid")]
use paste;
#[cfg(feature = "serdevalid")]
use regex::Regex;
#[cfg(feature = "serdejson")]
use serde::de::{Deserialize, Deserializer, Error};
#[cfg(feature = "serdejson")]
use serde::ser::{Serialize, Serializer};
#[cfg(feature = "serdevalid")]
use serde_valid;
use std::ops::{Deref, DerefMut};

#[derive(Debug, Clone, PartialEq, PartialOrd)]
/// Base64-encoded byte array
pub struct ByteArray(pub Vec<u8>);

#[cfg(feature = "serdejson")]
impl Serialize for ByteArray {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&encode(&self.0))
    }
}

#[cfg(feature = "serdejson")]
impl<'de> Deserialize<'de> for ByteArray {
    fn deserialize<D>(deserializer: D) -> Result<ByteArray, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        match decode(s) {
            Ok(bin) => Ok(ByteArray(bin)),
            _ => Err(D::Error::custom("invalid base64")),
        }
    }
}

// Validation macro to create impls for serde_valid integration.
#[cfg(feature = "serdevalid")]
macro_rules! impl_validate_byte_array {
    (
        $ErrorType:ident,
        $DiveFn:expr,
        $limit_type:ty$(,)*

    ) => {
        paste::paste! {
            #[cfg(feature = "serdevalid")]
            impl serde_valid::[<Validate $ErrorType >] for ByteArray {
                fn [<validate_ $ErrorType:snake>](&self, limit: $limit_type) -> Result<(), serde_valid::[<$ErrorType Error>]> {
                    self.$DiveFn.[<validate_ $ErrorType:snake>](limit)
                }
            }
        }
    };
}

// Allow validation of encoded string.
#[cfg(feature = "serdevalid")]
impl_validate_byte_array!(Pattern, to_string(), &Regex);
#[cfg(feature = "serdevalid")]
impl_validate_byte_array!(MaxLength, to_string(), usize);
#[cfg(feature = "serdevalid")]
impl_validate_byte_array!(MinLength, to_string(), usize);

#[cfg(feature = "serdevalid")]
impl serde_valid::ValidateEnumerate<&'static str> for ByteArray {
    fn validate_enumerate(
        &self,
        enumerate: &[&'static str],
    ) -> Result<(), serde_valid::EnumerateError> {
        self.to_string().validate_enumerate(enumerate)
    }
}

// Also allow validation decoded internals.
#[cfg(feature = "serdevalid")]
impl_validate_byte_array!(MaxItems, 0, usize);
#[cfg(feature = "serdevalid")]
impl_validate_byte_array!(MinItems, 0, usize);

#[cfg(feature = "serdevalid")]
impl serde_valid::ValidateUniqueItems for ByteArray {
    fn validate_unique_items(&self) -> Result<(), serde_valid::UniqueItemsError> {
        self.0.validate_unique_items()
    }
}

impl std::str::FromStr for ByteArray {
    type Err = DecodeError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Self(decode(s)?))
    }
}

impl ToString for ByteArray {
    fn to_string(&self) -> String {
        encode(&self.0)
    }
}

impl Deref for ByteArray {
    type Target = Vec<u8>;
    fn deref(&self) -> &Vec<u8> {
        &self.0
    }
}

impl DerefMut for ByteArray {
    fn deref_mut(&mut self) -> &mut Vec<u8> {
        &mut self.0
    }
}

#[cfg(test)]
#[cfg(feature = "serdevalid")]
mod serde_tests {
    use super::*;
    use serde_json::json;
    use serde_valid::Validate;
    use std::str::FromStr;

    #[derive(Validate)]
    struct ValidateByteArrayStruct {
        // Validate encoded as string
        #[validate(enumerate("YWJjZGU="))]
        #[validate(max_length = 8)]
        #[validate(min_length = 8)]
        #[validate(pattern = ".*=")]
        // Validate decoded as Vec (b"abcde")
        #[validate(max_items = 5)]
        #[validate(min_items = 5)]
        #[validate(unique_items)]
        byte_array: ByteArray,
    }

    #[test]
    fn valid_byte_array() {
        let test_struct = ValidateByteArrayStruct {
            byte_array: ByteArray::from_str("YWJjZGU=").unwrap(),
        };
        assert!(test_struct.validate().is_ok());
    }

    #[test]
    fn invalid_few_byte_array() {
        let test_struct = ValidateByteArrayStruct {
            byte_array: ByteArray::from_str("ZmZm").unwrap(),
        };
        let errors = test_struct.validate().unwrap_err().to_string();
        assert_eq!(
            errors,
            json!({"errors":[],
                "properties":{
                    "byte_array":{
                        "errors":[
                            "The value must be in [YWJjZGU=].",
                            "The length of the value must be `>= 8`.",
                            "The value must match the pattern of \".*=\".",
                            "The length of the items must be `>= 5`.",
                            "The items must be unique."
                            ]
                        }
                    }
                }
            )
            .to_string()
        );
    }

    #[test]
    fn invalid_many_byte_array() {
        let test_struct = ValidateByteArrayStruct {
            byte_array: ByteArray::from_str("ZmZmZmZmZg==").unwrap(),
        };
        let errors = test_struct.validate().unwrap_err().to_string();
        assert_eq!(
            errors,
            json!({"errors":[],
                "properties":{
                    "byte_array":{
                        "errors":[
                            "The value must be in [YWJjZGU=].",
                            "The length of the value must be `<= 8`.",
                            "The length of the items must be `<= 5`.",
                            "The items must be unique."
                            ]
                        }
                    }
                }
            )
            .to_string()
        );
    }
}