1use crate::{Error, Result, Row, StringColumn};
13use base64::{engine::general_purpose::STANDARD, Engine as _};
14
15#[derive(Debug, Clone, PartialEq, Eq, Default)]
19pub struct Base64Bytes(pub Vec<u8>);
20
21impl Base64Bytes {
22 pub fn into_vec(self) -> Vec<u8> {
23 self.0
24 }
25 pub fn as_slice(&self) -> &[u8] {
26 &self.0
27 }
28}
29
30impl From<Vec<u8>> for Base64Bytes {
31 fn from(v: Vec<u8>) -> Self {
32 Base64Bytes(v)
33 }
34}
35impl From<Base64Bytes> for Vec<u8> {
36 fn from(b: Base64Bytes) -> Self {
37 b.0
38 }
39}
40
41impl StringColumn for Base64Bytes {
42 fn to_column_str(&self) -> String {
43 STANDARD.encode(&self.0)
44 }
45 fn from_column_str(s: &str) -> Result<Self> {
46 STANDARD
47 .decode(s)
48 .map(Base64Bytes)
49 .map_err(|e| Error::msg(format!("invalid base64 in column: {e}")))
50 }
51}
52
53impl crate::SqlType for Base64Bytes {
57 const COLUMN_TYPE: crate::ColumnType = crate::ColumnType::Text;
58}
59impl crate::ToColumn for Base64Bytes {
60 fn to_value(&self) -> sea_query::Value {
61 sea_query::Value::String(Some(Box::new(self.to_column_str())))
62 }
63}
64impl crate::FromColumn for Base64Bytes {
65 fn from_column(row: &dyn Row, col: &str) -> Result<Self> {
66 let s =
67 row.get_string(col)?.ok_or_else(|| Error::msg(format!("column `{col}` was NULL")))?;
68 <Base64Bytes as StringColumn>::from_column_str(&s)
69 }
70}