Skip to content

Commit e794551

Browse files
authored
Merge pull request #8 from CoLearn-Dev/base64_serializer
Use base64 encoding for serializing Vec<u8>
2 parents 35089ba + f5434df commit e794551

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ license = "MIT"
1010
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1111

1212
[dependencies]
13+
base64 = "0.21.0"
1314
mysql = "23.0.1"
1415
mysql_common = "0.29.2"
1516
postgres = "0.19.4"

src/dbc.rs

+28-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ pub trait Connection {
1212
}
1313

1414
pub struct Database {
15-
pub(crate) url: String,
1615
pub(crate) connection: Box<dyn Connection>,
1716
}
1817

@@ -24,10 +23,7 @@ impl Database {
2423
_ => return Err("Unsupported dbc type".into()),
2524
};
2625

27-
Ok(Database {
28-
url: url.to_string(),
29-
connection,
30-
})
26+
Ok(Database { connection })
3127
}
3228

3329
pub fn execute_query(&mut self, query: &str) -> Result<QueryResult, Error> {
@@ -58,6 +54,7 @@ impl Database {
5854
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
5955
pub enum Value {
6056
NULL,
57+
#[serde(with = "base64")]
6158
Bytes(Vec<u8>),
6259
String(String),
6360
Bool(bool),
@@ -137,3 +134,29 @@ pub enum ColumnType {
137134
GEOMETRY,
138135
UNKNOWN,
139136
}
137+
138+
mod base64 {
139+
use base64::Engine;
140+
use serde::{Deserialize, Serialize};
141+
use serde::{Deserializer, Serializer};
142+
143+
pub fn serialize<S: Serializer>(v: &Vec<u8>, s: S) -> Result<S::Ok, S::Error> {
144+
let engine = base64::engine::GeneralPurpose::new(
145+
&base64::alphabet::STANDARD,
146+
base64::engine::general_purpose::NO_PAD,
147+
);
148+
let base64 = engine.encode(v);
149+
String::serialize(&base64, s)
150+
}
151+
152+
pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<Vec<u8>, D::Error> {
153+
let base64 = String::deserialize(d)?;
154+
let engine = base64::engine::GeneralPurpose::new(
155+
&base64::alphabet::STANDARD,
156+
base64::engine::general_purpose::NO_PAD,
157+
);
158+
engine
159+
.decode(base64.as_bytes())
160+
.map_err(serde::de::Error::custom)
161+
}
162+
}

tests/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ pub(crate) async fn test_query_with_params_and_serialize(
121121
let result = serde_json::to_string(&result)?;
122122

123123
// Verify the data returned by the query
124-
let expected_result = r#"{"rows":[{"values":[{"Int":1},{"Bytes":[117,112,100,97,116,101,100]}],"columns":[{"name":"id","column_type":"INT"},{"name":"name","column_type":"VARCHAR"}]}],"affected_rows":0}"#;
124+
let expected_result = r#"{"rows":[{"values":[{"Int":1},{"Bytes":"dXBkYXRlZA"}],"columns":[{"name":"id","column_type":"INT"},{"name":"name","column_type":"VARCHAR"}]}],"affected_rows":0}"#;
125125
assert_eq!(result, expected_result);
126126

127127
_cleanup_database(database)?;

0 commit comments

Comments
 (0)