Skip to main content

sqlitegraph_cli/
reasoning.rs

1use serde_json::{json, Map, Value};
2
3use crate::dsl::{parse_dsl, DslResult};
4use sqlitegraph::SqliteGraphError;
5
6const ERR_PREFIX: &str = "cli";
7
8/// Simplified command handler for v0.2.5
9/// Removed: subgraph, pipeline, explain-pipeline, safety-check (depend on missing modules)
10pub fn handle_command(
11    _client: &crate::BackendClient,
12    command: &str,
13    args: &[String],
14) -> Result<Option<String>, SqliteGraphError> {
15    match command {
16        "dsl-parse" => run_dsl_parse(args).map(Some),
17        // Removed commands that depend on missing modules:
18        // - subgraph (needs subgraph module)
19        // - pipeline (needs pipeline module)
20        // - explain-pipeline (needs pipeline module)
21        // - safety-check (needs safety module)
22        // - metrics (needs metrics_snapshot which may not exist)
23        _ => Ok(None),
24    }
25}
26
27fn run_dsl_parse(args: &[String]) -> Result<String, SqliteGraphError> {
28    let input = required_value(args, "--input")?;
29    let result = parse_dsl(&input);
30    let summary = summarize_dsl(result)?;
31    let mut object = Map::new();
32    object.insert("command".into(), Value::String("dsl-parse".into()));
33    object.insert("result".into(), summary);
34    encode(object)
35}
36
37fn summarize_dsl(result: DslResult) -> Result<Value, SqliteGraphError> {
38    match result {
39        DslResult::Pattern(pattern) => Ok(json!({
40            "type": "pattern",
41            "legs": pattern.legs.len(),
42        })),
43        DslResult::Error(msg) => Err(invalid(msg)),
44    }
45}
46
47fn required_value(args: &[String], flag: &str) -> Result<String, SqliteGraphError> {
48    value(args, flag).ok_or_else(|| invalid(format!("missing {flag}")))
49}
50
51fn value(args: &[String], flag: &str) -> Option<String> {
52    let mut iter = args.iter();
53    while let Some(arg) = iter.next() {
54        if arg == flag {
55            return iter.next().cloned();
56        }
57    }
58    None
59}
60
61fn encode(object: Map<String, Value>) -> Result<String, SqliteGraphError> {
62    serde_json::to_string(&Value::Object(object))
63        .map_err(|e| invalid(format!("{ERR_PREFIX} serialization failed: {e}")))
64}
65
66fn invalid<T: Into<String>>(message: T) -> SqliteGraphError {
67    SqliteGraphError::invalid_input(message.into())
68}