Skip to content

Commit

Permalink
Add Pull protocol message
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwalker committed Jul 17, 2024
1 parent 7446592 commit aa94241
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/src/bolt/request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod discard;
mod extra;
mod goodbye;
mod hello;
mod pull;
mod reset;
mod rollback;

Expand All @@ -11,5 +12,6 @@ pub use discard::Discard;
pub use extra::WrapExtra;
pub use goodbye::Goodbye;
pub use hello::Hello;
pub use pull::Pull;
pub use reset::Reset;
pub use rollback::Rollback;
91 changes: 91 additions & 0 deletions lib/src/bolt/request/pull.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use crate::{
bolt::{
request::extra::{Extra, WrapExtra},
ExpectedResponse, Summary,
},
errors::Result,
summary::Streaming,
};
use serde::Serialize;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Pull {
extra: Extra,
}

impl WrapExtra for Pull {
fn create(extra: Extra) -> Self {
Self { extra }
}

fn extra_mut(&mut self) -> &mut Extra {
&mut self.extra
}
}

impl Serialize for Pull {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_newtype_variant("Request", 0x3F, "PULL", &self.extra)
}
}

impl ExpectedResponse for Pull {
type Response = Summary<Streaming>;
}

#[cfg(test)]
mod tests {
use super::*;
use crate::{
bolt::{Message as _, MessageResponse as _},
packstream::bolt,
};

#[test]
fn serialize() {
let hello = Pull::some(42).for_query(1);
let bytes = hello.to_bytes().unwrap();

let expected = bolt()
.structure(1, 0x3F)
.tiny_map(2)
.tiny_string("n")
.tiny_int(42)
.tiny_string("qid")
.tiny_int(1)
.build();

assert_eq!(bytes, expected);
}

#[test]
fn serialize_default_values() {
let hello = Pull::all();
let bytes = hello.to_bytes().unwrap();

let expected = bolt()
.structure(1, 0x3F)
.tiny_map(1)
.tiny_string("n")
.tiny_int(-1)
.build();

assert_eq!(bytes, expected);
}

#[test]
fn parse() {
let data = bolt()
.tiny_map(1)
.tiny_string("has_more")
.bool(true)
.build();

let response = Streaming::parse(data).unwrap();

assert_eq!(response, Streaming::HasMore);
}
}

0 comments on commit aa94241

Please sign in to comment.