An asynchronous client for Bolt-compatible servers.
The below example demonstrates how to connect to a Neo4j server and send it Bolt messages.
use std::collections::HashMap;
use std::convert::TryFrom;
use std::env;
use std::iter::FromIterator;
use failure::Error;
use tokio::prelude::*;
use bolt_client::*;
use bolt_client::message::*;
use bolt_client::value::*;
#[tokio::main]
async fn main() -> Result<(), Error> {
let mut client = Client::new(env::var("BOLT_TEST_ADDR")?,
env::var("BOLT_TEST_DOMAIN").ok().as_deref()).await?;
let response_msg: Message = client.init(
"my-client-name/1.0".to_string(),
HashMap::from_iter(vec![
("scheme".to_string(), "basic".to_string()),
("principal".to_string(), env::var("BOLT_TEST_USERNAME")?),
("credentials".to_string(), env::var("BOLT_TEST_PASSWORD")?),
])).await?;
assert!(Success::try_from(response_msg).is_ok());
let response_msg = client.run("RETURN 1 as num;".to_string(), None).await?;
assert!(Success::try_from(response_msg).is_ok());
let (response_msg, records): (Message, Vec<Record>) = client.pull_all().await?;
assert!(Success::try_from(response_msg).is_ok());
assert_eq!(records[0].fields(), &[Value::from(1 as i8)]);
client.run("MATCH (n) DETACH DELETE n;".to_string(), None).await?;
client.pull_all().await?;
client.run("CREATE (:Client)-[:WRITTEN_IN]->(:Language {name: $name});".to_string(),
Some(HashMap::from_iter(
vec![("name".to_string(), Value::from("Rust"))]
))).await?;
client.pull_all().await?;
client.run("MATCH (rust:Language) RETURN rust;".to_string(), None).await?;
let (response_msg, records): (Message, Vec<Record>) = client.pull_all().await?;
assert!(Success::try_from(response_msg).is_ok());
let node = Node::try_from(records[0].fields()[0].clone())?;
assert_eq!(node.labels(), &["Language".to_string()]);
assert_eq!(node.properties(),
&HashMap::from_iter(vec![("name".to_string(), Value::from("Rust"))]));
Ok(())
}