From a56e2d2685e9723690c4601a3c13ddef50490c30 Mon Sep 17 00:00:00 2001 From: Pierpaolo Follia Date: Wed, 18 Dec 2024 19:28:52 +0100 Subject: [PATCH] Add Ignore to current Bolt implementation (#204) * Implement ignore to possible responses for summary for current bolt implementation * Run cargo fmt * Fix clippy warnings --------- Co-authored-by: Paul Horn --- lib/src/messages.rs | 18 +++++++++++++++--- lib/src/messages/ignore.rs | 38 ++++++++++++++++++++++++++++++++++++++ lib/src/types/serde/de.rs | 2 +- 3 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 lib/src/messages/ignore.rs diff --git a/lib/src/messages.rs b/lib/src/messages.rs index f6976d11..af7e8d6e 100644 --- a/lib/src/messages.rs +++ b/lib/src/messages.rs @@ -4,6 +4,7 @@ mod commit; mod discard; mod failure; mod hello; +mod ignore; mod pull; mod record; mod reset; @@ -11,6 +12,7 @@ mod rollback; mod run; mod success; +use crate::messages::ignore::Ignore; use crate::{ errors::{Error, Result}, types::{BoltMap, BoltWireFormat}, @@ -28,6 +30,7 @@ pub(crate) use success::Success; pub enum BoltResponse { Success(Success), Failure(Failure), + Ignore(Ignore), Record(Record), } @@ -222,15 +225,24 @@ impl BoltResponse { let record = Record::parse(version, &mut response)?; return Ok(BoltResponse::Record(record)); } - Err(Error::UnknownMessage(format!( - "unknown message {:?}", - response + if Ignore::can_parse(version, &response) { + let ignore = Ignore::parse(version, &mut response)?; + return Ok(BoltResponse::Ignore(ignore)); + } + Err(Error::UnknownMessage(response.iter().fold( + "unknown message ".to_owned(), + |mut output, byte| { + use std::fmt::Write; + let _ = write!(output, "{:02X}", byte); + output + }, ))) } pub fn into_error(self, msg: &'static str) -> Error { match self { BoltResponse::Failure(failure) => Error::Neo4j(failure.into_error()), + BoltResponse::Ignore(ignore) => Error::Neo4j(ignore.into_error()), _ => Error::UnexpectedMessage(format!("unexpected response for {}: {:?}", msg, self)), } } diff --git a/lib/src/messages/ignore.rs b/lib/src/messages/ignore.rs new file mode 100644 index 00000000..acd452be --- /dev/null +++ b/lib/src/messages/ignore.rs @@ -0,0 +1,38 @@ +use crate::errors::Neo4jError; +use neo4rs_macros::BoltStruct; + +#[derive(Debug, PartialEq, Clone, BoltStruct)] +#[signature(0xB0, 0x7E)] +pub struct Ignore; + +impl Ignore { + pub(crate) fn into_error(self) -> Neo4jError { + Neo4jError::new( + "Neo.ServerError.Ignored".into(), + "The request was ignored by the server because it is in a FAILED or INTERRUPTED state" + .into(), + ) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::types::BoltWireFormat; + use crate::version::Version; + use bytes::Bytes; + + #[test] + fn should_deserialize_success() { + let mut data = Bytes::from_static(&[0xB0, 0x7E]); + + let failure: Ignore = Ignore::parse(Version::V4_1, &mut data).unwrap(); + let failure = failure.into_error(); + + assert_eq!(failure.code(), "Neo.ServerError.Ignored"); + assert_eq!( + failure.message(), + "The request was ignored by the server because it is in a FAILED or INTERRUPTED state" + ); + } +} diff --git a/lib/src/types/serde/de.rs b/lib/src/types/serde/de.rs index 9fea04c6..5fda3b7f 100644 --- a/lib/src/types/serde/de.rs +++ b/lib/src/types/serde/de.rs @@ -168,7 +168,7 @@ impl<'de> Deserialize<'de> for Offset { } struct OffsetVisitor; - impl<'de> Visitor<'de> for OffsetVisitor { + impl Visitor<'_> for OffsetVisitor { type Value = FixedOffset; fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {