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.insert(name.into(), ParameterValue::Integer(value));
87    }
88
89    /// Sets a float parameter.
90    pub fn set_float(&mut self, name: impl Into<String>, value: f64) {
91        self.values.insert(name.into(), ParameterValue::Float(value));
92    }
93
94    /// Sets a string parameter.
95    pub fn set_string(&mut self, name: impl Into<String>, value: impl Into<String>) {
96        self.values.insert(name.into(), ParameterValue::String(value.into()));
97    }
98
99    /// Sets a boolean parameter.
100    pub fn set_bool(&mut self, name: impl Into<String>, value: bool) {
101        self.values.insert(name.into(), ParameterValue::Boolean(value));
102    }
103
104    /// Gets an integer parameter.
105    pub fn get_int(&self, name: &str) -> Option<i64> {
106        match self.values.get(name) {
107            Some(ParameterValue::Integer(v)) => Some(*v),
108            _ => None,
109        }
110    }
111
112    /// Gets a float parameter.
113    pub fn get_float(&self, name: &str) -> Option<f64> {
114        match self.values.get(name) {
115            Some(ParameterValue::Float(v)) => Some(*v),
116            _ => None,
117        }
118    }
119
120    /// Gets a string parameter.
121    pub fn get_string(&self, name: &str) -> Option<&str> {
122        match self.values.get(name) {
123            Some(ParameterValue::String(v)) => Some(v),
124            _ => None,
125        }
126    }
127
128    /// Gets a boolean parameter.
129    pub fn get_bool(&self, name: &str) -> Option<bool> {
130        match self.values.get(name) {
131            Some(ParameterValue::Boolean(v)) => Some(*v),
132            _ => None,
133        }
134    }
135}
136
137impl Default for Parameters {
138    fn default() -> Self {
139        Self::new()
140    }
141}
142
143/// A parameter value.
144#[derive(Debug, Clone)]
145enum ParameterValue {
146    Integer(i64),
147    Float(f64),
148    String(String),
149    Boolean(bool),
150}
151
152/// Result of an algorithm execution.
153pub struct AlgorithmResult {
154    /// Result columns.
155    pub columns: Vec<String>,
156    /// Result rows.
157    pub rows: Vec<Vec<graphos_common::types::Value>>,
158}
159
160impl AlgorithmResult {
161    /// Creates a new empty result.
162    pub fn new(columns: Vec<String>) -> Self {
163        Self {
164            columns,
165            rows: Vec::new(),
166        }
167    }
168
169    /// Adds a row to the result.
170    pub fn add_row(&mut self, row: Vec<graphos_common::types::Value>) {
171        self.rows.push(row);
172    }
173
174    /// Returns the number of rows.
175    pub fn row_count(&self) -> usize {
176        self.rows.len()
177    }
178}