Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bson implementation #4

Merged
merged 8 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ edition = "2021"
[dependencies]
aes-gcm-siv = "0.11.1"
rand = "0.8.5"
bson = "2.9.0"
serde = "1.0.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
85 changes: 80 additions & 5 deletions src/bson.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,80 @@
// No definitions yet.
// Need to define the encapsulate and reveal functions.
// Because BSON depends on the protocol version,
// need to develop an Enum<Struct, Struct, ...> to
// work based on the protocol version.
use std::io::{Error, ErrorKind};

use bson::spec::BinarySubtype;
use bson::{bson, Binary, Bson, Document};
use serde::ser::{SerializeStruct, Serializer};
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct AccVersion1 {
v: i32,
//other stuff
d: bson::Binary,
amyipdev marked this conversation as resolved.
Show resolved Hide resolved
}
impl AccVersion1 {
fn new(data: Vec<u8>) -> AccVersion1 {
AccVersion1 {
v: 1,
d: bson::Binary {
subtype: bson::spec::BinarySubtype::Generic,
bytes: data,
},
}
}
}/*
impl Serialize for AccVersion1 {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut state = serializer.serialize_struct("AccVersion1", 2)?;
state.serialize_field("v", &self.v)?;
state.serialize_field("d", &self.d)?;
state.end()
}
}*/
#[derive(PartialEq, Debug)]
enum PacketVersion {
V1(AccVersion1),
}

fn encapsulate(acc_enum: &PacketVersion) -> Result<Vec<u8>, std::io::Error> {
let acc_struct = match acc_enum {
PacketVersion::V1(structure) => structure,
_ => return Err(Error::from(ErrorKind::InvalidData)),
};
Ok(bson::to_vec(&acc_struct).unwrap())
}

fn reveal(bson_vec: Vec<u8>) -> Result<PacketVersion, std::io::Error> {
let doc: Document = bson::from_slice(&bson_vec).unwrap();
let version = doc.get_i32("v");
let result = match version {
Ok(1) => {
let acc_struct: AccVersion1 = bson::from_document(doc).unwrap();
PacketVersion::V1(acc_struct)
}
Ok(0) => {
let acc_struct: AccVersion1 = bson::from_document(doc).unwrap();
PacketVersion::V1(acc_struct)
}
Ok(_) => return Err(Error::from(ErrorKind::Unsupported)),
Err(error) => panic!("Error: {}", error),
};
Ok(result)
}

#[cfg(test)]
mod tests {
use super::*;
use bson::{doc, spec::BinarySubtype, Document};

#[test]
fn bson_test() {
let pkt = PacketVersion::V1(AccVersion1::new(
b"somebody once told me the world was gonna roll me".to_vec(),
));
let result = reveal(encapsulate(&pkt).unwrap()).unwrap();
assert_eq!(pkt, result)
}
}
Loading