#[provable_contracts_macros::contract("pmat-core.yaml", equation = "path_exists")]
pub async fn handle_refactor_status(
checkpoint: PathBuf,
format: RefactorOutputFormat,
) -> anyhow::Result<()> {
println!("📊 Reading refactor status from: {}", checkpoint.display());
validate_checkpoint_file(checkpoint.as_path())?;
let checkpoint_data = read_checkpoint_data(&checkpoint).await?;
format_refactor_status(&checkpoint_data, format, &checkpoint)?;
Ok(())
}
fn validate_checkpoint_file(checkpoint: &Path) -> anyhow::Result<()> {
if !checkpoint.exists() {
return Err(anyhow::anyhow!(
"Checkpoint file not found: {}",
checkpoint.display()
));
}
Ok(())
}
async fn read_checkpoint_data(checkpoint: &PathBuf) -> anyhow::Result<String> {
tokio::fs::read_to_string(checkpoint)
.await
.map_err(Into::into)
}
fn format_refactor_status(
checkpoint_data: &str,
format: RefactorOutputFormat,
checkpoint: &PathBuf,
) -> anyhow::Result<()> {
match format {
RefactorOutputFormat::Json => format_as_json(checkpoint_data),
RefactorOutputFormat::Table => format_as_table(checkpoint_data),
RefactorOutputFormat::Summary => format_as_summary(checkpoint_data, checkpoint.as_path()),
}
}
fn format_as_json(checkpoint_data: &str) -> anyhow::Result<()> {
let parsed: serde_json::Value = serde_json::from_str(checkpoint_data)?;
println!("{}", serde_json::to_string_pretty(&parsed)?);
Ok(())
}
fn format_as_table(checkpoint_data: &str) -> anyhow::Result<()> {
let state: serde_json::Value = serde_json::from_str(checkpoint_data)?;
print_table_header();
print_table_data(&state);
print_table_footer();
Ok(())
}
fn print_table_header() {
println!("┌─────────────────┬──────────────────────────────────────┐");
println!("│ Property │ Value │");
println!("├─────────────────┼──────────────────────────────────────┤");
}
fn print_table_data(state: &serde_json::Value) {
if let Some(current) = state.get("current") {
println!(
"│ Current State │ {:36} │",
format!("{current:?}").chars().take(36).collect::<String>()
);
}
if let Some(targets) = state.get("targets") {
if let Some(targets_array) = targets.as_array() {
println!("│ Target Count │ {:36} │", targets_array.len());
}
}
if let Some(index) = state.get("current_target_index") {
println!("│ Current Index │ {:36} │", index.as_u64().unwrap_or(0));
}
}
fn print_table_footer() {
println!("└─────────────────┴──────────────────────────────────────┘");
}
fn format_as_summary(checkpoint_data: &str, checkpoint: &Path) -> anyhow::Result<()> {
let state: serde_json::Value = serde_json::from_str(checkpoint_data)?;
println!("🔧 Refactor Status Summary");
println!(" Checkpoint: {}", checkpoint.display());
if let Some(current) = state.get("current") {
println!(" Current state: {current:?}");
}
if let Some(targets) = state.get("targets") {
if let Some(targets_array) = targets.as_array() {
println!(" Total targets: {}", targets_array.len());
}
}
Ok(())
}