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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//! Buffer edit decoders.
use super::{
DecodedCount, DecodedGuestString, DecodedTextRange, GuestDecodeError, GuestDecodeField,
GuestDecodeLimits,
};
use crate::buffer::BufferEdit;
/// Guest buffer edit after scalar, range, text, and payload-limit checks.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum DecodedBufferEdit {
/// Replace all buffer text.
SetAll {
/// Bounded replacement text.
text: DecodedGuestString,
},
/// Insert text at a byte boundary.
Insert {
/// Requested byte index.
byte_index: DecodedCount,
/// Bounded inserted text.
text: DecodedGuestString,
},
/// Replace an unchecked byte range.
Replace {
/// Requested byte range.
range: DecodedTextRange,
/// Bounded replacement text.
text: DecodedGuestString,
},
/// Delete an unchecked byte range.
Delete {
/// Requested byte range.
range: DecodedTextRange,
},
}
impl DecodedBufferEdit {
/// Decodes a whole-buffer replacement.
///
/// # Errors
///
/// Returns [`GuestDecodeError`] when the replacement text is oversized.
pub fn set_all(text: &str, limits: GuestDecodeLimits) -> Result<Self, GuestDecodeError> {
Ok(Self::SetAll {
text: DecodedGuestString::from_bytes(
GuestDecodeField::BufferEditText,
text.as_bytes(),
limits,
)?,
})
}
/// Decodes an insertion edit.
///
/// # Errors
///
/// Returns [`GuestDecodeError`] when the byte index or text exceeds runtime limits.
pub fn insert(
byte_index: u64,
text: &str,
limits: GuestDecodeLimits,
) -> Result<Self, GuestDecodeError> {
Ok(Self::Insert {
byte_index: DecodedCount::new(
GuestDecodeField::BufferEditByteIndex,
byte_index,
limits,
)?,
text: DecodedGuestString::from_bytes(
GuestDecodeField::BufferEditText,
text.as_bytes(),
limits,
)?,
})
}
/// Decodes a replacement edit.
///
/// # Errors
///
/// Returns [`GuestDecodeError`] when the range or text exceeds runtime limits.
pub fn replace(
start: u64,
end: u64,
text: &str,
limits: GuestDecodeLimits,
) -> Result<Self, GuestDecodeError> {
Ok(Self::Replace {
range: DecodedTextRange::new(GuestDecodeField::BufferEditRange, start, end, limits)?,
text: DecodedGuestString::from_bytes(
GuestDecodeField::BufferEditText,
text.as_bytes(),
limits,
)?,
})
}
/// Decodes a deletion edit.
///
/// # Errors
///
/// Returns [`GuestDecodeError`] when the range exceeds runtime limits.
pub fn delete(
start: u64,
end: u64,
limits: GuestDecodeLimits,
) -> Result<Self, GuestDecodeError> {
Ok(Self::Delete {
range: DecodedTextRange::new(GuestDecodeField::BufferEditRange, start, end, limits)?,
})
}
/// Converts the decoded request into an owner-validated buffer edit.
#[must_use]
pub fn into_buffer_edit(self) -> BufferEdit {
match self {
Self::SetAll { text } => BufferEdit::set_all(text.into_string()),
Self::Insert { byte_index, text } => {
BufferEdit::insert(byte_index.get(), text.into_string())
}
Self::Replace { range, text } => {
BufferEdit::replace_unchecked(range.get(), text.into_string())
}
Self::Delete { range } => BufferEdit::delete_unchecked(range.get()),
}
}
}