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
use std::{
    error::Error,
    fmt::{Display, Formatter},
    ops::Range,
    path::PathBuf,
};

use crate::{Validation, VosError, VosErrorKind};

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

impl Error for VosError {}

impl VosError {
    pub fn kind(&self) -> &VosErrorKind {
        &*self.kind
    }
    pub fn file(&self) -> PathBuf {
        PathBuf::from(&self.file)
    }
    pub fn range(&self) -> Option<Range<u32>> {
        self.range.clone()
    }

    pub fn with_file(mut self, file_path: impl Into<String>) -> Self {
        self.set_file(file_path);
        self
    }
    pub fn set_file(&mut self, file_path: impl Into<String>) {
        self.file = file_path.into()
    }
    pub fn with_range(mut self, range: Range<u32>) -> Self {
        self.set_range(range);
        self
    }
    pub fn set_range(&mut self, range: Range<u32>) {
        self.range = Some(range)
    }
}

impl VosError {
    pub fn parse_error(message: impl Into<String>) -> Self {
        Self { kind: Box::new(VosErrorKind::ParseError(message.into())), file: "".to_string(), range: None }
    }
}

impl<T> Validation<T> {
    pub fn no_problem(&self) -> bool {
        match self {
            Validation::Success { warn, error, .. } => error.is_empty() && warn.is_empty(),
            Validation::Failure { .. } => false,
        }
    }
}