rsmgp_sys/result/
mod.rs

1// Copyright (c) 2016-2021 Memgraph Ltd. [https://memgraph.com]
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//! Simplifies returning results to Memgraph and then to the client.
15
16use snafu::Snafu;
17use std::ffi::CStr;
18
19use crate::edge::*;
20use crate::list::*;
21use crate::map::*;
22use crate::memgraph::*;
23use crate::mgp::*;
24use crate::path::*;
25use crate::value::*;
26use crate::vertex::*;
27// Required here, if not present tests linking fails.
28#[double]
29use crate::mgp::ffi;
30use mockall_double::double;
31
32pub struct ResultRecord {
33    ptr: *mut mgp_result_record,
34    memgraph: Memgraph,
35}
36
37impl ResultRecord {
38    pub fn create(memgraph: &Memgraph) -> MgpResult<ResultRecord> {
39        unsafe {
40            let mgp_ptr = ffi::mgp_result_new_record(memgraph.result_ptr());
41            if mgp_ptr.is_null() {
42                return Err(MgpError::UnableToCreateResultRecord);
43            }
44            Ok(ResultRecord {
45                ptr: mgp_ptr,
46                memgraph: memgraph.clone(),
47            })
48        }
49    }
50
51    pub fn insert_mgp_value(&self, field: &CStr, value: &MgpValue) -> MgpResult<()> {
52        unsafe {
53            let inserted = ffi::mgp_result_record_insert(self.ptr, field.as_ptr(), value.mgp_ptr());
54            if inserted == 0 {
55                return Err(MgpError::UnableToInsertResultValue);
56            }
57            Ok(())
58        }
59    }
60
61    pub fn insert_null(&self, field: &CStr) -> MgpResult<()> {
62        self.insert_mgp_value(field, &MgpValue::make_null(&self.memgraph)?)
63    }
64
65    pub fn insert_bool(&self, field: &CStr, value: bool) -> MgpResult<()> {
66        self.insert_mgp_value(field, &MgpValue::make_bool(value, &self.memgraph)?)
67    }
68
69    pub fn insert_int(&self, field: &CStr, value: i64) -> MgpResult<()> {
70        self.insert_mgp_value(field, &MgpValue::make_int(value, &self.memgraph)?)
71    }
72
73    pub fn insert_double(&self, field: &CStr, value: f64) -> MgpResult<()> {
74        self.insert_mgp_value(field, &MgpValue::make_double(value, &self.memgraph)?)
75    }
76
77    pub fn insert_string(&self, field: &CStr, value: &CStr) -> MgpResult<()> {
78        self.insert_mgp_value(field, &MgpValue::make_string(value, &self.memgraph)?)
79    }
80
81    pub fn insert_list(&self, field: &CStr, value: &List) -> MgpResult<()> {
82        self.insert_mgp_value(field, &MgpValue::make_list(value, &self.memgraph)?)
83    }
84
85    pub fn insert_map(&self, field: &CStr, value: &Map) -> MgpResult<()> {
86        self.insert_mgp_value(field, &MgpValue::make_map(value, &self.memgraph)?)
87    }
88
89    pub fn insert_vertex(&self, field: &CStr, value: &Vertex) -> MgpResult<()> {
90        self.insert_mgp_value(field, &MgpValue::make_vertex(value, &self.memgraph)?)
91    }
92
93    pub fn insert_edge(&self, field: &CStr, value: &Edge) -> MgpResult<()> {
94        self.insert_mgp_value(field, &MgpValue::make_edge(value, &self.memgraph)?)
95    }
96
97    pub fn insert_path(&self, field: &CStr, value: &Path) -> MgpResult<()> {
98        self.insert_mgp_value(field, &MgpValue::make_path(value, &self.memgraph)?)
99    }
100}
101
102#[derive(Debug, PartialEq, Snafu)]
103#[snafu(visibility = "pub")]
104pub enum MgpError {
105    // EDGE
106    #[snafu(display("Unable to copy edge."))]
107    UnableToCopyEdge,
108
109    #[snafu(display("Unable to return edge property because of value allocation error."))]
110    UnableToReturnEdgePropertyValueAllocationError,
111
112    #[snafu(display("Unable to return edge property because of value creation error."))]
113    UnableToReturnEdgePropertyValueCreationError,
114
115    #[snafu(display("Unable to return edge property because of name allocation error."))]
116    UnableToReturnEdgePropertyNameAllocationError,
117
118    #[snafu(display("Unable to return edge properties iterator."))]
119    UnableToReturnEdgePropertiesIterator,
120
121    // LIST
122    #[snafu(display("Unable to create empty list."))]
123    UnableToCreateEmptyList,
124
125    #[snafu(display("Unable to copy list."))]
126    UnableToCopyList,
127
128    #[snafu(display("Unable to append list value."))]
129    UnableToAppendListValue,
130
131    #[snafu(display("Unable to append extend list value."))]
132    UnableToAppendExtendListValue,
133
134    #[snafu(display("Unable to access list value by index."))]
135    UnableToAccessListValueByIndex,
136
137    // MAP
138    #[snafu(display("Unable to copy map."))]
139    UnableToCopyMap,
140
141    #[snafu(display("Unable to create empty map."))]
142    UnableToCreateEmptyMap,
143
144    #[snafu(display("Unable to insert map value."))]
145    UnableToInsertMapValue,
146
147    #[snafu(display("Unable to access map value."))]
148    UnableToAccessMapValue,
149
150    #[snafu(display("Unable to create map iterator."))]
151    UnableToCreateMapIterator,
152
153    // MEMGRAPH
154    #[snafu(display("Unable to create graph vertices iterator."))]
155    UnableToCreateGraphVerticesIterator,
156
157    #[snafu(display("Unable to find vertex by id."))]
158    UnableToFindVertexById,
159
160    #[snafu(display("Unable to register read procedure."))]
161    UnableToRegisterReadProcedure,
162
163    #[snafu(display("Unable to add required arguments."))]
164    UnableToAddRequiredArguments,
165
166    #[snafu(display("Unable to add optional arguments."))]
167    UnableToAddOptionalArguments,
168
169    #[snafu(display("Unable to add return type."))]
170    UnableToAddReturnType,
171
172    #[snafu(display("Unable to add deprecated return type."))]
173    UnableToAddDeprecatedReturnType,
174
175    // PATH
176    #[snafu(display("Unable to copy path."))]
177    UnableToCopyPath,
178
179    #[snafu(display("Out of bound path vertex index."))]
180    OutOfBoundPathVertexIndex,
181
182    #[snafu(display("Out of bound path edge index."))]
183    OutOfBoundPathEdgeIndex,
184
185    #[snafu(display("Unable to create path with start Vertex."))]
186    UnableToCreatePathWithStartVertex,
187
188    #[snafu(display(
189        "Unable to expand path because of not matching vertex value or lack of memory."
190    ))]
191    UnableToExpandPath,
192
193    // RESULT
194    #[snafu(display("Unable to create result record."))]
195    UnableToCreateResultRecord,
196
197    #[snafu(display("Unable to insert result record."))]
198    UnableToInsertResultValue,
199
200    // VALUE
201    #[snafu(display("Unable to create new CString."))]
202    UnableToCreateCString,
203
204    #[snafu(display("Unable to make null value."))]
205    UnableToMakeNullValue,
206
207    #[snafu(display("Unable to make bool value."))]
208    UnableToMakeBoolValue,
209
210    #[snafu(display("Unable to make integer value."))]
211    UnableToMakeIntegerValue,
212
213    #[snafu(display("Unable to make double value."))]
214    UnableToMakeDoubleValue,
215
216    #[snafu(display("Unable to make Memgraph compatible string value."))]
217    UnableToMakeMemgraphStringValue,
218
219    #[snafu(display("Unable to make list value."))]
220    UnableToMakeListValue,
221
222    #[snafu(display("Unable to make map value."))]
223    UnableToMakeMapValue,
224
225    #[snafu(display("Unable to make vertex value."))]
226    UnableToMakeVertexValue,
227
228    #[snafu(display("Unable to make edge value."))]
229    UnableToMakeEdgeValue,
230
231    #[snafu(display("Unable to make path value."))]
232    UnableToMakePathValue,
233
234    #[snafu(display("Unable to make new Value::String."))]
235    UnableToMakeValueString,
236
237    // VERTEX
238    #[snafu(display("Unable to copy vertex."))]
239    UnableToCopyVertex,
240
241    #[snafu(display("Out of bound label index."))]
242    OutOfBoundLabelIndexError,
243
244    #[snafu(display("Unable to get vertex property."))]
245    UnableToGetVertexProperty,
246
247    #[snafu(display("Unable to return vertex property because of make name error."))]
248    UnableToReturnVertexPropertyMakeNameEror,
249
250    #[snafu(display("Unable to return vertex properties iterator."))]
251    UnableToReturnVertexPropertiesIterator,
252
253    #[snafu(display("Unable to return vertex in_edges iterator."))]
254    UnableToReturnVertexInEdgesIterator,
255
256    #[snafu(display("Unable to return vertex out_edges iterator."))]
257    UnableToReturnVertexOutEdgesIterator,
258}
259
260/// A result type holding [MgpError] by default.
261pub type MgpResult<T, E = MgpError> = std::result::Result<T, E>;
262
263#[cfg(test)]
264mod tests;