Compare commits

...

2 Commits

Author SHA1 Message Date
2647cee911 Minor cleanup 2024-08-12 00:41:33 +02:00
ee092097c7 [bencode/ser] Properly sort dictionaries 2024-08-12 00:40:51 +02:00
3 changed files with 25 additions and 20 deletions

View File

@ -329,8 +329,7 @@ impl <'a> ser::SerializeTupleVariant for &'a mut Serializer {
impl<'a> MapSerializer<'a> {
fn finalize(self) {
for (key, value) in self.sorted_map.into_iter() {
self.super_serializer.output.extend(key);
for value in self.sorted_map.values() {
self.super_serializer.output.extend(value);
}
self.super_serializer.output.push('e' as u8);
@ -346,10 +345,12 @@ impl <'a> ser::SerializeMap for MapSerializer<'a> {
T: ?Sized + Serialize
{
key.serialize(&mut self.value_serializer)?;
self.last_inserted_key = self.value_serializer.output.clone();
let insert_key_start = self.value_serializer.output.iter().position(|x| *x == b':')
.ok_or(Error::Syntax("missing \":\" from just serialized byte string".to_string()))?;
self.last_inserted_key = self.value_serializer.output[insert_key_start+1..].to_vec();
let mut key_to_insert = Vec::new();
swap(&mut self.value_serializer.output, &mut key_to_insert);
self.sorted_map.insert(key_to_insert, Vec::new());
self.sorted_map.insert(self.last_inserted_key.clone(), key_to_insert);
Ok(())
}
@ -370,7 +371,7 @@ impl <'a> ser::SerializeMap for MapSerializer<'a> {
swap(&mut self.value_serializer.output, &mut value_to_insert);
let Some(x) = self.sorted_map.get_mut(&self.last_inserted_key)
else { unreachable!() };
*x = value_to_insert;
x.extend(value_to_insert);
} else {
self.sorted_map.remove(&self.last_inserted_key);
self.value_serializer.output.clear();
@ -404,9 +405,13 @@ impl <'a> ser::SerializeStruct for MapSerializer<'a> {
let mut val_str = Vec::new();
swap(&mut self.value_serializer.output, &mut val_str);
key.serialize(&mut self.value_serializer)?;
let insert_key_start = self.value_serializer.output.iter().position(|x| *x == b':')
.ok_or(Error::Syntax("missing \":\" from just serialized byte string".to_string()))?;
let insert_key = self.value_serializer.output[insert_key_start+1..].to_vec();
let mut key_str = Vec::new();
swap(&mut self.value_serializer.output, &mut key_str);
self.sorted_map.insert(key_str, val_str);
key_str.extend(val_str);
self.sorted_map.insert(insert_key, key_str);
Ok(())
}
@ -467,15 +472,15 @@ mod test {
fn test_map() {
assert_eq!(to_string(&HashMap::from([
("foo", 42),
("bar", 13),
("bare", 13),
])).unwrap(),
"d3:bari13e3:fooi42ee"
"d4:barei13e3:fooi42ee"
);
assert_eq!(to_string(&HashMap::from([
("foo", "💩"),
("bar", "🙈"),
("bare", "🙈"),
])).unwrap(),
"d3:bar4:🙈3:foo4:💩e"
"d4:bare4:🙈3:foo4:💩e"
);
assert_eq!(to_string(&HashMap::from([
("foo", Some("💩")),
@ -504,12 +509,12 @@ mod test {
l: vec![3, 7, 9],
m: HashMap::from([
("foo".to_string(), 23),
("bar".to_string(), 18),
("bare".to_string(), 18),
]),
o: Some("baz".to_string()),
o2: None,
}).unwrap(),
"d1:lli3ei7ei9ee1:md3:bari18e3:fooi23ee1:o3:baze"
"d1:lli3ei7ei9ee1:md4:barei18e3:fooi23ee1:o3:baze"
);
#[derive(Serialize)]

View File

@ -60,7 +60,7 @@ fn main() -> Result<()> {
let resp = client.get(url).send()?;
let status = resp.status();
let body = resp.text()?;
let _body = resp.text()?;
println!("Response: {status}");
Ok(())

View File

@ -1,10 +1,10 @@
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use std::path;
use serde_bytes::ByteBuf;
use sha1::{Digest, Sha1};
use serde::ser::SerializeSeq;
use crate::bencode::de::from_bytes;
use crate::bencode::ser::to_bytes;
use serde::ser::SerializeSeq;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use serde_bytes::ByteBuf;
use sha1::{Digest, Sha1};
use std::path;
#[derive(Serialize, Deserialize, Debug)]
pub struct FileInfo {
@ -96,9 +96,9 @@ impl Torrent {
#[cfg(test)]
mod tests {
use std::{env, fs};
use std::path::Path;
use crate::torrent::Torrent;
use std::path::Path;
use std::{env, fs};
#[test]
fn check_correct_info_hash() {