use errcraft::ErrFrame;
fn read_config(path: &str) -> Result<String, ErrFrame> {
std::fs::read_to_string(path).map_err(|e| {
ErrFrame::new("Failed to read configuration file")
.context("path", path.to_string())
.context("operation", "read")
.with_source(e)
})
}
fn parse_config(content: &str) -> Result<(), ErrFrame> {
if content.is_empty() {
return Err(ErrFrame::new("Configuration file is empty")
.context_text("Expected valid TOML content"));
}
Ok(())
}
fn load_and_parse_config(path: &str) -> Result<(), ErrFrame> {
let content = read_config(path)?;
parse_config(&content)?;
Ok(())
}
fn main() {
println!("=== errcraft Simple Example ===\n");
let err1 = ErrFrame::new("Something went wrong");
println!("Example 1: Basic error");
err1.eprint();
println!();
let err2 = ErrFrame::new("Database connection failed")
.context("host", "localhost")
.context("port", 5432)
.context("database", "myapp");
println!("Example 2: Error with context");
err2.eprint();
println!();
println!("Example 3: Real file error");
if let Err(e) = load_and_parse_config("nonexistent.toml") {
e.eprint();
}
println!();
let err4 = ErrFrame::new("Upload failed")
.context("file", "report.pdf")
.context("size_mb", 50)
.context_text("File size exceeded the 25MB limit")
.context_text("Please compress the file and try again");
println!("Example 4: Error with text context");
err4.eprint();
}