| 1 | //! An encoder for torrent files |
| 2 | //! |
| 3 | //! This example will ... |
| 4 | //! |
| 5 | //! - serialize a torrent file representing object in bencode format |
| 6 | //! - and print the result into stdout. |
| 7 | //! |
| 8 | //! *Attention*: Please consider to pipe the output into a file of your choice. |
| 9 | //! |
| 10 | //! # Run the Example |
| 11 | //! |
| 12 | //! ``` |
| 13 | //! cargo run --example encode_torrent > example.torrent |
| 14 | //! ``` |
| 15 | |
| 16 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#; |
| 17 | |
| 18 | use bendyrust-analyzer cargo bendy 0.6.1 crate/::encodingrust-analyzer cargo bendy 0.6.1 encoding/::{AsStringrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/AsString#, Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#, SingleItemEncoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder#, ToBencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#}; |
| 19 | |
| 20 | /// Main struct containing all required information. |
| 21 | /// |
| 22 | /// Based on: [http://fileformats.wikia.com/wiki/Torrent_file]. |
| 23 | /// |
| 24 | /// # Design Decision |
| 25 | /// |
| 26 | /// To keep the example simple we won't parse the integers fields |
| 27 | /// into a concrete number type as the bencode integer definition |
| 28 | /// is actually a `BigNum` and the content may not fit. |
| 29 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 30 | struct MetaInforust-analyzer cargo bendy 0.6.1 MetaInfo# { |
| 31 | pub announcerust-analyzer cargo bendy 0.6.1 MetaInfo#announce.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 32 | pub inforust-analyzer cargo bendy 0.6.1 MetaInfo#info.: Inforust-analyzer cargo bendy 0.6.1 Info#, |
| 33 | pub commentrust-analyzer cargo bendy 0.6.1 MetaInfo#comment.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>, // not official element |
| 34 | pub creation_daterust-analyzer cargo bendy 0.6.1 MetaInfo#creation_date.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>, // not official element |
| 35 | pub http_seedsrust-analyzer cargo bendy 0.6.1 MetaInfo#http_seeds.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>>, // not official element |
| 36 | } |
| 37 | |
| 38 | /// File related information (Single-file format) |
| 39 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 40 | struct Inforust-analyzer cargo bendy 0.6.1 Info# { |
| 41 | pub piece_lengthrust-analyzer cargo bendy 0.6.1 Info#piece_length.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 42 | pub piecesrust-analyzer cargo bendy 0.6.1 Info#pieces.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, |
| 43 | pub namerust-analyzer cargo bendy 0.6.1 Info#name.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 44 | pub file_lengthrust-analyzer cargo bendy 0.6.1 Info#file_length.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 45 | } |
| 46 | |
| 47 | impl ToBencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode# for MetaInforust-analyzer cargo bendy 0.6.1 MetaInfo# { |
| 48 | // Adds an additional recursion level -- itself formatted as dictionary -- |
| 49 | // around the info struct. |
| 50 | const MAX_DEPTHrust-analyzer cargo bendy 0.6.1 impl#[MetaInfo][ToBencode]MAX_DEPTH.: usize = Inforust-analyzer cargo bendy 0.6.1 Info#::MAX_DEPTHrust-analyzer cargo bendy 0.6.1 impl#[Info][ToBencode]MAX_DEPTH. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1; |
| 51 | |
| 52 | fn encoderust-analyzer cargo bendy 0.6.1 impl#[MetaInfo][ToBencode]encode().(&self, encoder: SingleItemEncoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> { |
| 53 | encoder.emit_dictrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SingleItemEncoder<'a>`]emit_dict().(|mut e| { |
| 54 | e.emit_pairrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SortedDictEncoder<'a>`]emit_pair().(b"announce", &self.announcerust-analyzer cargo bendy 0.6.1 MetaInfo#announce.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 55 | |
| 56 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(comment) = &self.commentrust-analyzer cargo bendy 0.6.1 MetaInfo#comment. { |
| 57 | e.emit_pairrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SortedDictEncoder<'a>`]emit_pair().(b"comment", comment)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 58 | } |
| 59 | |
| 60 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(creation_date) = &self.creation_daterust-analyzer cargo bendy 0.6.1 MetaInfo#creation_date. { |
| 61 | e.emit_pairrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SortedDictEncoder<'a>`]emit_pair().(b"creation date", creation_date)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 62 | } |
| 63 | |
| 64 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(seeds) = &self.http_seedsrust-analyzer cargo bendy 0.6.1 MetaInfo#http_seeds. { |
| 65 | // List is a simple iterable wrapper that allows to encode |
| 66 | // any list like container as bencode list object. |
| 67 | e.emit_pairrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SortedDictEncoder<'a>`]emit_pair().(b"httpseeds", seeds)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 68 | } |
| 69 | |
| 70 | e.emit_pairrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SortedDictEncoder<'a>`]emit_pair().(b"info", &self.inforust-analyzer cargo bendy 0.6.1 MetaInfo#info.) |
| 71 | })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 72 | |
| 73 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | impl ToBencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode# for Inforust-analyzer cargo bendy 0.6.1 Info# { |
| 78 | // The struct is encoded as dictionary and all of it internals are encoded |
| 79 | // as flat values, i.e. strings or integers. |
| 80 | const MAX_DEPTHrust-analyzer cargo bendy 0.6.1 impl#[Info][ToBencode]MAX_DEPTH.: usize = 1; |
| 81 | |
| 82 | fn encoderust-analyzer cargo bendy 0.6.1 impl#[Info][ToBencode]encode().(&self, encoder: SingleItemEncoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> { |
| 83 | encoder.emit_dictrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SingleItemEncoder<'a>`]emit_dict().(|mut e| { |
| 84 | e.emit_pairrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SortedDictEncoder<'a>`]emit_pair().(b"length", &self.file_lengthrust-analyzer cargo bendy 0.6.1 Info#file_length.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 85 | e.emit_pairrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SortedDictEncoder<'a>`]emit_pair().(b"name", &self.namerust-analyzer cargo bendy 0.6.1 Info#name.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 86 | e.emit_pairrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SortedDictEncoder<'a>`]emit_pair().(b"piece length", &self.piece_lengthrust-analyzer cargo bendy 0.6.1 Info#piece_length.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 87 | e.emit_pairrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SortedDictEncoder<'a>`]emit_pair().(b"pieces", AsStringrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/AsString#(&self.piecesrust-analyzer cargo bendy 0.6.1 Info#pieces.)) |
| 88 | })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 89 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | fn mainrust-analyzer cargo bendy 0.6.1 main().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<dyn stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#>> { |
| 94 | let torrent = MetaInforust-analyzer cargo bendy 0.6.1 MetaInfo# { |
| 95 | announcerust-analyzer cargo bendy 0.6.1 MetaInfo#announce.: "http://bttracker.debian.org:6969/announce".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(), |
| 96 | commentrust-analyzer cargo bendy 0.6.1 MetaInfo#comment.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("\"Debian CD from cdimage.debian.org\"".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().()), |
| 97 | creation_daterust-analyzer cargo bendy 0.6.1 MetaInfo#creation_date.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(1_520_682_848.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()), |
| 98 | http_seedsrust-analyzer cargo bendy 0.6.1 MetaInfo#http_seeds.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![ |
| 99 | "https://cdimage.debian.org/cdimage/release/9.4.0//srv/cdbuilder.debian.org/dst/deb-cd/weekly-builds/amd64/iso-cd/debian-9.4.0-amd64-netinst.iso".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(), |
| 100 | "https://cdimage.debian.org/cdimage/archive/9.4.0//srv/cdbuilder.debian.org/dst/deb-cd/weekly-builds/amd64/iso-cd/debian-9.4.0-amd64-netinst.iso".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(), |
| 101 | ]), |
| 102 | inforust-analyzer cargo bendy 0.6.1 MetaInfo#info.: Inforust-analyzer cargo bendy 0.6.1 Info# { |
| 103 | piece_lengthrust-analyzer cargo bendy 0.6.1 Info#piece_length.: 262_144.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 104 | piecesrust-analyzer cargo bendy 0.6.1 Info#pieces.: include_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/include_bytes!!("torrent_files/pieces.iso").to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().(), |
| 105 | namerust-analyzer cargo bendy 0.6.1 Info#name.: "debian-9.4.0-amd64-netinst.iso".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(), |
| 106 | file_lengthrust-analyzer cargo bendy 0.6.1 Info#file_length.: 305_135_616.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 107 | }, |
| 108 | }; |
| 109 | |
| 110 | let data = torrent.to_bencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#to_bencode().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 111 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::stdoutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/stdout().().write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/impl#[Stdout][Write]write_all().(&data)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 112 | |
| 113 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 114 | } |