Skip to main content

synaptic_runnables/
pick.rs

1use async_trait::async_trait;
2use serde_json::Value;
3use synaptic_core::{RunnableConfig, SynapticError};
4
5use crate::Runnable;
6
7/// Extracts specified keys from a JSON object input.
8pub struct RunnablePick {
9    keys: Vec<String>,
10}
11
12impl RunnablePick {
13    pub fn new(keys: Vec<String>) -> Self {
14        Self { keys }
15    }
16}
17
18#[async_trait]
19impl Runnable<Value, Value> for RunnablePick {
20    async fn invoke(&self, input: Value, _config: &RunnableConfig) -> Result<Value, SynapticError> {
21        let obj = match &input {
22            Value::Object(map) => map,
23            other => {
24                return Err(SynapticError::Validation(format!(
25                    "RunnablePick expects a JSON object, got {}",
26                    other
27                )))
28            }
29        };
30
31        let mut result = serde_json::Map::new();
32        for key in &self.keys {
33            if let Some(value) = obj.get(key) {
34                result.insert(key.clone(), value.clone());
35            }
36        }
37
38        Ok(Value::Object(result))
39    }
40}