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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use fluent_syntax::parser::ParserError;
use std::{error::Error, fmt, ops::Range};

#[derive(Debug)]
struct Pos {
    line: usize,
    character: usize,
}

impl fmt::Display for Pos {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{};{}", self.line, self.character)
    }
}

fn unspan(src: &str, span: Range<usize>) -> Range<Pos> {
    let count = |idx| {
        let mut line = 1;
        let mut character = 1;
        for ch in src.bytes().take(idx) {
            // Count characters
            character += 1;

            // Count newlines
            if ch == b'\n' {
                line += 1;
                // If found new line, reset character count
                character = 1;
            }
        }
        Pos { line, character }
    };
    let Range { start, end } = span;
    count(start)..count(end)
}

// TODO:
// Ideally we wouldn't write this code, check this issue in fluent-rs.
// https://github.com/projectfluent/fluent-rs/issues/176
#[derive(Debug)]
pub enum ResourceErr {
    ParsingError {
        #[allow(dead_code)] // false-positive
        file: String,
        #[allow(dead_code)] // false-positive
        err: String,
    },
    #[allow(dead_code)] // false-positive
    BundleError(String),
}

impl ResourceErr {
    pub fn parsing_error(errs: Vec<ParserError>, file: String, src: &str) -> Self {
        let errs = errs
            .into_iter()
            .map(|e| {
                let Range {
                    start: from,
                    end: to,
                } = unspan(src, e.pos);
                format!("{from}..{to}, kind {:?}", e.kind)
            })
            .collect::<Vec<_>>();

        Self::ParsingError {
            file,
            err: format!("{errs:?}"),
        }
    }
}

impl fmt::Display for ResourceErr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{self:#?}") }
}

impl Error for ResourceErr {}