Repair errors exposed by parse tests in rust

This commit is contained in:
Tony Garnock-Jones 2023-10-29 21:02:48 +01:00
parent 755a8bc73b
commit e8c0a2565e
1 changed files with 21 additions and 9 deletions

View File

@ -313,6 +313,18 @@ impl<'de, 'src, N: NestedValue, Dec: DomainParse<N::Embedded>, S: BinarySource<'
}
}
fn read_set(&mut self, read_annotations: bool) -> io::Result<N> {
let items = self.upto(b'}', read_annotations)?;
let mut s = Set::<N>::new();
for i in items {
if s.contains(&i) {
return Err(io_syntax_error("Duplicate set element"));
}
s.insert(i);
}
Ok(N::new(s))
}
fn read_dictionary(&mut self, read_annotations: bool) -> io::Result<N> {
let mut d = Map::new();
loop {
@ -326,6 +338,9 @@ impl<'de, 'src, N: NestedValue, Dec: DomainParse<N::Embedded>, S: BinarySource<'
if self.next_byte()? != b':' {
return Err(io_syntax_error("Missing expected key/value separator"));
}
if d.contains_key(&k) {
return Err(io_syntax_error("Duplicate key"));
}
let v = Reader::<N>::demand_next(self, read_annotations)?;
d.insert(k, v);
}
@ -392,13 +407,12 @@ impl<'de, 'src, N: NestedValue, Dec: DomainParse<N::Embedded>, S: BinarySource<'
for TextReader<'de, 'src, N, Dec, S>
{
fn next(&mut self, read_annotations: bool) -> io::Result<Option<N>> {
self.skip_whitespace();
let c = match self.peek() {
Ok(c) => c,
match self.peek() {
Err(e) if is_eof_io_error(&e) => return Ok(None),
Err(e) => return Err(e.into()),
};
Ok(Some(match c {
_ => (),
}
self.skip_whitespace();
Ok(Some(match self.peek()? {
b'"' => {
self.skip()?;
N::new(self.read_string(b'"')?)
@ -429,9 +443,7 @@ impl<'de, 'src, N: NestedValue, Dec: DomainParse<N::Embedded>, S: BinarySource<'
match self.next_byte()? {
b'f' => N::new(false),
b't' => N::new(true),
b'{' => N::new(Set::from_iter(
self.upto(b'}', read_annotations)?.into_iter(),
)),
b'{' => self.read_set(read_annotations)?,
b'"' => self.read_literal_binary()?,
b'x' => match self.next_byte()? {
b'"' => N::new(&self.read_hex_binary()?[..]),