[serde_bencode] Impl int

This commit is contained in:
Fabian 2024-07-31 22:01:18 +02:00
parent 7475974764
commit a731269a7d

View File

@ -1,6 +1,5 @@
use std::fmt;
use std::fmt::Display;
use std::ops::{AddAssign, MulAssign};
use serde::{de, Deserialize, ser};
use serde::de::Visitor;
@ -14,6 +13,7 @@ pub enum Error {
Eof,
Syntax,
ExpectedInteger,
ExpectedIntegerEnd,
ExpectedListEnd,
ExpectedDictEnd,
TrailingCharacters,
@ -38,6 +38,7 @@ impl Display for Error {
Error::Eof => formatter.write_str("unexpected end of input"),
Error::Syntax => formatter.write_str("syntax error"),
Error::ExpectedInteger => formatter.write_str("expected integer"),
Error::ExpectedIntegerEnd => formatter.write_str("expected integer end char 'e'"),
Error::ExpectedListEnd => formatter.write_str("expected list end char 'e'"),
Error::ExpectedDictEnd => formatter.write_str("expected dict end char 'e'"),
Error::TrailingCharacters => formatter.write_str("trailing characters")
@ -79,12 +80,25 @@ impl<'de> Deserializer<'de> {
}
}
fn next_byte(&mut self) -> Result<u8> {
let ch = self.peek_byte()?;
self.input = &self.input[1..];
Ok(ch)
}
fn parse_int<T>(&mut self) -> Result<T>
where
//T: AddAssign<T> + MulAssign<T> + From<i8>,
T: From<i8>,
T: From<i8> + std::str::FromStr,
{
Ok(42.into())
if self.next_byte()? != 'i' as u8 {
return Err(Error::Syntax)
}
let end_pos = self.input.iter().position(|&x| x == 'e' as u8)
.ok_or_else(|| Error::ExpectedIntegerEnd)?;
let int_str = std::str::from_utf8(&self.input[..end_pos]).map_err(|_| Error::Syntax)?;
let int = int_str.parse::<T>().map_err(|_| Error::ExpectedInteger)?;
self.input = &self.input[end_pos + 1..];
Ok(int)
}
}
@ -304,11 +318,13 @@ impl <'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
#[cfg(test)]
mod test {
use crate::serde_bencode::from_bytes;
use crate::serde_bencode::{Error, from_bytes};
#[test]
fn test_int() {
assert_eq!(from_bytes(&"i42e".as_bytes()), Ok(42));
assert_eq!(from_bytes::<i32>(&"i42".as_bytes()), Err(Error::ExpectedIntegerEnd));
assert_eq!(from_bytes::<i32>(&"42e".as_bytes()), Err(Error::Syntax));
// 2**16 + 1 = 65537
assert_eq!(from_bytes(&"i65537e".as_bytes()), Ok(65537));
}