File tree Expand file tree Collapse file tree 3 files changed +12
-9
lines changed
Expand file tree Collapse file tree 3 files changed +12
-9
lines changed Original file line number Diff line number Diff line change 1- # Torrent Parser
1+ > :warning : This project is still a WIP :warning :
2+
3+ # Bencode Parser
24
35A bencode parser written in Rust
46
Original file line number Diff line number Diff line change 22//!
33//! ## Example
44//! ```rust
5- //! let res: BEncode = BEncode::parse("./src/Hello.txt");
5+ //! let path: PathBuf = PathBuf::from("./src/Hello.txt");
6+ //! let bytes = fs::read(path).expect("Couldn't Read File!");
7+ //! let res: BEncode = BEncode::parse(bytes);
68//! println!("Decoded Object: {:?}", res);
79//!
810//! ```
911
1012use std:: cmp:: Ordering ;
1113use std:: collections:: HashMap ;
12- use std:: path:: PathBuf ;
13- use std:: { fmt, fs} ;
14+ use std:: fmt;
1415
1516/// The BEncode Object.
1617/// This enum wraps the data types supported by bencode objects, with an addition of `String`.
@@ -46,10 +47,7 @@ impl fmt::Debug for BEncode {
4647impl BEncode {
4748 /// This function returns the parsed [`BEncode`] object, given a valid path to a file containing bencode.
4849 /// returns a `Bencode::Int(-1)` if the bencode cannot be parsed
49- pub fn parse ( file_path : & str ) -> Self {
50- let path: PathBuf = PathBuf :: from ( file_path) ;
51- let bytes = fs:: read ( path) . expect ( "Couldn't Read File!" ) ;
52-
50+ pub fn parse ( bytes : Vec < u8 > ) -> Self {
5351 // =====================STATE VARIABLES==========================
5452 let mut parents: Vec < BEncode > = Vec :: new ( ) ;
5553 let mut dict_keys: Vec < String > = Vec :: new ( ) ;
Original file line number Diff line number Diff line change @@ -27,7 +27,10 @@ fn main() {
2727 return ;
2828 }
2929
30- let res: BEncode = BEncode :: parse ( & args. input ) ;
30+ let path: PathBuf = PathBuf :: from ( & args. input ) ;
31+ let bytes = fs:: read ( path) . expect ( "Couldn't Read File!" ) ;
32+
33+ let res: BEncode = BEncode :: parse ( bytes) ;
3134
3235 if let BEncode :: Dictionary ( _) = res {
3336 let file_path: PathBuf = PathBuf :: from ( args. output ) ;
You can’t perform that action at this time.
0 commit comments