#![allow(dead_code)]
use std::path::Path;
use super::types::SerializableStreamSpec;
#[derive(Debug)]
pub enum AstLoadError {
EnvVarNotSet,
FileReadError { path: String, error: String },
ParseError(String),
}
impl std::fmt::Display for AstLoadError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AstLoadError::EnvVarNotSet => write!(f, "CARGO_MANIFEST_DIR not set"),
AstLoadError::FileReadError { path, error } => {
write!(f, "Failed to read AST file {:?}: {}", path, error)
}
AstLoadError::ParseError(e) => write!(f, "Failed to parse AST JSON: {}", e),
}
}
}
impl std::error::Error for AstLoadError {}
pub fn load_ast_from_file(ast_path: &str) -> Result<SerializableStreamSpec, AstLoadError> {
let manifest_dir =
std::env::var("CARGO_MANIFEST_DIR").map_err(|_| AstLoadError::EnvVarNotSet)?;
let full_path = Path::new(&manifest_dir).join(ast_path);
let content = std::fs::read_to_string(&full_path).map_err(|e| AstLoadError::FileReadError {
path: full_path.display().to_string(),
error: e.to_string(),
})?;
serde_json::from_str(&content).map_err(|e| AstLoadError::ParseError(e.to_string()))
}
pub fn load_ast_by_entity_name(entity_name: &str) -> Result<SerializableStreamSpec, AstLoadError> {
let ast_path = format!(".hyperstack/{}.ast.json", entity_name);
load_ast_from_file(&ast_path)
}