init [skip ci]

This commit is contained in:
codecrafters-bot 2024-10-02 20:57:20 +00:00
commit fdf26f9688
9 changed files with 1596 additions and 0 deletions

2
.gitattributes vendored Normal file
View File

@ -0,0 +1,2 @@
* text=auto
*.torrent binary

10
.gitignore vendored Normal file
View File

@ -0,0 +1,10 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/
# These are backup files generated by rustfmt
**/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

1451
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

36
Cargo.toml Normal file
View File

@ -0,0 +1,36 @@
# DON'T EDIT THIS!
#
# Codecrafters relies on this file being intact to run tests successfully. Any changes
# here will not reflect when CodeCrafters tests your code, and might even cause build
# failures.
#
# DON'T EDIT THIS!
[package]
name = "bittorrent-starter-rust"
version = "0.1.0"
authors = ["Codecrafters <hello@codecrafters.io>"]
edition = "2021"
# DON'T EDIT THIS!
#
# Codecrafters relies on this file being intact to run tests successfully. Any changes
# here will not reflect when CodeCrafters tests your code, and might even cause build
# failures.
#
# DON'T EDIT THIS!
[dependencies]
anyhow = "1.0.68" # error handling
bytes = "1.3.0" # helps wrap responses from reqwest
clap = { version = "4.0.32", features = ["derive"]} # creating a cli
hex = "0.4.3"
regex = "1" # for regular expressions
reqwest = { version = "0.11.18", features = ["json", "blocking"] } # http requests
serde = { version = "1.0.136", features = ["derive"] } # for json mangling
serde_bencode = "0.2.3" # for bencode encoding/decoding
serde_bytes = "0.11.12" # for dealing with bytes
serde_json = "1.0.105" # for json mangling
serde_urlencoded = "0.7.1" # for url encoding
sha1 = "0.10.1" # hashing
tempfile = "3" # creating temporary directories
thiserror = "1.0.38" # error handling
tokio = { version = "1.23.0", features = ["full"] } # async http requests

35
README.md Normal file
View File

@ -0,0 +1,35 @@
[![progress-banner](https://backend.codecrafters.io/progress/bittorrent/ec9f21f6-198a-4329-8b4c-b7e0dff6c180)](https://app.codecrafters.io/users/codecrafters-bot?r=2qF)
This is a starting point for Rust solutions to the
["Build Your Own BitTorrent" Challenge](https://app.codecrafters.io/courses/bittorrent/overview).
In this challenge, youll build a BitTorrent client that's capable of parsing a
.torrent file and downloading a file from a peer. Along the way, well learn
about how torrent files are structured, HTTP trackers, BitTorrents Peer
Protocol, pipelining and more.
**Note**: If you're viewing this repo on GitHub, head over to
[codecrafters.io](https://codecrafters.io) to try the challenge.
# Passing the first stage
The entry point for your BitTorrent implementation is in `src/main.rs`. Study
and uncomment the relevant code, and push your changes to pass the first stage:
```sh
git commit -am "pass 1st stage" # any msg
git push origin master
```
Time to move on to the next stage!
# Stage 2 & beyond
Note: This section is for stages 2 and beyond.
1. Ensure you have `cargo (1.70)` installed locally
1. Run `./your_bittorrent.sh` to run your program, which is implemented in
`src/main.rs`. This command compiles your Rust project, so it might be slow
the first time you run it. Subsequent runs will be fast.
1. Commit your changes and run `git push origin master` to submit your solution
to CodeCrafters. Test output will be streamed to your terminal.

11
codecrafters.yml Normal file
View File

@ -0,0 +1,11 @@
# Set this to true if you want debug logs.
#
# These can be VERY verbose, so we suggest turning them off
# unless you really need them.
debug: false
# Use this to change the Rust version used to run your code
# on Codecrafters.
#
# Available versions: rust-1.77
language_pack: rust-1.77

1
sample.torrent Normal file
View File

@ -0,0 +1 @@
d8:announce55:http://bittorrent-test-tracker.codecrafters.io/announce10:created by13:mktorrent 1.14:infod6:lengthi92063e4:name10:sample.txt12:piece lengthi32768e6:pieces60:èvöz*ˆ†èókg&â—-n"uæ vfVsn<73>ÿµR­<>5ð “zß¼<E2809A> r'­ž<C2AD>šÌee

38
src/main.rs Normal file
View File

@ -0,0 +1,38 @@
use serde_json;
use std::env;
// Available if you need it!
// use serde_bencode
#[allow(dead_code)]
fn decode_bencoded_value(encoded_value: &str) -> serde_json::Value {
// If encoded_value starts with a digit, it's a number
if encoded_value.chars().next().unwrap().is_digit(10) {
// Example: "5:hello" -> "hello"
let colon_index = encoded_value.find(':').unwrap();
let number_string = &encoded_value[..colon_index];
let number = number_string.parse::<i64>().unwrap();
let string = &encoded_value[colon_index + 1..colon_index + 1 + number as usize];
return serde_json::Value::String(string.to_string());
} else {
panic!("Unhandled encoded value: {}", encoded_value)
}
}
// Usage: your_bittorrent.sh decode "<encoded_value>"
fn main() {
let args: Vec<String> = env::args().collect();
let command = &args[1];
if command == "decode" {
// You can use print statements as follows for debugging, they'll be visible when running tests.
println!("Logs from your program will appear here!");
// Uncomment this block to pass the first stage
// let encoded_value = &args[2];
// let decoded_value = decode_bencoded_value(encoded_value);
// println!("{}", decoded_value.to_string());
} else {
println!("unknown command: {}", args[1])
}
}

12
your_bittorrent.sh Executable file
View File

@ -0,0 +1,12 @@
#!/bin/sh
#
# DON'T EDIT THIS!
#
# CodeCrafters uses this file to test your code. Don't make any changes here!
#
# DON'T EDIT THIS!
exec cargo run \
--quiet \
--release \
--target-dir=/tmp/codecrafters-bittorrent-target \
--manifest-path $(dirname "$0")/Cargo.toml -- "$@"