dpscript/dpscript/validator/checker/
enums.rs

1use super::Checker;
2use crate::{CheckerContext, DuoValidatorError, Enum, Result, ValidatorError};
3
4impl Checker<Enum> for Enum {
5    fn check(item: &mut Enum, cx: &mut CheckerContext) -> Result<()> {
6        let module = cx.current_module();
7        let enums = module.enums();
8        let imports = module.imported_objects()?;
9        let mut occurences = 0;
10
11        for it in enums {
12            if it.name.0 == item.name.0 {
13                occurences += 1;
14
15                if occurences > 1 {
16                    return Err(DuoValidatorError {
17                        src: module.source.clone(),
18                        at: item.name.1,
19                        other: it.name.1,
20                        err: format!("Duplicate identifier: {}", item.name.0),
21                    }
22                    .into());
23                }
24            }
25        }
26
27        for it in imports {
28            if it.export.name() == item.name.0 {
29                occurences += 1;
30
31                if occurences > 1 {
32                    return Err(DuoValidatorError {
33                        src: module.source.clone(),
34                        at: item.name.1,
35                        other: it.export.span(),
36                        err: format!("Duplicate identifier: {}", item.name.0),
37                    }
38                    .into());
39                }
40            }
41        }
42
43        let mut items = Vec::new();
44
45        for (entry, span) in &item.entries {
46            if items.contains(entry) {
47                return Err(ValidatorError {
48                    src: module.source.clone(),
49                    at: span.clone(),
50                    err: format!("Enum entries must be unique!"),
51                }
52                .into());
53            } else {
54                items.push(entry.clone());
55            }
56        }
57
58        Ok(())
59    }
60}