Skip to main content

yomi_core/registry/
error.rs

1//! Registry errors.
2
3use super::*;
4
5/// A registry error.
6#[non_exhaustive]
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum RegistryError {
9    /// A command (or variable) with the same name was registered twice.
10    DuplicateCommand(String),
11    /// Tried to bind a command that is not registered.
12    UnknownCommand(String),
13    /// The types of the command to bind do not match the parameter shape of the sugar.
14    SugarMismatch(String),
15    /// The same kind of sugar was bound twice.
16    AlreadyBound(Sugar),
17    /// The name is invalid (not an identifier, or the first half of the package name differs from `Package::NAME`).
18    InvalidName(String),
19    /// A package with the same name was registered twice.
20    DuplicatePackage(String),
21    /// A package declared in `DEPENDS` is neither registered nor in the same bulk registration.
22    MissingDependency {
23        /// The package being registered.
24        package: String,
25        /// The unregistered dependency.
26        dependency: String,
27    },
28    /// Package dependencies form a cycle (including a dependency on itself).
29    DependencyCycle(String),
30    /// The return type of a query is `()` (a query that returns no value cannot be made).
31    QueryWithoutValue(String),
32    /// The type of a variable cannot be used (it contains `()`).
33    UnsupportedVarType(String),
34    /// The initial value of a variable is `NaN` or `±∞` (which never appear as script values), or does not match the type of
35    /// the variable.
36    InvalidVarValue(String),
37    /// A type with the same name was registered twice.
38    DuplicateType(String),
39    /// A type used by a type, command or variable is not registered yet (types are registered before they are used).
40    UnknownType {
41        /// The name of the unregistered type.
42        name: String,
43        /// The name of the type, command or variable that uses that type.
44        used_by: String,
45    },
46    /// A type contains itself (even directly).
47    RecursiveType(String),
48    /// Uses a type that cannot be a `map` key (`float`, `list`, `map`, `Option`, and so on). The argument is the name of the
49    /// user.
50    InvalidMapKey(String),
51    /// The nesting of types is too deep (`MAX_TYPE_DEPTH`). The argument is the name of the user.
52    TypeTooDeep(String),
53    /// A type declaration is invalid (names of fields or variants are not identifiers or collide, there are no variants, or a
54    /// field is `()`).
55    InvalidTypeDecl(String),
56    /// There is no command to attach an alias, description or parameter names to (no command was registered just before).
57    NoCommand,
58    /// The number of parameter names differs from the number of parameters of the command. Or a default has no name, has the
59    /// wrong type, or parameters with defaults are not at the end.
60    InvalidParams(String),
61}
62
63impl std::fmt::Display for RegistryError {
64    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
65        match self {
66            RegistryError::DuplicateCommand(n) => write!(f, "duplicate command: {n}"),
67            RegistryError::UnknownCommand(n) => write!(f, "unknown command: {n}"),
68            RegistryError::SugarMismatch(n) => write!(f, "type mismatch for sugar binding: {n}"),
69            RegistryError::AlreadyBound(s) => write!(f, "sugar already bound: {}", s.name()),
70            RegistryError::InvalidName(n) => write!(f, "invalid name: {n}"),
71            RegistryError::DuplicatePackage(n) => write!(f, "duplicate package: {n}"),
72            RegistryError::MissingDependency {
73                package,
74                dependency,
75            } => write!(
76                f,
77                "package {package} depends on {dependency}, which is not registered"
78            ),
79            RegistryError::DependencyCycle(n) => write!(f, "package {n} depends on itself"),
80            RegistryError::QueryWithoutValue(n) => write!(f, "query returns no value: {n}"),
81            RegistryError::UnsupportedVarType(n) => write!(f, "unsupported variable type: {n}"),
82            RegistryError::InvalidVarValue(n) => write!(f, "invalid initial value: {n}"),
83            RegistryError::DuplicateType(n) => write!(f, "duplicate type: {n}"),
84            RegistryError::UnknownType { name, used_by } => {
85                write!(f, "type {name} (used by {used_by}) is not registered")
86            }
87            RegistryError::RecursiveType(n) => write!(f, "type {n} contains itself"),
88            RegistryError::InvalidMapKey(n) => write!(f, "invalid map key type in {n}"),
89            RegistryError::TypeTooDeep(n) => write!(f, "type nesting too deep in {n}"),
90            RegistryError::InvalidTypeDecl(n) => write!(f, "invalid type declaration: {n}"),
91            RegistryError::NoCommand => write!(f, "no command to attach the item to"),
92            RegistryError::InvalidParams(n) => {
93                write!(f, "invalid parameter names or defaults: {n}")
94            }
95        }
96    }
97}
98
99impl std::error::Error for RegistryError {}