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
use core::fmt;
use std::path::PathBuf;

use crate::{
    artifacts::{error::SourceLocation, Severity},
    compilers::CompilationError,
};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct VyperSourceLocation {
    file: PathBuf,
    #[serde(rename = "lineno")]
    line: Option<u64>,
    #[serde(rename = "col_offset")]
    offset: Option<u64>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct VyperCompilationError {
    pub message: String,
    pub severity: Severity,
    pub source_location: Option<VyperSourceLocation>,
    pub formatted_message: Option<String>,
}

impl CompilationError for VyperCompilationError {
    fn is_warning(&self) -> bool {
        self.severity.is_warning()
    }

    fn is_error(&self) -> bool {
        self.severity.is_error()
    }

    fn source_location(&self) -> Option<SourceLocation> {
        None
    }

    fn severity(&self) -> Severity {
        self.severity
    }

    fn error_code(&self) -> Option<u64> {
        None
    }
}

impl fmt::Display for VyperCompilationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(location) = &self.source_location {
            write!(f, "Location: {}", location.file.display())?;
            if let Some(line) = location.line {
                write!(f, ":{}", line)?;
            }
            if let Some(offset) = location.offset {
                write!(f, ":{}", offset)?;
            }
            writeln!(f)?;
        }
        if let Some(message) = &self.formatted_message {
            write!(f, "{}", message)
        } else {
            write!(f, "{}", self.message)
        }
    }
}