use std::fs;
use tempfile::TempDir;
use wlk::{DeltaOperation, ShadowFile};
fn main() -> anyhow::Result<()> {
println!("=== WLK Branching Workflow Example ===\n");
println!("This demonstrates WLK's implicit branching feature.\n");
let temp_dir = TempDir::new()?;
let file_path = temp_dir.path().join("story.txt");
println!("1. Starting with initial content...");
let initial_content = "Once upon a time, there was a developer.";
fs::write(&file_path, initial_content)?;
let mut shadow = ShadowFile::new("story.txt", initial_content.to_string());
let shadow_path = temp_dir.path().join(".wlk").join("story.txt.wlk");
fs::create_dir_all(shadow_path.parent().unwrap())?;
println!(" Initial: {:?}\n", initial_content);
println!("2. Creating Branch 1: Happy ending...");
shadow.apply_delta(
DeltaOperation::Insert {
index: initial_content.len(),
value: " They lived happily ever after.".to_string(),
},
Some("Happy ending".to_string()),
)?;
let happy_id = shadow.current_head.clone();
let happy_content = shadow.current_content()?;
println!(" Delta ID: {}", happy_id);
println!(" Content: {:?}\n", happy_content);
println!("3. Rewinding to initial state...");
let initial_id = shadow.initial_snapshot.id.clone();
shadow.set_head(&initial_id)?;
println!(" Back at: d0\n");
println!("4. Creating Branch 2: Adventure ending...");
shadow.apply_delta(
DeltaOperation::Insert {
index: initial_content.len(),
value: " They embarked on an epic adventure.".to_string(),
},
Some("Adventure ending".to_string()),
)?;
let adventure_id = shadow.current_head.clone();
let adventure_content = shadow.current_content()?;
println!(" Delta ID: {}", adventure_id);
println!(" Content: {:?}\n", adventure_content);
println!("5. Rewinding to initial state again...");
let initial_id = shadow.initial_snapshot.id.clone();
shadow.set_head(&initial_id)?;
println!("6. Creating Branch 3: Mystery ending...");
shadow.apply_delta(
DeltaOperation::Insert {
index: initial_content.len(),
value: " They vanished without a trace.".to_string(),
},
Some("Mystery ending".to_string()),
)?;
let mystery_id = shadow.current_head.clone();
let mystery_content = shadow.current_content()?;
println!(" Delta ID: {}", mystery_id);
println!(" Content: {:?}\n", mystery_content);
println!("7. Branch structure:");
println!(" d0 (initial)");
println!(" ├─ {} (happy ending)", happy_id);
println!(" ├─ {} (adventure ending)", adventure_id);
println!(" └─ {} (mystery ending) ← HEAD\n", mystery_id);
let leaves = shadow.find_leaves();
println!("8. All branch endpoints:");
for leaf in &leaves {
let content = shadow.reconstruct_at(&leaf.id)?;
println!(" {} - {:?}", leaf.id, leaf.annotation);
println!(" → {:?}", content);
}
println!("\n=== Key Insight ===");
println!("In WLK, you don't explicitly create branches.");
println!("Just rewind to any point and make changes - a new branch is created automatically!");
println!("Each file can have multiple parallel histories without any complexity.");
Ok(())
}