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