mod format;
mod repl;
use std::path::PathBuf;
use std::sync::Arc;
use anyhow::{Context, Result};
use clap::Parser;
use kglite::api::io::load_file;
use kglite::api::storage::{new_dir_graph_in_mode, StorageMode};
use kglite::api::DirGraph;
#[derive(Parser, Debug)]
#[command(name = "kglite", version, about)]
struct Cli {
graph: Option<PathBuf>,
}
fn main() -> Result<()> {
let cli = Cli::parse();
let (graph, source): (Arc<DirGraph>, Option<String>) = match &cli.graph {
Some(path) if path.exists() => {
let p = path.to_string_lossy().to_string();
let g = load_file(&p).with_context(|| format!("failed to open {p}"))?;
(g, Some(p))
}
Some(path) => {
let p = path.to_string_lossy().to_string();
eprintln!("note: {p} does not exist — starting an empty in-memory graph");
(Arc::new(fresh_graph()?), None)
}
None => (Arc::new(fresh_graph()?), None),
};
repl::run(graph, source.as_deref())
}
fn fresh_graph() -> Result<DirGraph> {
new_dir_graph_in_mode(StorageMode::Memory, None)
.map_err(|e| anyhow::anyhow!("failed to create an in-memory graph: {e}"))
}