Skip to main content

graphos_adapters/plugins/
traits.rs

1//! Plugin traits.
2
3use graphos_common::utils::error::Result;
4use std::collections::HashMap;
5
6/// A Graphos plugin.
7pub trait Plugin: Send + Sync {
8    /// Returns the name of the plugin.
9    fn name(&self) -> &str;
10
11    /// Returns the version of the plugin.
12    fn version(&self) -> &str;
13
14    /// Called when the plugin is loaded.
15    fn on_load(&self) -> Result<()> {
16        Ok(())
17    }
18
19    /// Called when the plugin is unloaded.
20    fn on_unload(&self) -> Result<()> {
21        Ok(())
22    }
23}
24
25/// A graph algorithm that can be invoked from queries.
26pub trait Algorithm: Send + Sync {
27    /// Returns the name of the algorithm.
28    fn name(&self) -> &str;
29
30    /// Returns a description of the algorithm.
31    fn description(&self) -> &str;
32
33    /// Returns the parameter definitions.
34    fn parameters(&self) -> &[ParameterDef];
35
36    /// Executes the algorithm.
37    fn execute(&self, params: &Parameters) -> Result<AlgorithmResult>;
38}
39
40/// Definition of an algorithm parameter.
41#[derive(Debug, Clone)]
42pub struct ParameterDef {
43    /// Parameter name.
44    pub name: String,
45    /// Parameter description.
46    pub description: String,
47    /// Parameter type.
48    pub param_type: ParameterType,
49    /// Whether the parameter is required.
50    pub required: bool,
51    /// Default value (if any).
52    pub default: Option<String>,
53}
54
55/// Types of algorithm parameters.
56#[derive(Debug, Clone, Copy, PartialEq, Eq)]
57pub enum ParameterType {
58    /// Integer parameter.
59    Integer,
60    /// Float parameter.
61    Float,
62    /// String parameter.
63    String,
64    /// Boolean parameter.
65    Boolean,
66    /// Node ID parameter.
67    NodeId,
68}
69
70/// Parameters passed to an algorithm.
71pub struct Parameters {
72    /// Parameter values.
73    values: HashMap<String, ParameterValue>,
74}
75
76impl Parameters {
77    /// Creates a new empty parameter set.
78    pub fn new() -> Self {
79        Self {
80            values: HashMap::new(),
81        }
82    }
83
84    /// Sets an integer parameter.
85    pub fn set_int(&mut self, name: impl Into<String>, value: i64) {
86        self.values
87            .insert(name.into(), ParameterValue::Integer(value));
88    }
89
90    /// Sets a float parameter.
91    pub fn set_float(&mut self, name: impl Into<String>, value: f64) {
92        self.values
93            .insert(name.into(), ParameterValue::Float(value));
94    }
95
96    /// Sets a string parameter.
97    pub fn set_string(&mut self, name: impl Into<String>, value: impl Into<String>) {
98        self.values
99            .insert(name.into(), ParameterValue::String(value.into()));
100    }
101
102    /// Sets a boolean parameter.
103    pub fn set_bool(&mut self, name: impl Into<String>, value: bool) {
104        self.values
105            .insert(name.into(), ParameterValue::Boolean(value));
106    }
107
108    /// Gets an integer parameter.
109    pub fn get_int(&self, name: &str) -> Option<i64> {
110        match self.values.get(name) {
111            Some(ParameterValue::Integer(v)) => Some(*v),
112            _ => None,
113        }
114    }
115
116    /// Gets a float parameter.
117    pub fn get_float(&self, name: &str) -> Option<f64> {
118        match self.values.get(name) {
119            Some(ParameterValue::Float(v)) => Some(*v),
120            _ => None,
121        }
122    }
123
124    /// Gets a string parameter.
125    pub fn get_string(&self, name: &str) -> Option<&str> {
126        match self.values.get(name) {
127            Some(ParameterValue::String(v)) => Some(v),
128            _ => None,
129        }
130    }
131
132    /// Gets a boolean parameter.
133    pub fn get_bool(&self, name: &str) -> Option<bool> {
134        match self.values.get(name) {
135            Some(ParameterValue::Boolean(v)) => Some(*v),
136            _ => None,
137        }
138    }
139}
140
141impl Default for Parameters {
142    fn default() -> Self {
143        Self::new()
144    }
145}
146
147/// A parameter value.
148#[derive(Debug, Clone)]
149enum ParameterValue {
150    Integer(i64),
151    Float(f64),
152    String(String),
153    Boolean(bool),
154}
155
156/// Result of an algorithm execution.
157pub struct AlgorithmResult {
158    /// Result columns.
159    pub columns: Vec<String>,
160    /// Result rows.
161    pub rows: Vec<Vec<graphos_common::types::Value>>,
162}
163
164impl AlgorithmResult {
165    /// Creates a new empty result.
166    pub fn new(columns: Vec<String>) -> Self {
167        Self {
168            columns,
169            rows: Vec::new(),
170        }
171    }
172
173    /// Adds a row to the result.
174    pub fn add_row(&mut self, row: Vec<graphos_common::types::Value>) {
175        self.rows.push(row);
176    }
177
178    /// Returns the number of rows.
179    pub fn row_count(&self) -> usize {
180        self.rows.len()
181    }
182}