use anyhow::{Context, Result};
use sqry_core::graph::unified::concurrent::GraphSnapshot;
use sqry_core::graph::unified::resolution::{FileScope, ResolutionMode, SymbolQuery};
pub fn run(snapshot: &GraphSnapshot, symbol: &str, explain: bool, json: bool) -> Result<()> {
let query = SymbolQuery {
symbol,
file_scope: FileScope::Any,
mode: ResolutionMode::AllowSuffixCandidates,
};
let plane = snapshot.binding_plane();
let resolution = plane.resolve(&query);
if json {
print_json(&plane, symbol, &resolution, explain)?;
} else {
print_text(&plane, symbol, &resolution, explain);
}
Ok(())
}
fn print_text(
plane: &sqry_core::graph::unified::bind::BindingPlane<'_>,
symbol: &str,
resolution: &sqry_core::graph::unified::bind::BindingResolution,
explain: bool,
) {
println!("symbol: {symbol}");
println!("outcome: {:?}", resolution.result.outcome);
for binding in &resolution.result.bindings {
println!(
" - node_id={:?} classification={:?} kind={:?}",
binding.node_id, binding.classification, binding.kind
);
}
if explain {
let rendering = plane.explain(resolution);
println!("\nwitness trace:");
print!("{}", rendering.text);
}
}
fn print_json(
plane: &sqry_core::graph::unified::bind::BindingPlane<'_>,
symbol: &str,
resolution: &sqry_core::graph::unified::bind::BindingResolution,
explain: bool,
) -> Result<()> {
let bindings: Vec<serde_json::Value> = resolution
.result
.bindings
.iter()
.map(|b| {
serde_json::json!({
"node_id": format!("{:?}", b.node_id),
"classification": format!("{:?}", b.classification),
"kind": format!("{:?}", b.kind),
})
})
.collect();
let explain_value: serde_json::Value = if explain {
let rendering = plane.explain(resolution);
rendering.json.clone()
} else {
serde_json::Value::Null
};
let outcome_value = serde_json::to_value(&resolution.result.outcome)
.unwrap_or_else(|_| serde_json::Value::String(format!("{:?}", resolution.result.outcome)));
let doc = serde_json::json!({
"symbol": symbol,
"outcome": outcome_value,
"bindings": bindings,
"explain": explain_value,
});
println!(
"{}",
serde_json::to_string_pretty(&doc).context("JSON serialization failed")?
);
Ok(())
}