From c8a634a67433b7382d3917f6634ebca4fa8c4be0 Mon Sep 17 00:00:00 2001 From: Ibiyemi Abiodun Date: Thu, 12 Dec 2024 14:39:11 -0500 Subject: [PATCH] feat: add support for deserializing strings as byte arrays --- ciborium/src/de/mod.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/ciborium/src/de/mod.rs b/ciborium/src/de/mod.rs index 18742e4..3bf7fba 100644 --- a/ciborium/src/de/mod.rs +++ b/ciborium/src/de/mod.rs @@ -392,7 +392,7 @@ where return match self.decoder.pull()? { Header::Tag(..) => continue, - Header::Bytes(Some(len)) if len <= self.scratch.len() => { + Header::Text(Some(len)) | Header::Bytes(Some(len)) if len <= self.scratch.len() => { self.decoder.read_exact(&mut self.scratch[..len])?; visitor.visit_bytes(&self.scratch[..len]) } @@ -428,6 +428,19 @@ where visitor.visit_byte_buf(buffer) } + Header::Text(len) => { + let mut buffer = String::new(); + + let mut segments = self.decoder.text(len); + while let Some(mut segment) = segments.pull()? { + while let Some(chunk) = segment.pull(self.scratch)? { + buffer.push_str(chunk); + } + } + + visitor.visit_byte_buf(buffer.into_bytes()) + } + Header::Array(len) => self.recurse(|me| { let access = Access(me, len); visitor.visit_seq(access)