graphos_adapters/plugins/
traits.rs1use graphos_common::utils::error::Result;
4use std::collections::HashMap;
5
6pub trait Plugin: Send + Sync {
8 fn name(&self) -> &str;
10
11 fn version(&self) -> &str;
13
14 fn on_load(&self) -> Result<()> {
16 Ok(())
17 }
18
19 fn on_unload(&self) -> Result<()> {
21 Ok(())
22 }
23}
24
25pub trait Algorithm: Send + Sync {
27 fn name(&self) -> &str;
29
30 fn description(&self) -> &str;
32
33 fn parameters(&self) -> &[ParameterDef];
35
36 fn execute(&self, params: &Parameters) -> Result<AlgorithmResult>;
38}
39
40#[derive(Debug, Clone)]
42pub struct ParameterDef {
43 pub name: String,
45 pub description: String,
47 pub param_type: ParameterType,
49 pub required: bool,
51 pub default: Option<String>,
53}
54
55#[derive(Debug, Clone, Copy, PartialEq, Eq)]
57pub enum ParameterType {
58 Integer,
60 Float,
62 String,
64 Boolean,
66 NodeId,
68}
69
70pub struct Parameters {
72 values: HashMap<String, ParameterValue>,
74}
75
76impl Parameters {
77 pub fn new() -> Self {
79 Self {
80 values: HashMap::new(),
81 }
82 }
83
84 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 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 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 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 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 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 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 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#[derive(Debug, Clone)]
149enum ParameterValue {
150 Integer(i64),
151 Float(f64),
152 String(String),
153 Boolean(bool),
154}
155
156pub struct AlgorithmResult {
158 pub columns: Vec<String>,
160 pub rows: Vec<Vec<graphos_common::types::Value>>,
162}
163
164impl AlgorithmResult {
165 pub fn new(columns: Vec<String>) -> Self {
167 Self {
168 columns,
169 rows: Vec::new(),
170 }
171 }
172
173 pub fn add_row(&mut self, row: Vec<graphos_common::types::Value>) {
175 self.rows.push(row);
176 }
177
178 pub fn row_count(&self) -> usize {
180 self.rows.len()
181 }
182}