inspect_core/limit.rs
1//! Inspection limits and safety constraints.
2
3/// Limits to prevent pathological inspection behavior.
4///
5/// These limits protect against accidentally traversing enormous data
6/// structures, infinite loops, or excessive memory allocation during
7/// introspection.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub struct InspectLimits {
10 /// Maximum tree depth to traverse.
11 pub max_depth: usize,
12
13 /// Maximum number of items to inspect in a collection.
14 pub max_items: usize,
15
16 /// Maximum total number of nodes to visit.
17 pub max_nodes: usize,
18
19 /// Maximum length of a string to include.
20 pub max_string_length: usize,
21
22 /// Maximum number of bytes to include.
23 pub max_bytes: usize,
24}
25
26impl InspectLimits {
27 /// Create limits with all constraints set to maximum.
28 pub const fn unlimited() -> Self {
29 Self {
30 max_depth: usize::MAX,
31 max_items: usize::MAX,
32 max_nodes: usize::MAX,
33 max_string_length: usize::MAX,
34 max_bytes: usize::MAX,
35 }
36 }
37
38 /// Create limits suitable for interactive debugging.
39 pub const fn debug() -> Self {
40 Self {
41 max_depth: 32,
42 max_items: 100,
43 max_nodes: 10_000,
44 max_string_length: 1024,
45 max_bytes: 4096,
46 }
47 }
48
49 /// Create limits suitable for compact logging.
50 pub const fn compact() -> Self {
51 Self {
52 max_depth: 8,
53 max_items: 10,
54 max_nodes: 1000,
55 max_string_length: 128,
56 max_bytes: 256,
57 }
58 }
59}
60
61impl Default for InspectLimits {
62 fn default() -> Self {
63 Self::debug()
64 }
65}