[bencode] Change signatures some more

This commit is contained in:
Fabian 2024-07-24 23:29:41 +02:00
parent 854858c854
commit 1129348248

View File

@ -73,14 +73,14 @@ impl Display for Bencode {
impl Bencode { impl Bencode {
pub fn encode(&self) -> Vec<u8> { pub fn compose(&self) -> Vec<u8> {
match self { match self {
Bencode::Integer(i) => {format!("i{}e", i).as_bytes().to_vec()} Bencode::Integer(i) => {format!("i{}e", i).as_bytes().to_vec()}
Bencode::Bytes(b) => {[format!("{}:", b.0.len()).as_bytes(), &b.0[..]].concat()} Bencode::Bytes(b) => {[format!("{}:", b.0.len()).as_bytes(), &b.0[..]].concat()}
Bencode::List(l) => { Bencode::List(l) => {
let mut result = vec!['l' as u8]; let mut result = vec!['l' as u8];
for item in l { for item in l {
result.extend(item.encode()) result.extend(item.compose())
} }
result.push('e' as u8); result.push('e' as u8);
result result
@ -88,16 +88,16 @@ impl Bencode {
Bencode::Dict(d) => { Bencode::Dict(d) => {
let mut result = vec!['d' as u8]; let mut result = vec!['d' as u8];
for (k, v) in d { for (k, v) in d {
result.extend(Self::encode(&Bencode::Bytes(k.clone()))); result.extend(Self::compose(&Bencode::Bytes(k.clone())));
result.extend(v.encode()); result.extend(v.compose());
} }
result.push('e' as u8); result.push('e' as u8);
result result
} }
} }
} }
pub fn decode(input: &[u8]) -> Result<Bencode> { pub fn parse(input: &[u8]) -> Result<Bencode> {
let (result, end_pos) = Self::decode_type(input)?; let (result, end_pos) = Self::parse_type(input)?;
ensure!(end_pos == input.len() - 1, ensure!(end_pos == input.len() - 1,
"Could not fully decode input. Got {} chars left to decode", input.len() - 1 - end_pos); "Could not fully decode input. Got {} chars left to decode", input.len() - 1 - end_pos);
Ok(result) Ok(result)
@ -106,12 +106,12 @@ impl Bencode {
pub fn sha1(&self) -> [u8; 20] { pub fn sha1(&self) -> [u8; 20] {
let mut hasher = Sha1::new(); let mut hasher = Sha1::new();
hasher.update(self.encode()); hasher.update(self.compose());
hasher.finalize().into() hasher.finalize().into()
} }
fn decode_type(input: &[u8]) -> Result<(Bencode, usize)>{ fn parse_type(input: &[u8]) -> Result<(Bencode, usize)>{
if input.len() == 0 { if input.len() == 0 {
return Err(anyhow!("Empty string is not valid bencode")) return Err(anyhow!("Empty string is not valid bencode"))
} }
@ -140,7 +140,7 @@ impl Bencode {
let (to_decode, end_decoded) = match type_ { let (to_decode, end_decoded) = match type_ {
BencodeType::Integer => (&input[1..end_to_decode], end_to_decode), BencodeType::Integer => (&input[1..end_to_decode], end_to_decode),
BencodeType::Bytes => { BencodeType::Bytes => {
let bytes_len = Self::decode_int_only(&input[0..end_to_decode])? as usize; let bytes_len = Self::parse_int_only(&input[0..end_to_decode])? as usize;
let bytes_start= end_to_decode + 1; let bytes_start= end_to_decode + 1;
let end_pos = bytes_start + bytes_len; let end_pos = bytes_start + bytes_len;
(&input[bytes_start..end_pos], end_pos - 1) (&input[bytes_start..end_pos], end_pos - 1)
@ -150,39 +150,39 @@ impl Bencode {
match type_ { match type_ {
BencodeType::Integer => { BencodeType::Integer => {
let result = Self::decode_int(to_decode)?; let result = Self::parse_int(to_decode)?;
Ok((result, end_decoded)) Ok((result, end_decoded))
}, },
BencodeType::Bytes => { BencodeType::Bytes => {
let result = Self::decode_bytes(to_decode)?; let result = Self::parse_bytes(to_decode)?;
Ok((result, end_decoded)) Ok((result, end_decoded))
}, },
BencodeType::List => { BencodeType::List => {
let (result, end_pos) = Self::decode_list(to_decode)?; let (result, end_pos) = Self::parse_list(to_decode)?;
Ok((result, end_pos + 1)) Ok((result, end_pos + 1))
}, },
BencodeType::Dict => { BencodeType::Dict => {
let (result, end_pos) = Self::decode_dict(to_decode)?; let (result, end_pos) = Self::parse_dict(to_decode)?;
Ok((result, end_pos + 1)) Ok((result, end_pos + 1))
}, },
} }
} }
fn decode_int_only(input: &[u8]) -> Result<i64> { fn parse_int_only(input: &[u8]) -> Result<i64> {
let int_str = std::str::from_utf8(input)?; let int_str = std::str::from_utf8(input)?;
int_str.parse::<i64>().map_err(anyhow::Error::msg) int_str.parse::<i64>().map_err(anyhow::Error::msg)
} }
fn decode_int(input: &[u8]) -> Result<Bencode> { fn parse_int(input: &[u8]) -> Result<Bencode> {
let int = Self::decode_int_only(input)?; let int = Self::parse_int_only(input)?;
Ok(Bencode::Integer(int)) Ok(Bencode::Integer(int))
} }
fn decode_bytes(input: &[u8]) -> Result<Bencode> { fn parse_bytes(input: &[u8]) -> Result<Bencode> {
Ok(Bencode::Bytes(ByteString::from_slice(input))) Ok(Bencode::Bytes(ByteString::from_slice(input)))
} }
fn decode_list(input: &[u8]) -> Result<(Bencode, usize)> { fn parse_list(input: &[u8]) -> Result<(Bencode, usize)> {
let mut result = Vec::new(); let mut result = Vec::new();
let mut decoded_pos = 0; let mut decoded_pos = 0;
loop { loop {
@ -192,14 +192,14 @@ impl Bencode {
if input[decoded_pos] == 'e' as u8 { if input[decoded_pos] == 'e' as u8 {
break; break;
} }
let (li_result, end_pos ) = Self::decode_type(&input[decoded_pos..])?; let (li_result, end_pos ) = Self::parse_type(&input[decoded_pos..])?;
result.push(li_result); result.push(li_result);
decoded_pos += end_pos + 1; decoded_pos += end_pos + 1;
} }
Ok((Bencode::List(result), decoded_pos)) Ok((Bencode::List(result), decoded_pos))
} }
fn decode_dict(input: &[u8]) -> Result<(Bencode, usize)> { fn parse_dict(input: &[u8]) -> Result<(Bencode, usize)> {
let mut result = HashMap::new(); let mut result = HashMap::new();
let mut decoded_pos = 0; let mut decoded_pos = 0;
loop { loop {
@ -209,11 +209,11 @@ impl Bencode {
if input[decoded_pos] == 'e' as u8 { if input[decoded_pos] == 'e' as u8 {
break; break;
} }
let (Bencode::Bytes(key_result), end_pos) = Self::decode_type(&input[decoded_pos..])? else { let (Bencode::Bytes(key_result), end_pos) = Self::parse_type(&input[decoded_pos..])? else {
return Err(anyhow!("Type of dictionary key not Bytes")) return Err(anyhow!("Type of dictionary key not Bytes"))
}; };
decoded_pos += end_pos + 1; decoded_pos += end_pos + 1;
let (value_result, end_pos) = Self::decode_type(&input[decoded_pos..])?; let (value_result, end_pos) = Self::parse_type(&input[decoded_pos..])?;
result.insert(key_result, value_result); result.insert(key_result, value_result);
decoded_pos += end_pos + 1; decoded_pos += end_pos + 1;
} }
@ -233,75 +233,75 @@ mod tests {
} }
#[test] #[test]
fn test_encode_int() { fn test_compose_int() {
assert_eq!(Bencode::Integer(42).encode(), "i42e".as_bytes()); assert_eq!(Bencode::Integer(42).compose(), "i42e".as_bytes());
assert_eq!(Bencode::Integer(17).encode(), "i17e".as_bytes()); assert_eq!(Bencode::Integer(17).compose(), "i17e".as_bytes());
} }
#[test] #[test]
fn test_encode_bytes() { fn test_compose_bytes() {
assert_eq!(Bencode::Bytes(ByteString::from_str("foo")).encode(), "3:foo".as_bytes()); assert_eq!(Bencode::Bytes(ByteString::from_str("foo")).compose(), "3:foo".as_bytes());
assert_eq!(Bencode::Bytes(ByteString::from_str("bar")).encode(), "3:bar".as_bytes()); assert_eq!(Bencode::Bytes(ByteString::from_str("bar")).compose(), "3:bar".as_bytes());
} }
#[test] #[test]
fn test_encode_list() { fn test_compose_list() {
assert_eq!(Bencode::List(vec![Bencode::Bytes(ByteString::from_str("foo")), Bencode::Integer(42)]).encode(), "l3:fooi42ee".as_bytes()); assert_eq!(Bencode::List(vec![Bencode::Bytes(ByteString::from_str("foo")), Bencode::Integer(42)]).compose(), "l3:fooi42ee".as_bytes());
} }
#[test] #[test]
fn test_encode_dict() { fn test_compose_dict() {
assert_eq!(Bencode::Dict(HashMap::from([(ByteString::from_str("foo"), Bencode::Integer(42))])).encode(), "d3:fooi42ee".as_bytes()); assert_eq!(Bencode::Dict(HashMap::from([(ByteString::from_str("foo"), Bencode::Integer(42))])).compose(), "d3:fooi42ee".as_bytes());
} }
#[test] #[test]
fn test_integer_only() { fn test_integer_only() {
assert_eq!(Bencode::decode_int_only("42".as_bytes()).unwrap(), 42); assert_eq!(Bencode::parse_int_only("42".as_bytes()).unwrap(), 42);
assert_eq!(Bencode::decode_int_only("17".as_bytes()).unwrap(), 17); assert_eq!(Bencode::parse_int_only("17".as_bytes()).unwrap(), 17);
assert_eq!(Bencode::decode_int_only("-17".as_bytes()).unwrap(), -17); assert_eq!(Bencode::parse_int_only("-17".as_bytes()).unwrap(), -17);
} }
#[test] #[test]
fn test_integer() { fn test_integer() {
assert_eq!(Bencode::decode_int("42".as_bytes()).unwrap(), Bencode::Integer(42)); assert_eq!(Bencode::parse_int("42".as_bytes()).unwrap(), Bencode::Integer(42));
assert_eq!(Bencode::decode_int("17".as_bytes()).unwrap(), Bencode::Integer(17)); assert_eq!(Bencode::parse_int("17".as_bytes()).unwrap(), Bencode::Integer(17));
assert_eq!(Bencode::decode_int("-17".as_bytes()).unwrap(), Bencode::Integer(-17)); assert_eq!(Bencode::parse_int("-17".as_bytes()).unwrap(), Bencode::Integer(-17));
} }
#[test] #[test]
fn test_integer_str() { fn test_integer_str() {
assert_eq!(Bencode::decode("i42e".as_bytes()).unwrap(), Bencode::Integer(42)); assert_eq!(Bencode::parse("i42e".as_bytes()).unwrap(), Bencode::Integer(42));
assert_eq!(Bencode::decode("i17e".as_bytes()).unwrap(), Bencode::Integer(17)); assert_eq!(Bencode::parse("i17e".as_bytes()).unwrap(), Bencode::Integer(17));
assert_eq!(Bencode::decode("i-17e".as_bytes()).unwrap(), Bencode::Integer(-17)); assert_eq!(Bencode::parse("i-17e".as_bytes()).unwrap(), Bencode::Integer(-17));
} }
#[test] #[test]
fn test_bytes() { fn test_bytes() {
assert_eq!(Bencode::decode_bytes("hallo".as_bytes()).unwrap(), Bencode::Bytes(ByteString::from_str("hallo"))); assert_eq!(Bencode::parse_bytes("hallo".as_bytes()).unwrap(), Bencode::Bytes(ByteString::from_str("hallo")));
assert_eq!(Bencode::decode_bytes("tschüss".as_bytes()).unwrap(), Bencode::Bytes(ByteString::from_str("tschüss"))); assert_eq!(Bencode::parse_bytes("tschüss".as_bytes()).unwrap(), Bencode::Bytes(ByteString::from_str("tschüss")));
assert_eq!(Bencode::decode_bytes("💩".as_bytes()).unwrap(), Bencode::Bytes(ByteString::from_str("💩"))); assert_eq!(Bencode::parse_bytes("💩".as_bytes()).unwrap(), Bencode::Bytes(ByteString::from_str("💩")));
} }
fn util_encode_bytes(input: &str) -> String { fn util_compose_bytes(input: &str) -> String {
format!("{}:{input}", input.len()) format!("{}:{input}", input.len())
} }
#[test] #[test]
fn test_bytes_str() { fn test_bytes_str() {
assert_eq!(Bencode::decode(util_encode_bytes("hallo").as_bytes()).unwrap(), Bencode::Bytes(ByteString::from_str("hallo"))); assert_eq!(Bencode::parse(util_compose_bytes("hallo").as_bytes()).unwrap(), Bencode::Bytes(ByteString::from_str("hallo")));
assert_eq!(Bencode::decode(util_encode_bytes("tschüss").as_bytes()).unwrap(), Bencode::Bytes(ByteString::from_str("tschüss"))); assert_eq!(Bencode::parse(util_compose_bytes("tschüss").as_bytes()).unwrap(), Bencode::Bytes(ByteString::from_str("tschüss")));
assert_eq!(Bencode::decode(util_encode_bytes("💩").as_bytes()).unwrap(), Bencode::Bytes(ByteString::from_str("💩"))); assert_eq!(Bencode::parse(util_compose_bytes("💩").as_bytes()).unwrap(), Bencode::Bytes(ByteString::from_str("💩")));
assert_eq!(Bencode::decode(util_encode_bytes("hallo 💩, this is a long text").as_bytes()).unwrap(), Bencode::Bytes(ByteString::from_str("hallo 💩, this is a long text"))); assert_eq!(Bencode::parse(util_compose_bytes("hallo 💩, this is a long text").as_bytes()).unwrap(), Bencode::Bytes(ByteString::from_str("hallo 💩, this is a long text")));
} }
#[test] #[test]
fn test_list() { fn test_list() {
assert_eq!(Bencode::decode_list( assert_eq!(Bencode::parse_list(
("e").as_bytes()).unwrap(), ("e").as_bytes()).unwrap(),
(Bencode::List(Vec::new()), 0) (Bencode::List(Vec::new()), 0)
); );
let str = "i42ee"; let str = "i42ee";
assert_eq!(Bencode::decode_list( assert_eq!(Bencode::parse_list(
str.as_bytes()).unwrap(), str.as_bytes()).unwrap(),
( (
Bencode::List(vec![ Bencode::List(vec![
@ -310,8 +310,8 @@ mod tests {
str.len() - 1, str.len() - 1,
) )
); );
let str = format!("{}e", util_encode_bytes("hallo")); let str = format!("{}e", util_compose_bytes("hallo"));
assert_eq!(Bencode::decode_list( assert_eq!(Bencode::parse_list(
str.as_bytes()).unwrap(), str.as_bytes()).unwrap(),
( (
Bencode::List(vec![ Bencode::List(vec![
@ -320,8 +320,8 @@ mod tests {
str.len() - 1, str.len() - 1,
) )
); );
let str = format!("{}{}e", util_encode_bytes("hallo"), "i42e"); let str = format!("{}{}e", util_compose_bytes("hallo"), "i42e");
assert_eq!(Bencode::decode_list( assert_eq!(Bencode::parse_list(
str.as_bytes()).unwrap(), str.as_bytes()).unwrap(),
( (
Bencode::List(vec![ Bencode::List(vec![
@ -331,8 +331,8 @@ mod tests {
str.len() - 1, str.len() - 1,
) )
); );
let str = format!("{}{}{}{}e", "i-17e", util_encode_bytes("hallo"), "i42e", util_encode_bytes("tschüssi💩")); let str = format!("{}{}{}{}e", "i-17e", util_compose_bytes("hallo"), "i42e", util_compose_bytes("tschüssi💩"));
assert_eq!(Bencode::decode_list( assert_eq!(Bencode::parse_list(
str.as_bytes()).unwrap(), str.as_bytes()).unwrap(),
( (
Bencode::List(vec![ Bencode::List(vec![
@ -348,7 +348,7 @@ mod tests {
#[test] #[test]
fn test_multi_list() { fn test_multi_list() {
assert_eq!(Bencode::decode_list( assert_eq!(Bencode::parse_list(
("lelee").as_bytes()).unwrap(), ("lelee").as_bytes()).unwrap(),
(Bencode::List(vec![ (Bencode::List(vec![
Bencode::List(Vec::new()), Bencode::List(Vec::new()),
@ -356,8 +356,8 @@ mod tests {
]), ]),
4) 4)
); );
let str = format!("l{}{}ee", util_encode_bytes("hallo"), "i42e"); let str = format!("l{}{}ee", util_compose_bytes("hallo"), "i42e");
assert_eq!(Bencode::decode_list( assert_eq!(Bencode::parse_list(
str.as_bytes()).unwrap(), str.as_bytes()).unwrap(),
(Bencode::List(vec![ (Bencode::List(vec![
Bencode::List(vec![ Bencode::List(vec![
@ -367,8 +367,8 @@ mod tests {
]), ]),
str.len() - 1) str.len() - 1)
); );
let str = format!("l{}{}el{}{}{}ee", util_encode_bytes("hallo"), "i42e", "i17e", util_encode_bytes("tschüss💩"), "i33e"); let str = format!("l{}{}el{}{}{}ee", util_compose_bytes("hallo"), "i42e", "i17e", util_compose_bytes("tschüss💩"), "i33e");
assert_eq!(Bencode::decode_list( assert_eq!(Bencode::parse_list(
str.as_bytes()).unwrap(), str.as_bytes()).unwrap(),
(Bencode::List(vec![ (Bencode::List(vec![
Bencode::List(vec![ Bencode::List(vec![
@ -387,31 +387,31 @@ mod tests {
#[test] #[test]
fn test_list_str() { fn test_list_str() {
assert_eq!(Bencode::decode( assert_eq!(Bencode::parse(
"le".as_bytes()).unwrap(), "le".as_bytes()).unwrap(),
Bencode::List(Vec::new()) Bencode::List(Vec::new())
); );
assert_eq!(Bencode::decode( assert_eq!(Bencode::parse(
"li42ee".as_bytes()).unwrap(), "li42ee".as_bytes()).unwrap(),
Bencode::List(vec![ Bencode::List(vec![
Bencode::Integer(42), Bencode::Integer(42),
]) ])
); );
assert_eq!(Bencode::decode( assert_eq!(Bencode::parse(
(format!("l{}e", util_encode_bytes("hallo"))).as_bytes()).unwrap(), (format!("l{}e", util_compose_bytes("hallo"))).as_bytes()).unwrap(),
Bencode::List(vec![ Bencode::List(vec![
Bencode::Bytes(ByteString::from_str("hallo")), Bencode::Bytes(ByteString::from_str("hallo")),
]) ])
); );
assert_eq!(Bencode::decode( assert_eq!(Bencode::parse(
(format!("l{}{}e", util_encode_bytes("hallo"), "i42e")).as_bytes()).unwrap(), (format!("l{}{}e", util_compose_bytes("hallo"), "i42e")).as_bytes()).unwrap(),
Bencode::List(vec![ Bencode::List(vec![
Bencode::Bytes(ByteString::from_str("hallo")), Bencode::Bytes(ByteString::from_str("hallo")),
Bencode::Integer(42), Bencode::Integer(42),
]) ])
); );
assert_eq!(Bencode::decode( assert_eq!(Bencode::parse(
(format!("l{}{}{}{}e", "i-17e", util_encode_bytes("hallo"), "i42e", util_encode_bytes("tschüssi💩"))).as_bytes()).unwrap(), (format!("l{}{}{}{}e", "i-17e", util_compose_bytes("hallo"), "i42e", util_compose_bytes("tschüssi💩"))).as_bytes()).unwrap(),
Bencode::List(vec![ Bencode::List(vec![
Bencode::Integer(-17), Bencode::Integer(-17),
Bencode::Bytes(ByteString::from_str("hallo")), Bencode::Bytes(ByteString::from_str("hallo")),
@ -419,14 +419,14 @@ mod tests {
Bencode::Bytes(ByteString::from_str("tschüssi💩")), Bencode::Bytes(ByteString::from_str("tschüssi💩")),
]) ])
); );
assert_eq!(Bencode::decode("llelee".as_bytes()).unwrap(), assert_eq!(Bencode::parse("llelee".as_bytes()).unwrap(),
Bencode::List(vec![ Bencode::List(vec![
Bencode::List(Vec::new()), Bencode::List(Vec::new()),
Bencode::List(Vec::new()), Bencode::List(Vec::new()),
]), ]),
); );
let str = format!("ll{}{}ee", util_encode_bytes("hallo"), "i42e"); let str = format!("ll{}{}ee", util_compose_bytes("hallo"), "i42e");
assert_eq!(Bencode::decode(str.as_bytes()).unwrap(), assert_eq!(Bencode::parse(str.as_bytes()).unwrap(),
Bencode::List(vec![ Bencode::List(vec![
Bencode::List(vec![ Bencode::List(vec![
Bencode::Bytes(ByteString::from_str("hallo")), Bencode::Bytes(ByteString::from_str("hallo")),
@ -434,8 +434,8 @@ mod tests {
]), ]),
]), ]),
); );
let str = format!("ll{}{}el{}{}{}ee", util_encode_bytes("hallo"), "i42e", "i17e", util_encode_bytes("tschüss💩"), "i33e"); let str = format!("ll{}{}el{}{}{}ee", util_compose_bytes("hallo"), "i42e", "i17e", util_compose_bytes("tschüss💩"), "i33e");
assert_eq!(Bencode::decode(str.as_bytes()).unwrap(), assert_eq!(Bencode::parse(str.as_bytes()).unwrap(),
Bencode::List(vec![ Bencode::List(vec![
Bencode::List(vec![ Bencode::List(vec![
Bencode::Bytes(ByteString::from_str("hallo")), Bencode::Bytes(ByteString::from_str("hallo")),
@ -448,8 +448,8 @@ mod tests {
]), ]),
]), ]),
); );
let str = format!("ll{}{}ed{}{}{}{}ee", util_encode_bytes("hallo"), "i42e", util_encode_bytes("foo"), "i23e", util_encode_bytes("bar"), util_encode_bytes("baz")); let str = format!("ll{}{}ed{}{}{}{}ee", util_compose_bytes("hallo"), "i42e", util_compose_bytes("foo"), "i23e", util_compose_bytes("bar"), util_compose_bytes("baz"));
assert_eq!(Bencode::decode(str.as_bytes()).unwrap(), assert_eq!(Bencode::parse(str.as_bytes()).unwrap(),
Bencode::List(vec![ Bencode::List(vec![
Bencode::List(vec![ Bencode::List(vec![
Bencode::Bytes(ByteString::from_str("hallo")), Bencode::Bytes(ByteString::from_str("hallo")),
@ -464,26 +464,26 @@ mod tests {
} }
#[test] #[test]
fn test_dict() { fn test_dict() {
assert_eq!(Bencode::decode_dict( assert_eq!(Bencode::parse_dict(
("e").as_bytes()).unwrap(), ("e").as_bytes()).unwrap(),
(Bencode::Dict(HashMap::new()), 0) (Bencode::Dict(HashMap::new()), 0)
); );
let str = "3:fooi42ee"; let str = "3:fooi42ee";
assert_eq!(Bencode::decode_dict( assert_eq!(Bencode::parse_dict(
str.as_bytes()).unwrap(), str.as_bytes()).unwrap(),
(Bencode::Dict(HashMap::from([ (Bencode::Dict(HashMap::from([
(ByteString::from_str("foo"), Bencode::Integer(42)), (ByteString::from_str("foo"), Bencode::Integer(42)),
])), str.len() - 1) ])), str.len() - 1)
); );
let str = "3:foo3:bare"; let str = "3:foo3:bare";
assert_eq!(Bencode::decode_dict( assert_eq!(Bencode::parse_dict(
str.as_bytes()).unwrap(), str.as_bytes()).unwrap(),
(Bencode::Dict(HashMap::from([ (Bencode::Dict(HashMap::from([
(ByteString::from_str("foo"), Bencode::Bytes(ByteString::from_str("bar"))), (ByteString::from_str("foo"), Bencode::Bytes(ByteString::from_str("bar"))),
])), str.len() - 1) ])), str.len() - 1)
); );
let str = "3:fooi42e3:bar3:baze"; let str = "3:fooi42e3:bar3:baze";
assert_eq!(Bencode::decode_dict( assert_eq!(Bencode::parse_dict(
str.as_bytes()).unwrap(), str.as_bytes()).unwrap(),
(Bencode::Dict(HashMap::from([ (Bencode::Dict(HashMap::from([
(ByteString::from_str("foo"), Bencode::Integer(42)), (ByteString::from_str("foo"), Bencode::Integer(42)),
@ -491,7 +491,7 @@ mod tests {
])), str.len() - 1) ])), str.len() - 1)
); );
let str = "3:fooli42ei17ee3:bar3:baze"; let str = "3:fooli42ei17ee3:bar3:baze";
assert_eq!(Bencode::decode_dict( assert_eq!(Bencode::parse_dict(
str.as_bytes()).unwrap(), str.as_bytes()).unwrap(),
(Bencode::Dict(HashMap::from([ (Bencode::Dict(HashMap::from([
(ByteString::from_str("foo"), Bencode::List( (ByteString::from_str("foo"), Bencode::List(
@ -503,36 +503,36 @@ mod tests {
#[test] #[test]
fn test_dict_str() { fn test_dict_str() {
assert_eq!(Bencode::decode("de".as_bytes()).unwrap(), assert_eq!(Bencode::parse("de".as_bytes()).unwrap(),
Bencode::Dict(HashMap::new()) Bencode::Dict(HashMap::new())
); );
let str = "d3:fooi42ee"; let str = "d3:fooi42ee";
assert_eq!(Bencode::decode(str.as_bytes()).unwrap(), assert_eq!(Bencode::parse(str.as_bytes()).unwrap(),
Bencode::Dict(HashMap::from([ Bencode::Dict(HashMap::from([
(ByteString::from_str("foo"), Bencode::Integer(42)), (ByteString::from_str("foo"), Bencode::Integer(42)),
])) ]))
); );
let str = format!("d{}i42ee", util_encode_bytes("💩")); let str = format!("d{}i42ee", util_compose_bytes("💩"));
assert_eq!(Bencode::decode(str.as_bytes()).unwrap(), assert_eq!(Bencode::parse(str.as_bytes()).unwrap(),
Bencode::Dict(HashMap::from([ Bencode::Dict(HashMap::from([
(ByteString::from_str("💩"), Bencode::Integer(42)), (ByteString::from_str("💩"), Bencode::Integer(42)),
])) ]))
); );
let str = "d3:foo3:bare"; let str = "d3:foo3:bare";
assert_eq!(Bencode::decode(str.as_bytes()).unwrap(), assert_eq!(Bencode::parse(str.as_bytes()).unwrap(),
Bencode::Dict(HashMap::from([ Bencode::Dict(HashMap::from([
(ByteString::from_str("foo"), Bencode::Bytes(ByteString::from_str("bar"))), (ByteString::from_str("foo"), Bencode::Bytes(ByteString::from_str("bar"))),
])) ]))
); );
let str = "d3:fooi42e3:bar3:baze"; let str = "d3:fooi42e3:bar3:baze";
assert_eq!(Bencode::decode(str.as_bytes()).unwrap(), assert_eq!(Bencode::parse(str.as_bytes()).unwrap(),
Bencode::Dict(HashMap::from([ Bencode::Dict(HashMap::from([
(ByteString::from_str("foo"), Bencode::Integer(42)), (ByteString::from_str("foo"), Bencode::Integer(42)),
(ByteString::from_str("bar"), Bencode::Bytes(ByteString::from_str("baz"))), (ByteString::from_str("bar"), Bencode::Bytes(ByteString::from_str("baz"))),
])) ]))
); );
let str = "d3:fooli42ei17ee3:bar3:baze"; let str = "d3:fooli42ei17ee3:bar3:baze";
assert_eq!(Bencode::decode(str.as_bytes()).unwrap(), assert_eq!(Bencode::parse(str.as_bytes()).unwrap(),
Bencode::Dict(HashMap::from([ Bencode::Dict(HashMap::from([
(ByteString::from_str("foo"), Bencode::List( (ByteString::from_str("foo"), Bencode::List(
vec![Bencode::Integer(42), Bencode::Integer(17)])), vec![Bencode::Integer(42), Bencode::Integer(17)])),
@ -540,7 +540,7 @@ mod tests {
])) ]))
); );
let str = "d3:foo3:bare"; let str = "d3:foo3:bare";
assert_eq!(Bencode::decode(str.as_bytes()).unwrap(), assert_eq!(Bencode::parse(str.as_bytes()).unwrap(),
Bencode::Dict(HashMap::from([ Bencode::Dict(HashMap::from([
(ByteString::from_str("foo"), Bencode::Bytes(ByteString::from_str("bar"))), (ByteString::from_str("foo"), Bencode::Bytes(ByteString::from_str("bar"))),
])) ]))