use debtmap::analyzers::effects::analyze_file_effect;
use debtmap::config::DebtmapConfig;
use debtmap::core::Language;
use debtmap::effects::{run_effect, AnalysisEffect};
use debtmap::io::effects::read_file_effect;
use std::path::PathBuf;
use stillwater::EffectExt;
fn main() -> anyhow::Result<()> {
println!("Effect Pipeline Example\n");
println!("Demonstrating Stillwater effects for pure functional composition\n");
println!("=== Example 1: Basic Effect Composition ===\n");
example_basic_composition()?;
println!("\n=== Example 2: Pure Transformations ===\n");
example_pure_transformations()?;
println!("\n=== Example 3: Effect Error Handling ===\n");
example_error_handling()?;
println!("\n=== All Examples Complete ===");
Ok(())
}
fn example_basic_composition() -> anyhow::Result<()> {
let config = DebtmapConfig::default();
let pipeline = create_analysis_pipeline("src/lib.rs".into());
let function_count = run_effect(pipeline, config)?;
println!("Found {} functions in src/lib.rs", function_count);
Ok(())
}
fn example_pure_transformations() -> anyhow::Result<()> {
let config = DebtmapConfig::default();
let pipeline = read_file_effect("src/lib.rs".into())
.map(|content| {
println!(" [Pure] Counting lines...");
content.lines().count()
})
.map(|line_count| {
println!(" [Pure] Estimating complexity from {} lines", line_count);
(line_count as f64 / 10.0).ceil() as usize
})
.boxed();
let estimated_complexity = run_effect(pipeline, config)?;
println!("Estimated complexity: {}", estimated_complexity);
Ok(())
}
fn example_error_handling() -> anyhow::Result<()> {
let config = DebtmapConfig::default();
let pipeline = read_file_effect("nonexistent.rs".into())
.map(|content| content.len())
.boxed();
match run_effect(pipeline, config) {
Ok(len) => println!("File length: {}", len),
Err(e) => {
println!("Expected error occurred: {}", e);
println!("(This demonstrates effect error handling)");
}
}
Ok(())
}
fn create_analysis_pipeline(path: PathBuf) -> AnalysisEffect<usize> {
read_file_effect(path.clone())
.and_then(move |content| {
println!(" [I/O] Analyzing file: {}", path.display());
analyze_file_effect(path, content, Language::Rust)
})
.map(|metrics| {
println!(" [Pure] Extracting metrics...");
metrics.complexity.functions.len()
})
.boxed() }