kotoba_rewrite/rewrite/
applier.rs1use kotoba_core::ir::*;
4use kotoba_storage::KeyValueStore;
5use kotoba_core::types::*;
6use std::collections::HashMap;
7use std::sync::Arc;
8use tracing::warn;
9
10#[derive(Debug)]
12pub struct RuleApplier<T: KeyValueStore + 'static> {
13 storage: Arc<T>,
14}
15
16impl<T: KeyValueStore + 'static> RuleApplier<T> {
17 pub fn new(storage: Arc<T>) -> Self {
18 Self { storage }
19 }
20
21 pub async fn apply_rule(&self, graph_key: &str, rule: &RuleIR, match_data: &serde_json::Value) -> anyhow::Result<serde_json::Value> {
23 warn!("Rule application not fully implemented yet");
25
26 Ok(serde_json::json!({
28 "graph_key": graph_key,
29 "rule_name": rule.name,
30 "match_data": match_data,
31 "patch": {
32 "deletions": [],
33 "additions": [],
34 "updates": []
35 },
36 "status": "pending"
37 }))
38 }
39
40 pub async fn apply_patch(&self, graph_key: &str, patch: &serde_json::Value) -> anyhow::Result<()> {
42 warn!("Patch application not implemented yet");
44
45 let patch_key = format!("patch:{}:{}", graph_key, chrono::Utc::now().timestamp());
47 let patch_bytes = serde_json::to_vec(patch)?;
48
49 self.storage.put(patch_key.as_bytes(), &patch_bytes).await?;
50 Ok(())
51 }
52
53 pub async fn save_patch(&self, graph_key: &str, rule_name: &str, patch: &serde_json::Value) -> anyhow::Result<()> {
55 let patch_key = format!("patch:{}:{}:{}", graph_key, rule_name, chrono::Utc::now().timestamp());
56 let patch_bytes = serde_json::to_vec(patch)?;
57
58 self.storage.put(patch_key.as_bytes(), &patch_bytes).await?;
59 Ok(())
60 }
61
62 pub async fn load_patches(&self, graph_key: &str) -> anyhow::Result<Vec<serde_json::Value>> {
64 let prefix = format!("patch:{}:", graph_key);
65 let keys = self.storage.scan(prefix.as_bytes()).await?;
66
67 let mut patches = Vec::new();
68 for key_bytes in keys {
69 if let Ok(key_str) = std::str::from_utf8(&key_bytes.0) {
70 if key_str.starts_with(&prefix) {
71 if let Some(patch_data) = self.storage.get(&key_bytes.0).await? {
72 if let Ok(patch_json) = serde_json::from_slice::<serde_json::Value>(&patch_data) {
73 patches.push(patch_json);
74 }
75 }
76 }
77 }
78 }
79
80 Ok(patches)
81 }
82}