use {anyhow::Context, problemo::*};

// This example demonstrates conversion to/from Anyhow errors.
// It requires the "anyhow" feature to be enabled.

tag_error!(FileSystemError);
tag_error!(OperatingSystemError);

fn parse_file_anyhow(path: &str) -> anyhow::Result<String> {
    std::fs::read_to_string(path)
        .context("file system")
        .context("operating system")
}

fn parse_file_problemo(path: &str) -> Result<String, Problem> {
    std::fs::read_to_string(path)
        .via(FileSystemError)
        .via(OperatingSystemError)
}

fn main() {
    if let Err(anyhow_error) = parse_file_anyhow("non-existing.txt") {
        println!("Anyhow:");
        for error in anyhow_error.chain() {
            println!("• {}", error);
        }

        println!("\nAnyhow to Problemo:");
        for cause in anyhow_error.into_problem() {
            for error in cause.iter_sources() {
                println!("• {}", error);
            }
        }
    }

    if let Err(problem) = parse_file_problemo("non-existing.txt") {
        println!("\nProblemo:");
        for cause in &problem {
            for error in cause.iter_sources() {
                println!("• {}", error);
            }
        }

        println!("\nProblemo to Anyhow:");
        let anyhow_error: anyhow::Error = problem.into();
        for error in anyhow_error.chain() {
            println!("• {}", error);
        }
    }
}
