Skip to main content

antfly_embedded/
cypher.rs

1// Copyright 2026 Antfly, Inc.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use crate::ffi::{borrow_slice, take_buffer};
17use crate::{Database, Error};
18use antfly_embedded_sys as sys;
19
20#[derive(Debug)]
21pub struct GraphError {
22    pub native: Error,
23    pub diagnostic: Option<serde_json::Value>,
24    pub body: Vec<u8>,
25}
26impl From<Error> for GraphError {
27    fn from(native: Error) -> Self {
28        Self {
29            native,
30            diagnostic: None,
31            body: Vec::new(),
32        }
33    }
34}
35impl std::fmt::Display for GraphError {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        if let Some(diagnostic) = &self.diagnostic {
38            if let Some(message) = diagnostic["message"].as_str() {
39                return f.write_str(message);
40            }
41        }
42        self.native.fmt(f)
43    }
44}
45impl std::error::Error for GraphError {}
46impl Database {
47    fn graph_call(
48        &self,
49        call: impl FnOnce(*mut sys::antfly_db, *mut sys::antfly_buffer) -> i32,
50    ) -> Result<Vec<u8>, GraphError> {
51        let (code, body) = self.with_handle(|handle| {
52            let mut out = sys::antfly_buffer::default();
53            let code = call(handle, &mut out);
54            Ok((code, unsafe { take_buffer(out) }))
55        })?;
56        if code == sys::ANTFLY_OK {
57            return Ok(body);
58        }
59        Err(GraphError {
60            native: Error::from_code(code),
61            diagnostic: serde_json::from_slice(&body).ok(),
62            body,
63        })
64    }
65    pub fn graph_catalog_json(&self, request: impl AsRef<[u8]>) -> Result<Vec<u8>, GraphError> {
66        self.graph_call(|handle, out| unsafe {
67            sys::antfly_db_graph_catalog_json(handle, borrow_slice(request.as_ref()), out)
68        })
69    }
70    pub fn cypher_json(
71        &self,
72        graph: impl AsRef<[u8]>,
73        request: impl AsRef<[u8]>,
74    ) -> Result<Vec<u8>, GraphError> {
75        self.graph_call(|handle, out| unsafe {
76            sys::antfly_db_cypher_json(
77                handle,
78                borrow_slice(graph.as_ref()),
79                borrow_slice(request.as_ref()),
80                out,
81            )
82        })
83    }
84    pub fn graph_write_status_json(
85        &self,
86        transaction_id: impl AsRef<[u8]>,
87    ) -> Result<Vec<u8>, GraphError> {
88        self.graph_call(|handle, out| unsafe {
89            sys::antfly_db_graph_write_status_json(
90                handle,
91                borrow_slice(transaction_id.as_ref()),
92                out,
93            )
94        })
95    }
96}