arrow_graph/algorithms/
traits.rs

1use arrow::record_batch::RecordBatch;
2use serde::{Deserialize, Serialize};
3use crate::graph::ArrowGraph;
4use crate::error::Result;
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct AlgorithmParams {
8    pub params: serde_json::Map<String, serde_json::Value>,
9}
10
11impl AlgorithmParams {
12    pub fn new() -> Self {
13        Self {
14            params: serde_json::Map::new(),
15        }
16    }
17    
18    pub fn with_param<T: Serialize>(mut self, key: &str, value: T) -> Self {
19        self.params.insert(
20            key.to_string(),
21            serde_json::to_value(value).unwrap(),
22        );
23        self
24    }
25    
26    pub fn get<T: for<'de> Deserialize<'de>>(&self, key: &str) -> Option<T> {
27        self.params.get(key)
28            .and_then(|v| serde_json::from_value(v.clone()).ok())
29    }
30}
31
32impl Default for AlgorithmParams {
33    fn default() -> Self {
34        Self::new()
35    }
36}
37
38pub trait GraphAlgorithm {
39    fn execute(&self, graph: &ArrowGraph, params: &AlgorithmParams) -> Result<RecordBatch>;
40    
41    fn name(&self) -> &'static str;
42    
43    fn description(&self) -> &'static str {
44        "Graph algorithm"
45    }
46}