Skip to main content

regorus/utils/limits/
error.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4#![allow(dead_code)]
5
6use core::{fmt, time::Duration};
7
8/// Errors reported when execution time or memory ceilings are enforced.
9#[derive(Clone, Copy, PartialEq, Eq)]
10pub enum LimitError {
11    /// Reported when the execution timer observes elapsed time beyond the configured limit.
12    TimeLimitExceeded {
13        /// Elapsed work duration when the threshold was exceeded.
14        elapsed: Duration,
15        /// Configured time limit.
16        limit: Duration,
17    },
18    /// Reported when the memory tracker estimates usage beyond the configured limit.
19    MemoryLimitExceeded {
20        /// Estimated bytes in use when the limit was detected.
21        usage: u64,
22        /// Configured memory ceiling in bytes.
23        limit: u64,
24    },
25}
26
27impl fmt::Debug for LimitError {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        match self {
30            Self::TimeLimitExceeded { elapsed, limit } => f
31                .debug_struct("TimeLimitExceeded")
32                .field("elapsed", elapsed)
33                .field("limit", limit)
34                .finish(),
35            Self::MemoryLimitExceeded { usage, limit } => f
36                .debug_struct("MemoryLimitExceeded")
37                .field("usage", usage)
38                .field("limit", limit)
39                .finish(),
40        }
41    }
42}
43
44impl fmt::Display for LimitError {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        match self {
47            Self::TimeLimitExceeded { elapsed, limit } => {
48                let elapsed_ns = elapsed.as_nanos();
49                let limit_ns = limit.as_nanos();
50                write!(
51                    f,
52                    "execution exceeded time limit (elapsed={}ns, limit={}ns)",
53                    elapsed_ns, limit_ns
54                )
55            }
56            Self::MemoryLimitExceeded { usage, limit } => {
57                write!(
58                    f,
59                    "execution exceeded memory limit (usage={} bytes, limit={} bytes)",
60                    usage, limit
61                )
62            }
63        }
64    }
65}
66
67impl core::error::Error for LimitError {}