cloud_sdk/transport/
workspace.rs1use core::fmt;
4
5use cloud_sdk_sanitization::sanitize_bytes;
6
7pub const RESPONSE_DECODER_SCRATCH_BYTES: usize = 256;
9pub const RESPONSE_CURSOR_SCRATCH_BYTES: usize = 1024;
11pub const RESPONSE_PROVIDER_LINK_SCRATCH_BYTES: usize = 2048;
13
14pub struct ResponseDecodeWorkspace {
26 decoder: [u8; RESPONSE_DECODER_SCRATCH_BYTES],
27 cursor: [u8; RESPONSE_CURSOR_SCRATCH_BYTES],
28 provider_link: [u8; RESPONSE_PROVIDER_LINK_SCRATCH_BYTES],
29}
30
31impl ResponseDecodeWorkspace {
32 pub(crate) const fn new() -> Self {
33 Self {
34 decoder: [0; RESPONSE_DECODER_SCRATCH_BYTES],
35 cursor: [0; RESPONSE_CURSOR_SCRATCH_BYTES],
36 provider_link: [0; RESPONSE_PROVIDER_LINK_SCRATCH_BYTES],
37 }
38 }
39
40 #[must_use]
42 pub const fn new_for_provider() -> Self {
43 Self::new()
44 }
45
46 pub fn decoder_scratch_mut(&mut self) -> &mut [u8] {
48 &mut self.decoder
49 }
50
51 pub fn cursor_scratch_mut(&mut self) -> &mut [u8] {
56 &mut self.cursor
57 }
58
59 pub fn provider_link_scratch_mut(&mut self) -> &mut [u8] {
64 &mut self.provider_link
65 }
66
67 fn clear(&mut self) {
68 sanitize_bytes(&mut self.decoder);
69 sanitize_bytes(&mut self.cursor);
70 sanitize_bytes(&mut self.provider_link);
71 }
72}
73
74impl Drop for ResponseDecodeWorkspace {
75 fn drop(&mut self) {
76 self.clear();
77 }
78}
79
80impl fmt::Debug for ResponseDecodeWorkspace {
81 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
82 formatter.write_str("ResponseDecodeWorkspace([redacted])")
83 }
84}
85
86#[cfg(test)]
87mod tests {
88 use super::ResponseDecodeWorkspace;
89
90 #[test]
91 fn complete_workspace_uses_one_clear_path() {
92 let mut workspace = ResponseDecodeWorkspace::new();
93 workspace.decoder.fill(0x11);
94 workspace.cursor.fill(0x22);
95 workspace.provider_link.fill(0x33);
96 workspace.clear();
97 assert!(workspace.decoder.iter().all(|byte| *byte == 0));
98 assert!(workspace.cursor.iter().all(|byte| *byte == 0));
99 assert!(workspace.provider_link.iter().all(|byte| *byte == 0));
100 }
101}