1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use std::convert::Infallible;

use derive_more::From;

use kodept_ast::traits::{Accessor, IdProducer, Linker};
use kodept_core::code_point::CodePoint;
use kodept_core::file_relative::CodePath;

use crate::error::report::{Report, ReportMessage};

#[derive(Debug, From)]
pub enum UnrecoverableError {
    Report(Report),
    Infallible(Infallible),
}

impl UnrecoverableError {
    pub fn into_report(self) -> Report {
        match self {
            UnrecoverableError::Report(x) => x,
            UnrecoverableError::Infallible(_) => unreachable!(),
        }
    }
}

pub trait Reporter: FileContextual {
    fn report_and_fail<R: Into<ReportMessage>>(
        &self,
        at: Vec<CodePoint>,
        message: R,
    ) -> Result<Infallible, UnrecoverableError> {
        Err(Report::new(&self.file_path(), at, message).into())
    }

    fn add_report<R: Into<ReportMessage>>(&mut self, at: Vec<CodePoint>, message: R) {
        self.report(Report::new(&self.file_path(), at, message))
    }

    fn report(&mut self, report: Report);
}

pub trait FileContextual {
    fn file_path(&self) -> CodePath;
}

pub trait Context<'c>: IdProducer + Linker<'c> + Accessor<'c> + Reporter {}

impl<'c, T: IdProducer + Linker<'c> + Accessor<'c> + Reporter> Context<'c> for T {}