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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use proc_macro2::Span;

use std::{
    fmt::{self, Display},
    ops::Range,
};

#[derive(Debug, PartialEq)]
pub(crate) struct ParseError {
    pub(crate) pos: usize,
    pub(crate) kind: ParseErrorKind,
}

#[derive(Debug, PartialEq)]
pub(crate) enum ParseErrorKind {
    /// A `{` that wasn't closed.
    UnclosedArg,
    /// A `}` that doesn't close an argument.
    InvalidClosedArg,
    /// When parsing the number of a positional arguments
    NotANumber {
        what: String,
    },
    /// When parsing the identifier of a named argument
    NotAnIdent {
        what: String,
    },
    UnknownFormatting {
        what: String,
    },
}

#[allow(dead_code)]
impl ParseErrorKind {
    pub fn not_a_number(what: &str) -> Self {
        Self::NotANumber {
            what: what.to_string(),
        }
    }
    pub fn not_an_ident(what: &str) -> Self {
        Self::NotAnIdent {
            what: what.to_string(),
        }
    }
    pub fn unknown_formatting(what: &str) -> Self {
        Self::UnknownFormatting {
            what: what.to_string(),
        }
    }
}

////////////////////////////////////////////////////////////////////////////////

#[derive(Debug, PartialEq)]
pub(crate) struct DisplayParseError<'a> {
    pub(crate) str: &'a str,
    pub(crate) error_span: Range<usize>,
    pub(crate) kind: ParseErrorKind,
}
impl ParseError {
    fn error_span(&self) -> Range<usize> {
        let len = match &self.kind {
            ParseErrorKind::UnclosedArg => 0,
            ParseErrorKind::InvalidClosedArg => 0,
            ParseErrorKind::NotANumber { what } => what.len(),
            ParseErrorKind::NotAnIdent { what } => what.len(),
            ParseErrorKind::UnknownFormatting { what } => what.len(),
        };

        self.pos..self.pos + len
    }

    pub(crate) fn into_crate_err(self, span: Span, original_str: &str) -> crate::Error {
        let display = DisplayParseError {
            str: original_str,
            error_span: self.error_span(),
            kind: self.kind,
        };

        crate::Error::new(span, display)
    }
}

impl Display for ParseErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ParseErrorKind::UnclosedArg => f.write_str("unclosed argument"),
            ParseErrorKind::InvalidClosedArg => f.write_str("`}` closing a nonexistent argument"),
            ParseErrorKind::NotANumber { what } => writeln!(f, "not a number: \"{}\"", what),
            ParseErrorKind::NotAnIdent { what } => {
                writeln!(f, "not a valid identifier: \"{}\"", what)
            }
            ParseErrorKind::UnknownFormatting { what } => {
                writeln!(f, "unknown formatting: \"{}\"", what)
            }
        }
    }
}

impl Display for DisplayParseError<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("failed to parse the format string ")?;

        // Gets the amount of chars up to the error,
        // this is good enough for most cases,
        // but doesn't acount for multi-char characters.
        let chars = self.str[..self.error_span.start].chars().count();
        writeln!(f, "at the character number {}, ", chars)?;

        Display::fmt(&self.kind, f)?;

        Ok(())
    }
}