diff --git a/src/main.rs b/src/main.rs index 333c81c..cf17f66 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ // who do you think you are? my mom?! #![allow(uncommon_codepoints)] -use std::{env, fs}; +use std::{env, fs, path}; use crate::bencode::de::from_bytes; use crate::bencode::ser::to_bytes; @@ -9,17 +9,41 @@ use anyhow::{anyhow, Result}; use rand::prelude::*; use reqwest::blocking::Client; use reqwest::Url; -use serde::{de, Deserialize, Deserializer, Serialize}; +use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; +use serde::ser::SerializeSeq; use serde_bytes::ByteBuf; use sha1::{Digest, Sha1}; mod bencode; +mod torrent; #[derive(Serialize, Deserialize, Debug)] struct FileInfo { length: i64, - // TODO with os.path.join - path: Vec, + #[serde(deserialize_with = "deserialize_path", serialize_with = "serialize_path")] + path: String, +} + +fn deserialize_path<'de, D>(deserializer: D) -> Result +where D: Deserializer<'de> { + let parts: Vec = Vec::deserialize(deserializer)?; + for p in parts.iter() { + let res = p.find(path::MAIN_SEPARATOR_STR); + if let Some(_) = res { + return Err(de::Error::custom(format!("Unable to deal with {} (platform path separator in path parts", path::MAIN_SEPARATOR_STR))) + } + } + Ok(parts.join(path::MAIN_SEPARATOR_STR)) +} + +fn serialize_path(v: &String, serializer: S) -> Result +where S: Serializer { + let parts: Vec<&str> = v.split(path::MAIN_SEPARATOR_STR).collect(); + let mut seq = serializer.serialize_seq(Some(parts.len()))?; + for p in parts { + seq.serialize_element(p)?; + } + seq.end() } #[derive(Serialize, Deserialize, Debug)] @@ -46,7 +70,7 @@ where D: Deserializer<'de> { } fn serialize_pieces(v: &Vec<[u8; 20]>, serializer: S) -> Result -where S: serde::ser::Serializer +where S: Serializer { let mut buf = Vec::new(); for i in v { @@ -129,7 +153,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(())