wasmcloud_actor_graphdb/
generated.rs

1extern crate rmp_serde as rmps;
2use rmps::{Deserializer, Serializer};
3use serde::{Deserialize, Serialize};
4use std::io::Cursor;
5
6#[cfg(feature = "guest")]
7extern crate wapc_guest as guest;
8#[cfg(feature = "guest")]
9use guest::prelude::*;
10
11#[cfg(feature = "guest")]
12use lazy_static::lazy_static;
13#[cfg(feature = "guest")]
14use std::sync::RwLock;
15
16#[cfg(feature = "guest")]
17pub struct Host {
18    binding: String,
19}
20
21#[cfg(feature = "guest")]
22impl Default for Host {
23    fn default() -> Self {
24        Host {
25            binding: "default".to_string(),
26        }
27    }
28}
29
30/// Creates a named host binding
31#[cfg(feature = "guest")]
32pub fn host(binding: &str) -> Host {
33    Host {
34        binding: binding.to_string(),
35    }
36}
37
38/// Creates the default host binding
39#[cfg(feature = "guest")]
40pub fn default() -> Host {
41    Host::default()
42}
43
44#[cfg(feature = "guest")]
45impl Host {
46    /// Execute a query on a given graph
47    pub(crate) fn _query_graph(
48        &self,
49        graph_name: String,
50        query: String,
51    ) -> HandlerResult<QueryResponse> {
52        let input_args = QueryGraphArgs { graph_name, query };
53        host_call(
54            &self.binding,
55            "wasmcloud:graphdb",
56            "QueryGraph",
57            &serialize(input_args)?,
58        )
59        .map(|vec| {
60            let resp = deserialize::<QueryResponse>(vec.as_ref()).unwrap();
61            resp
62        })
63        .map_err(|e| e.into())
64    }
65    /// Delete a graph from the database
66    pub fn delete_graph(&self, graph_name: String) -> HandlerResult<DeleteResponse> {
67        let input_args = DeleteGraphArgs { graph_name };
68        host_call(
69            &self.binding,
70            "wasmcloud:graphdb",
71            "DeleteGraph",
72            &serialize(input_args)?,
73        )
74        .map(|vec| {
75            let resp = deserialize::<DeleteResponse>(vec.as_ref()).unwrap();
76            resp
77        })
78        .map_err(|e| e.into())
79    }
80}
81
82#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
83pub struct QueryGraphArgs {
84    #[serde(rename = "graphName")]
85    pub graph_name: String,
86    #[serde(rename = "query")]
87    pub query: String,
88}
89
90#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
91pub struct DeleteGraphArgs {
92    #[serde(rename = "graphName")]
93    pub graph_name: String,
94}
95
96/// Result from a graph query
97#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
98pub struct QueryResponse {
99    #[serde(rename = "resultSet")]
100    pub result_set: ResultSet,
101}
102
103/// Indicates the success of a delete operation
104#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
105pub struct DeleteResponse {
106    #[serde(rename = "success")]
107    pub success: bool,
108}
109
110/// Result from a graph query, contains a list of columns which were returned from
111/// the query and statistics about query time
112#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
113pub struct ResultSet {
114    #[serde(rename = "columns")]
115    pub columns: Vec<Column>,
116    #[serde(rename = "statistics")]
117    pub statistics: Vec<String>,
118}
119
120/// A single entity in a graph database, which may be a scalar, node, or relation
121#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
122pub struct Column {
123    #[serde(rename = "scalars")]
124    pub scalars: Option<Vec<Scalar>>,
125    #[serde(rename = "nodes")]
126    pub nodes: Option<Vec<Node>>,
127    #[serde(rename = "relations")]
128    pub relations: Option<Vec<Relation>>,
129}
130
131/// Represents a scalar value, all fields should be examined to determine the value
132/// contained in the scalar
133#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
134pub struct Scalar {
135    #[serde(rename = "boolValue")]
136    pub bool_value: Option<bool>,
137    #[serde(rename = "intValue")]
138    pub int_value: Option<i64>,
139    #[serde(rename = "doubleValue")]
140    pub double_value: Option<f64>,
141    #[serde(rename = "stringValue")]
142    pub string_value: Option<String>,
143}
144
145/// A node in a graph database, comprised of a list of labels and a map of
146/// properties
147#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
148pub struct Node {
149    #[serde(rename = "labels")]
150    pub labels: Vec<String>,
151    #[serde(rename = "properties")]
152    pub properties: std::collections::HashMap<String, Scalar>,
153}
154
155/// A relationship between exactly two nodes
156#[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)]
157pub struct Relation {
158    #[serde(rename = "typeName")]
159    pub type_name: String,
160    #[serde(rename = "properties")]
161    pub properties: std::collections::HashMap<String, Scalar>,
162}
163
164/// The standard function for serializing codec structs into a format that can be
165/// used for message exchange between actor and host. Use of any other function to
166/// serialize could result in breaking incompatibilities.
167pub fn serialize<T>(
168    item: T,
169) -> ::std::result::Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>>
170where
171    T: Serialize,
172{
173    let mut buf = Vec::new();
174    item.serialize(&mut Serializer::new(&mut buf).with_struct_map())?;
175    Ok(buf)
176}
177
178/// The standard function for de-serializing codec structs from a format suitable
179/// for message exchange between actor and host. Use of any other function to
180/// deserialize could result in breaking incompatibilities.
181pub fn deserialize<'de, T: Deserialize<'de>>(
182    buf: &[u8],
183) -> ::std::result::Result<T, Box<dyn std::error::Error + Send + Sync>> {
184    let mut de = Deserializer::new(Cursor::new(buf));
185    match Deserialize::deserialize(&mut de) {
186        Ok(t) => Ok(t),
187        Err(e) => Err(format!("Failed to de-serialize: {}", e).into()),
188    }
189}