From 8fb6eda14493703abeb2a45e8ea918e30c01ca3f Mon Sep 17 00:00:00 2001 From: Faerbit Date: Wed, 31 Jul 2024 22:01:18 +0200 Subject: [PATCH] [serde_bencode] Impl int --- src/serde_bencode.rs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/serde_bencode.rs b/src/serde_bencode.rs index 4122f1c..c17eac7 100644 --- a/src/serde_bencode.rs +++ b/src/serde_bencode.rs @@ -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 { + let ch = self.peek_byte()?; + self.input = &self.input[1..]; + Ok(ch) + } + fn parse_int(&mut self) -> Result where - //T: AddAssign + MulAssign + From, - T: From, + T: 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::().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::(&"i42".as_bytes()), Err(Error::ExpectedIntegerEnd)); + assert_eq!(from_bytes::(&"42e".as_bytes()), Err(Error::Syntax)); // 2**16 + 1 = 65537 assert_eq!(from_bytes(&"i65537e".as_bytes()), Ok(65537)); }