maslc 1.0.1

Maduka Authorization Specification Language (MASL) toolchain and runtime
use std::fs;
use std::path::PathBuf;
use std::process::exit;

use crate::diagnostics::print_diagnostics;

/// Runs the `lint` command on a single `.mdk` file.
pub fn run(path: PathBuf) {
    let source = match fs::read_to_string(&path) {
        Ok(s) => s,
        Err(e) => {
            eprintln!("Error reading file: {}", e);
            exit(1);
        }
    };

    let filepath = path.to_str().unwrap_or("");

    match masl_parser::parse(&source) {
        Ok(ast) => match masl_hir::analyze(ast) {
            Ok(hir) => {
                let lint_diags = masl_linter::lint(&hir);
                if lint_diags.is_empty() {
                    println!("No lint warnings found.");
                    exit(0);
                } else {
                    print_diagnostics(&lint_diags, &source, filepath);
                    exit(1);
                }
            }
            Err(e) => {
                print_diagnostics(&e, &source, filepath);
                exit(1);
            }
        },
        Err(e) => {
            print_diagnostics(&e, &source, filepath);
            exit(1);
        }
    }
}