use crate::ffi::{borrow_slice, take_buffer};
use crate::{Database, Error};
use antfly_embedded_sys as sys;
#[derive(Debug)]
pub struct GraphError {
pub native: Error,
pub diagnostic: Option<serde_json::Value>,
pub body: Vec<u8>,
}
impl From<Error> for GraphError {
fn from(native: Error) -> Self {
Self {
native,
diagnostic: None,
body: Vec::new(),
}
}
}
impl std::fmt::Display for GraphError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(diagnostic) = &self.diagnostic {
if let Some(message) = diagnostic["message"].as_str() {
return f.write_str(message);
}
}
self.native.fmt(f)
}
}
impl std::error::Error for GraphError {}
impl Database {
fn graph_call(
&self,
call: impl FnOnce(*mut sys::antfly_db, *mut sys::antfly_buffer) -> i32,
) -> Result<Vec<u8>, GraphError> {
let (code, body) = self.with_handle(|handle| {
let mut out = sys::antfly_buffer::default();
let code = call(handle, &mut out);
Ok((code, unsafe { take_buffer(out) }))
})?;
if code == sys::ANTFLY_OK {
return Ok(body);
}
Err(GraphError {
native: Error::from_code(code),
diagnostic: serde_json::from_slice(&body).ok(),
body,
})
}
pub fn graph_catalog_json(&self, request: impl AsRef<[u8]>) -> Result<Vec<u8>, GraphError> {
self.graph_call(|handle, out| unsafe {
sys::antfly_db_graph_catalog_json(handle, borrow_slice(request.as_ref()), out)
})
}
pub fn cypher_json(
&self,
graph: impl AsRef<[u8]>,
request: impl AsRef<[u8]>,
) -> Result<Vec<u8>, GraphError> {
self.graph_call(|handle, out| unsafe {
sys::antfly_db_cypher_json(
handle,
borrow_slice(graph.as_ref()),
borrow_slice(request.as_ref()),
out,
)
})
}
pub fn graph_write_status_json(
&self,
transaction_id: impl AsRef<[u8]>,
) -> Result<Vec<u8>, GraphError> {
self.graph_call(|handle, out| unsafe {
sys::antfly_db_graph_write_status_json(
handle,
borrow_slice(transaction_id.as_ref()),
out,
)
})
}
}