[main] Implemented joining pathes for FileInfo
s. Still broken though :/
This commit is contained in:
parent
0cfc7e19a4
commit
fd4a7e3589
36
src/main.rs
36
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<String>,
|
||||
#[serde(deserialize_with = "deserialize_path", serialize_with = "serialize_path")]
|
||||
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)]
|
||||
@ -46,7 +70,7 @@ where D: Deserializer<'de> {
|
||||
}
|
||||
|
||||
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();
|
||||
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(())
|
||||
|
Loading…
Reference in New Issue
Block a user