soroban_wasmi/table/
error.rs

1use super::TableType;
2use crate::core::ValType;
3use core::{fmt, fmt::Display};
4
5/// Errors that may occur upon operating with table entities.
6#[derive(Debug)]
7#[non_exhaustive]
8pub enum TableError {
9    /// Occurs when growing a table out of its set bounds.
10    GrowOutOfBounds {
11        /// The maximum allowed table size.
12        maximum: u32,
13        /// The current table size before the growth operation.
14        current: u32,
15        /// The amount of requested invalid growth.
16        delta: u32,
17    },
18    /// Occurs when operating with a [`Table`](crate::Table) and mismatching element types.
19    ElementTypeMismatch {
20        /// Expected element type for the [`Table`](crate::Table).
21        expected: ValType,
22        /// Encountered element type.
23        actual: ValType,
24    },
25    /// Occurs when accessing the table out of bounds.
26    AccessOutOfBounds {
27        /// The current size of the table.
28        current: u32,
29        /// The accessed index that is out of bounds.
30        offset: u32,
31    },
32    /// Occur when coping elements of tables out of bounds.
33    CopyOutOfBounds,
34    /// Occurs when `ty` is not a subtype of `other`.
35    InvalidSubtype {
36        /// The [`TableType`] which is not a subtype of `other`.
37        ty: TableType,
38        /// The [`TableType`] which is supposed to be a supertype of `ty`.
39        other: TableType,
40    },
41    TooManyTables,
42}
43
44#[cfg(feature = "std")]
45impl std::error::Error for TableError {}
46
47impl Display for TableError {
48    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49        match self {
50            Self::GrowOutOfBounds {
51                maximum,
52                current,
53                delta,
54            } => {
55                write!(
56                    f,
57                    "tried to grow table with size of {current} and maximum of \
58                                    {maximum} by {delta} out of bounds",
59                )
60            }
61            Self::ElementTypeMismatch { expected, actual } => {
62                write!(f, "encountered mismatching table element type, expected {expected:?} but found {actual:?}")
63            }
64            Self::AccessOutOfBounds { current, offset } => {
65                write!(
66                    f,
67                    "out of bounds access of table element {offset} \
68                    of table with size {current}",
69                )
70            }
71            Self::CopyOutOfBounds => {
72                write!(f, "out of bounds access of table elements while copying")
73            }
74            Self::InvalidSubtype { ty, other } => {
75                write!(f, "table type {ty:?} is not a subtype of {other:?}",)
76            }
77            Self::TooManyTables => {
78                write!(f, "too many tables")
79            }
80        }
81    }
82}