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
use std::fmt::{Debug, Display, Formatter};

use ariadne::{Color, Report, ReportKind};

use crate::{errors::ValkyrieReport, FileSpan, ValkyrieError, ValkyrieErrorKind};

mod kind;

#[derive(Copy, Clone)]
pub enum DuplicateKind {
    Type = 1002,
    Function = 1003,
    Variable = 1004,
}

#[derive(Clone, Debug)]
pub struct DuplicateError {
    kind: DuplicateKind,
    name: String,
    this_item: FileSpan,
    last_item: FileSpan,
}

impl Display for DuplicateError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "Duplicate {} `{}`", self.kind, self.name)
    }
}

impl DuplicateError {
    pub fn as_report(&self, level: ReportKind) -> ValkyrieReport {
        let mut report = Report::build(level, self.this_item.file, 0).with_code(self.kind as u32);
        report.set_message(self.to_string());
        report.add_label(
            self.this_item.as_label(format!("{:?} `{}` is defined here.", self.kind, self.name)).with_color(Color::Blue),
        );
        report.add_label(
            self.last_item
                .as_label(format!("But {} `{}` has been defined here.", self.kind, self.name))
                .with_color(Color::Cyan),
        );
        report.set_help(format!("Items must have unique names, rename one of the items to have a unique name"));
        report.finish()
    }
}

impl ValkyrieError {
    pub fn duplicate_type(name: String, this: FileSpan, last: FileSpan) -> Self {
        let this = DuplicateError { kind: DuplicateKind::Type, name, this_item: this, last_item: last };
        Self { kind: ValkyrieErrorKind::Duplicate(Box::new(this)), level: ReportKind::Error }
    }
    pub fn duplicate_function(name: String, this: FileSpan, last: FileSpan) -> Self {
        let this = DuplicateError { kind: DuplicateKind::Function, name, this_item: this, last_item: last };
        Self { kind: ValkyrieErrorKind::Duplicate(Box::new(this)), level: ReportKind::Error }
    }
    pub fn duplicate_variable(name: String, this: FileSpan, last: FileSpan) -> Self {
        let this = DuplicateError { kind: DuplicateKind::Variable, name, this_item: this, last_item: last };
        Self { kind: ValkyrieErrorKind::Duplicate(Box::new(this)), level: ReportKind::Error }
    }
}