bolt_proto/
lib.rs

1#![warn(rust_2018_idioms)]
2
3//! This crate contains the primitives used in the [Bolt](https://neo4j.com/docs/bolt/current)
4//! protocol. The [`Message`] and [`Value`] enums are of particular importance, and are the primary
5//! units of information sent and consumed by Bolt clients/servers.
6
7pub use message::Message;
8pub use server_state::ServerState;
9pub use value::Value;
10
11pub mod error;
12pub mod message;
13mod serialization;
14mod server_state;
15pub mod value;
16pub mod version;
17
18#[doc(hidden)]
19#[macro_export]
20macro_rules! impl_message_with_metadata {
21    ($T:path) => {
22        impl $T {
23            pub fn new(
24                metadata: ::std::collections::HashMap<::std::string::String, $crate::value::Value>,
25            ) -> Self {
26                Self { metadata }
27            }
28
29            pub fn metadata(
30                &self,
31            ) -> &::std::collections::HashMap<::std::string::String, $crate::value::Value> {
32                &self.metadata
33            }
34        }
35    };
36}
37
38#[doc(hidden)]
39#[macro_export]
40macro_rules! impl_try_from_message {
41    ($T:path, $V:ident) => {
42        impl ::std::convert::TryFrom<$crate::Message> for $T {
43            type Error = $crate::error::ConversionError;
44
45            fn try_from(message: $crate::Message) -> $crate::error::ConversionResult<Self> {
46                match message {
47                    $crate::Message::$V(inner) => Ok(inner),
48                    _ => Err($crate::error::ConversionError::FromMessage(message)),
49                }
50            }
51        }
52    };
53}
54
55#[doc(hidden)]
56#[macro_export]
57macro_rules! impl_empty_message_tests {
58    ($T:ident) => {
59        mod tests {
60            use ::bytes::Bytes;
61            use ::std::sync::{Arc, Mutex};
62
63            use $crate::serialization::*;
64
65            use super::*;
66
67            #[test]
68            fn get_marker() {
69                assert_eq!($T.get_marker().unwrap(), MARKER);
70            }
71
72            #[test]
73            fn get_signature() {
74                assert_eq!($T.get_signature(), SIGNATURE);
75            }
76
77            #[test]
78            fn try_into_bytes() {
79                let msg = $T;
80                assert_eq!(
81                    msg.try_into_bytes().unwrap(),
82                    Bytes::from_static(&[MARKER, SIGNATURE])
83                );
84            }
85
86            #[test]
87            fn try_from_bytes() {
88                let msg = $T;
89                let msg_bytes = &[];
90                assert_eq!(
91                    $T::try_from(Arc::new(Mutex::new(Bytes::from_static(msg_bytes)))).unwrap(),
92                    msg
93                );
94            }
95        }
96    };
97}