Skip to content

Commit

Permalink
Use base64 encoding for serializing Vec<u8>
Browse files Browse the repository at this point in the history
  • Loading branch information
nociza committed Apr 25, 2023
1 parent aa57495 commit f5434df
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ license = "MIT"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
base64 = "0.21.0"
mysql = "23.0.1"
mysql_common = "0.29.2"
postgres = "0.19.4"
Expand Down
33 changes: 28 additions & 5 deletions src/dbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ pub trait Connection {
}

pub struct Database {
pub(crate) url: String,
pub(crate) connection: Box<dyn Connection>,
}

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

Ok(Database {
url: url.to_string(),
connection,
})
Ok(Database { connection })
}

pub fn execute_query(&mut self, query: &str) -> Result<QueryResult, Error> {
Expand Down Expand Up @@ -58,6 +54,7 @@ impl Database {
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub enum Value {
NULL,
#[serde(with = "base64")]
Bytes(Vec<u8>),
String(String),
Bool(bool),
Expand Down Expand Up @@ -137,3 +134,29 @@ pub enum ColumnType {
GEOMETRY,
UNKNOWN,
}

mod base64 {
use base64::Engine;
use serde::{Deserialize, Serialize};
use serde::{Deserializer, Serializer};

pub fn serialize<S: Serializer>(v: &Vec<u8>, s: S) -> Result<S::Ok, S::Error> {
let engine = base64::engine::GeneralPurpose::new(
&base64::alphabet::STANDARD,
base64::engine::general_purpose::NO_PAD,
);
let base64 = engine.encode(v);
String::serialize(&base64, s)
}

pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<Vec<u8>, D::Error> {
let base64 = String::deserialize(d)?;
let engine = base64::engine::GeneralPurpose::new(
&base64::alphabet::STANDARD,
base64::engine::general_purpose::NO_PAD,
);
engine
.decode(base64.as_bytes())
.map_err(serde::de::Error::custom)
}
}
2 changes: 1 addition & 1 deletion tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub(crate) async fn test_query_with_params_and_serialize(
let result = serde_json::to_string(&result)?;

// Verify the data returned by the query
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}"#;
let expected_result = r#"{"rows":[{"values":[{"Int":1},{"Bytes":"dXBkYXRlZA"}],"columns":[{"name":"id","column_type":"INT"},{"name":"name","column_type":"VARCHAR"}]}],"affected_rows":0}"#;
assert_eq!(result, expected_result);

_cleanup_database(database)?;
Expand Down

0 comments on commit f5434df

Please sign in to comment.