use super::write::Write;
use crate::Error;
#[derive(Debug, Clone)]
pub(crate) struct Cursor<'a> {
remaining: &'a [u8],
}
impl<'a> Cursor<'a> {
pub(crate) fn new(remaining: &'a [u8]) -> Self {
Self { remaining }
}
pub(crate) fn remaining(&self) -> &'a [u8] {
self.remaining
}
pub(crate) fn next(&mut self) -> Option<u8> {
let (&first, tail) = self.remaining.split_first()?;
self.remaining = tail;
Some(first)
}
pub(crate) fn read_optional_tag(&mut self, tag: &[u8]) -> bool {
if self.remaining.starts_with(tag) {
self.read_exact(tag.len());
true
} else {
false
}
}
pub(crate) fn read_while<F: Fn(&u8) -> bool>(&mut self, f: F) -> &'a [u8] {
match self.remaining.iter().position(|x| !f(x)) {
None => self.read_exact(self.remaining.len()),
Some(position) => self.read_exact(position),
}
}
pub(crate) fn read_until<F: Fn(&u8) -> bool>(&mut self, f: F) -> &'a [u8] {
match self.remaining.iter().position(f) {
None => self.read_exact(self.remaining.len()),
Some(position) => self.read_exact(position),
}
}
fn read_exact(&mut self, count: usize) -> &'a [u8] {
let (result, remaining) = self.remaining.split_at(count);
self.remaining = remaining;
result
}
}
pub(crate) struct SizeLimiter<'a> {
inner: &'a mut dyn Write,
size_limit: usize,
count: usize,
}
impl<'a> SizeLimiter<'a> {
pub(crate) fn new(inner: &'a mut dyn Write, size_limit: usize) -> Self {
Self {
inner,
size_limit,
count: 0,
}
}
}
impl<'a> Write for SizeLimiter<'a> {
fn write(&mut self, buf: &[u8]) -> Result<usize, Error> {
if self.count + buf.len() > self.size_limit {
return Err(Error::FormattedStringTooLarge);
}
let written = self.inner.write(buf)?;
self.count += written;
Ok(written)
}
}
#[cfg(test)]
mod tests {
#[cfg(feature = "alloc")]
#[test]
fn test_cursor_debug_is_non_empty() {
use alloc::format;
use super::Cursor;
assert!(!format!("{:?}", Cursor::new(&[])).is_empty());
}
}