[main] Implemented joining pathes for FileInfos. Still broken though :/

This commit is contained in:
Fabian 2024-08-11 23:40:17 +02:00
parent 0cfc7e19a4
commit fd4a7e3589

View File

@ -1,7 +1,7 @@
// who do you think you are? my mom?! // who do you think you are? my mom?!
#![allow(uncommon_codepoints)] #![allow(uncommon_codepoints)]
use std::{env, fs}; use std::{env, fs, path};
use crate::bencode::de::from_bytes; use crate::bencode::de::from_bytes;
use crate::bencode::ser::to_bytes; use crate::bencode::ser::to_bytes;
@ -9,17 +9,41 @@ use anyhow::{anyhow, Result};
use rand::prelude::*; use rand::prelude::*;
use reqwest::blocking::Client; use reqwest::blocking::Client;
use reqwest::Url; 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 serde_bytes::ByteBuf;
use sha1::{Digest, Sha1}; use sha1::{Digest, Sha1};
mod bencode; mod bencode;
mod torrent;
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
struct FileInfo { struct FileInfo {
length: i64, length: i64,
// TODO with os.path.join #[serde(deserialize_with = "deserialize_path", serialize_with = "serialize_path")]
path: Vec<String>, path: String,
}
fn deserialize_path<'de, D>(deserializer: D) -> Result<String, D::Error>
where D: Deserializer<'de> {
let parts: Vec<String> = 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<S>(v: &String, serializer: S) -> Result<S::Ok, S::Error>
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)] #[derive(Serialize, Deserialize, Debug)]
@ -46,7 +70,7 @@ where D: Deserializer<'de> {
} }
fn serialize_pieces<S>(v: &Vec<[u8; 20]>, serializer: S) -> Result<S::Ok, S::Error> fn serialize_pieces<S>(v: &Vec<[u8; 20]>, serializer: S) -> Result<S::Ok, S::Error>
where S: serde::ser::Serializer where S: Serializer
{ {
let mut buf = Vec::new(); let mut buf = Vec::new();
for i in v { for i in v {
@ -129,7 +153,7 @@ fn main() -> Result<()> {
let resp = client.get(url).send()?; let resp = client.get(url).send()?;
let status = resp.status(); let status = resp.status();
// let body = resp.text()?; let body = resp.text()?;
println!("Response: {status}"); println!("Response: {status}");
Ok(()) Ok(())