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
use super::rules::MatchBounds;
use failure::Fail;
use nom::types::CompleteStr;
use serde::Serialize;

pub(crate) const UNKNOWN: u32 = 1; // couldn't recognize token

#[derive(Debug, Fail, PartialEq, Clone, Serialize)]
pub enum DateTimeError {
    #[fail(display = "can't parse time unambiguously in: {}", msg)]
    AmbiguousTime { msg: String },
    #[fail(display = "invalid time in: {}, {} can't be {}", text, what, value)]
    InvalidTime {
        text: String,
        what: String,
        value: i32,
    },
    #[fail(display = "can't parse, rules intersection detected in {}", text)]
    IntersectionError { text: String },
}

#[derive(Debug, Clone, Serialize)]
pub struct SemanticError<'a> {
    // meta info for parser
    bounds: MatchBounds,
    tail: &'a str,

    // user visible error info
    error: DateTimeError,
}

impl<'a> SemanticError<'a> {
    pub fn extract_error(&self) -> DateTimeError {
        self.error.clone()
    }

    pub fn set_tail(&mut self, tail: CompleteStr<'a>) {
        self.tail = *tail;
    }

    pub fn get_tail(&self) -> &'a str {
        self.tail
    }

    pub fn set_bounds(&mut self, bounds: MatchBounds) {
        self.bounds = bounds;
    }

    pub fn get_start_idx(&self) -> usize {
        self.bounds.start_idx
    }

    pub fn get_end_idx(&self) -> usize {
        self.bounds.end_idx
    }
}

pub fn ambiguous_time_error(msg: &str) -> SemanticError {
    SemanticError {
        bounds: MatchBounds::new(0, 0),
        tail: "",
        error: DateTimeError::AmbiguousTime {
            msg: msg.to_owned(),
        },
    }
}

pub fn invalid_time_error<'a>(msg: &'a str, what: &'a str, value: i32) -> SemanticError<'a> {
    SemanticError {
        bounds: MatchBounds::new(0, 0),
        tail: "",
        error: DateTimeError::InvalidTime {
            text: msg.to_owned(),
            what: what.to_owned(),
            value,
        },
    }
}

pub fn intersection_error(text: &str) -> SemanticError {
    SemanticError {
        bounds: MatchBounds::new(0, 0),
        tail: "",
        error: DateTimeError::IntersectionError {
            text: text.to_owned(),
        },
    }
}