Skip to main content

zrx_module/module/
error.rs

1// Copyright (c) 2025-2026 Zensical and contributors
2
3// SPDX-License-Identifier: MIT
4// Third-party contributions licensed under DCO
5
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to
8// deal in the Software without restriction, including without limitation the
9// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10// sell copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12
13// The above copyright notice and this permission notice shall be included in
14// all copies or substantial portions of the Software.
15
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22// IN THE SOFTWARE.
23
24// ----------------------------------------------------------------------------
25
26//! Module error.
27
28use std::{error, result};
29use thiserror::Error;
30
31use zrx_id as id;
32use zrx_id::expression;
33
34mod convert;
35
36pub use convert::IntoError;
37
38// ----------------------------------------------------------------------------
39// Enums
40// ----------------------------------------------------------------------------
41
42/// Module error.
43///
44/// Modules are the fundamental building blocks of the system, as they allow to
45/// register functionality with the system as part of the implementation of the
46/// [`Module`][] trait. As such, [`Module::setup`][] must be allowed to return
47/// any kind of error, which is then converted into a module error.
48///
49/// The enum is deliberately not marked as non-exhaustive, because this type is
50/// intended to be the sink for all errors that typically occur in modules, and
51/// should never be used in external code. As such, we're allowed to freely add
52/// new variants, moving errors out of [`Error::Other`] when necessary.
53///
54/// In order to integrate with error types that are not already covered by the
55/// existing variants, the [`IntoError`] conversion trait is provided. Please
56/// note that this trait should only be used sparingly, and could denote a code
57/// smell, since [`Module::setup`][] implementations should rarely be fallible
58/// besides identifier and expression parsing errors, especially not involving
59/// side effects like file system or network access. Implementations should only
60/// fail due to programmer or configuration errors, which should be covered by
61/// the existing variants. In case you think an essential variant is missing,
62/// please report it on our issue tracker.
63///
64/// [`Module`]: crate::module::Module
65/// [`Module::setup`]: crate::module::Module::setup
66#[derive(Debug, Error)]
67pub enum Error {
68    /// Identifier error.
69    #[error(transparent)]
70    Id(#[from] id::Error),
71    /// Expression error.
72    #[error(transparent)]
73    Expression(#[from] expression::Error),
74    /// Other error.
75    #[error(transparent)]
76    Other(#[from] Box<dyn error::Error + Send>),
77}
78
79// ----------------------------------------------------------------------------
80// Type aliases
81// ----------------------------------------------------------------------------
82
83/// Module result.
84pub type Result<T = ()> = result::Result<T, Error>;