1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use core::fmt;
/// Which configured parse, load or edit limit was exceeded.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum LimitKind {
/// The source contains more bytes than the configured cap.
InputBytes,
/// Parsing reached more items than the configured cap.
Items,
/// Parsing produced more diagnostics than the configured cap.
Diagnostics,
/// The source or planned output contains more physical lines than the configured cap.
Lines,
/// The planned edit output contains more bytes than the configured cap.
OutputBytes,
}
impl LimitKind {
/// Returns the stable lowercase name of the limit kind.
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::InputBytes => "input-bytes",
Self::Items => "items",
Self::Diagnostics => "diagnostics",
Self::Lines => "lines",
Self::OutputBytes => "output-bytes",
}
}
}
impl fmt::Display for LimitKind {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
/// A configured limit and the value that exceeded it.
///
/// The values are sizes and counts, never document contents.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct LimitExceeded {
kind: LimitKind,
limit: usize,
observed: usize,
}
impl LimitExceeded {
pub(crate) const fn new(kind: LimitKind, limit: usize, observed: usize) -> Self {
Self {
kind,
limit,
observed,
}
}
/// Returns which limit was exceeded.
#[must_use]
pub const fn kind(self) -> LimitKind {
self.kind
}
/// Returns the configured maximum.
#[must_use]
pub const fn limit(self) -> usize {
self.limit
}
/// Returns the size or count that was observed.
#[must_use]
pub const fn observed(self) -> usize {
self.observed
}
}
impl fmt::Display for LimitExceeded {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
formatter,
"{} limit {} exceeded with observed {}",
self.kind, self.limit, self.observed
)
}
}