| 1 | //! A decoder for torrent files. |
| 2 | //! |
| 3 | //! This example will ... |
| 4 | //! |
| 5 | //! - read a torrent file, |
| 6 | //! - deserialize the bencode formatted information |
| 7 | //! - and print the result into stdout. |
| 8 | //! |
| 9 | //! *Attention*: Please consider to pipe the output into a file of your choice. |
| 10 | //! |
| 11 | //! # Run the Example |
| 12 | //! |
| 13 | //! ``` |
| 14 | //! cargo run --example decode_torrent > parsing_output.txt |
| 15 | //! ``` |
| 16 | |
| 17 | use bendyrust-analyzer cargo bendy 0.6.1 crate/::{ |
| 18 | decodingrust-analyzer cargo bendy 0.6.1 decoding/::{Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#, FromBencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#, Objectrust-analyzer cargo bendy 0.6.1 decoding/object/Object#, ResultExtrust-analyzer cargo bendy 0.6.1 decoding/error/ResultExt#}, |
| 19 | encodingrust-analyzer cargo bendy 0.6.1 encoding/::AsStringrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/AsString#, |
| 20 | }; |
| 21 | |
| 22 | static EXAMPLE_TORRENTrust-analyzer cargo bendy 0.6.1 EXAMPLE_TORRENT.: &[u8] = |
| 23 | include_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/include_bytes!!("torrent_files/debian-9.4.0-amd64-netinst.iso.torrent"); |
| 24 | |
| 25 | /// Main struct containing all required information. |
| 26 | /// |
| 27 | /// Based on: [http://fileformats.wikia.com/wiki/Torrent_file]. |
| 28 | /// |
| 29 | /// # Design Decision |
| 30 | /// |
| 31 | /// To keep the example simple we won't parse the integers fields |
| 32 | /// into a concrete number type as the bencode integer definition |
| 33 | /// is actually a `BigNum` and the content may not fit. |
| 34 | #[allow(dead_code)] |
| 35 | #[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:)] |
| 36 | struct MetaInforust-analyzer cargo bendy 0.6.1 MetaInfo# { |
| 37 | pub announcerust-analyzer cargo bendy 0.6.1 MetaInfo#announce.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 38 | pub inforust-analyzer cargo bendy 0.6.1 MetaInfo#info.: Inforust-analyzer cargo bendy 0.6.1 Info#, |
| 39 | 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 |
| 40 | 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#<u64>, // not official element |
| 41 | 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 |
| 42 | } |
| 43 | |
| 44 | /// File related information (Single-file format) |
| 45 | #[allow(dead_code)] |
| 46 | #[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:)] |
| 47 | struct Inforust-analyzer cargo bendy 0.6.1 Info# { |
| 48 | 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#, |
| 49 | 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>, |
| 50 | pub namerust-analyzer cargo bendy 0.6.1 Info#name.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 51 | 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#, |
| 52 | } |
| 53 | |
| 54 | impl FromBencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode# for MetaInforust-analyzer cargo bendy 0.6.1 MetaInfo# { |
| 55 | // Try to parse with a `max_depth` of two. |
| 56 | // |
| 57 | // The required max depth of a data structure is calculated as follows: |
| 58 | // |
| 59 | // - Every potential nesting level encoded as bencode dictionary or list count as +1, |
| 60 | // - everything else is ignored. |
| 61 | // |
| 62 | // This typically means that we only need to count the amount of nested structs and container |
| 63 | // types. (Potentially ignoring lists of bytes as they are normally encoded as strings.) |
| 64 | // |
| 65 | // struct MetaInfo { // encoded as dictionary (+1) |
| 66 | // announce: String, |
| 67 | // info: Info { // encoded as dictionary (+1) |
| 68 | // piece_length: String, |
| 69 | // pieces: Vec<u8>, // encoded as string and therefore ignored |
| 70 | // name: String, |
| 71 | // file_length: String, |
| 72 | // }, |
| 73 | // comment: Option<String>, |
| 74 | // creation_date: Option<u64>, |
| 75 | // http_seeds: Option<Vec<String>> // if available encoded as list but even then doesn't |
| 76 | // increase the limit over the deepest chain including |
| 77 | // info |
| 78 | // } |
| 79 | const EXPECTED_RECURSION_DEPTHrust-analyzer cargo bendy 0.6.1 impl#[MetaInfo][FromBencode]EXPECTED_RECURSION_DEPTH.: usize = Inforust-analyzer cargo bendy 0.6.1 Info#::EXPECTED_RECURSION_DEPTHrust-analyzer cargo bendy 0.6.1 impl#[Info][FromBencode]EXPECTED_RECURSION_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; |
| 80 | |
| 81 | /// Entry point for decoding a torrent. The dictionary is parsed for all |
| 82 | /// non-optional and optional fields. Missing optional fields are ignored |
| 83 | /// but any other missing fields result in stopping the decoding and in |
| 84 | /// spawning [`DecodingError::MissingField`]. |
| 85 | fn decode_bencode_objectrust-analyzer cargo bendy 0.6.1 impl#[MetaInfo][FromBencode]decode_bencode_object().(object: Objectrust-analyzer cargo bendy 0.6.1 decoding/object/Object#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo bendy 0.6.1 impl#[MetaInfo][FromBencode], Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#> |
| 86 | where |
| 87 | Selfrust-analyzer cargo bendy 0.6.1 impl#[MetaInfo][FromBencode]: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#, |
| 88 | { |
| 89 | let mut announce = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 90 | let mut comment = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 91 | let mut creation_date = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 92 | let mut http_seeds = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 93 | let mut info = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 94 | |
| 95 | let mut dict_dec = object.try_into_dictionaryrust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]try_into_dictionary().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 96 | while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(pair) = dict_dec.next_pairrust-analyzer cargo bendy 0.6.1 decoding/decoder/impl#[`DictDecoder<'obj, 'ser>`]next_pair().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). { |
| 97 | match pair { |
| 98 | (b"announce", value) => { |
| 99 | announce = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::decode_bencode_objectrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/impl#[String][FromBencode]decode_bencode_object().(value) |
| 100 | .contextrust-analyzer cargo bendy 0.6.1 decoding/error/impl#[`Result<T, Error>`][ResultExt]context().("announce") |
| 101 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 102 | }, |
| 103 | (b"comment", value) => { |
| 104 | comment = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::decode_bencode_objectrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/impl#[String][FromBencode]decode_bencode_object().(value) |
| 105 | .contextrust-analyzer cargo bendy 0.6.1 decoding/error/impl#[`Result<T, Error>`][ResultExt]context().("comment") |
| 106 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 107 | }, |
| 108 | (b"creation date", value) => { |
| 109 | creation_date = u64::decode_bencode_objectrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/impl#[u64][FromBencode]decode_bencode_object().(value) |
| 110 | .contextrust-analyzer cargo bendy 0.6.1 decoding/error/impl#[`Result<T, Error>`][ResultExt]context().("creation_date") |
| 111 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 112 | }, |
| 113 | (b"httpseeds", value) => { |
| 114 | http_seeds = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::decode_bencode_objectrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/impl#[`Vec<ContentT>`][FromBencode]decode_bencode_object().(value) |
| 115 | .contextrust-analyzer cargo bendy 0.6.1 decoding/error/impl#[`Result<T, Error>`][ResultExt]context().("http_seeds") |
| 116 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 117 | }, |
| 118 | (b"info", value) => { |
| 119 | info = Inforust-analyzer cargo bendy 0.6.1 Info#::decode_bencode_objectrust-analyzer cargo bendy 0.6.1 impl#[Info][FromBencode]decode_bencode_object().(value) |
| 120 | .contextrust-analyzer cargo bendy 0.6.1 decoding/error/impl#[`Result<T, Error>`][ResultExt]context().("info") |
| 121 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 122 | }, |
| 123 | (unknown_field, _) => { |
| 124 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#::unexpected_fieldrust-analyzer cargo bendy 0.6.1 decoding/error/impl#[Error]unexpected_field().(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::from_utf8_lossyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]from_utf8_lossy().( |
| 125 | unknown_field, |
| 126 | ))); |
| 127 | }, |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | let announce = announce.ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#::missing_fieldrust-analyzer cargo bendy 0.6.1 decoding/error/impl#[Error]missing_field().("announce"))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 132 | let info = info.ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#::missing_fieldrust-analyzer cargo bendy 0.6.1 decoding/error/impl#[Error]missing_field().("info"))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 133 | |
| 134 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(MetaInforust-analyzer cargo bendy 0.6.1 MetaInfo# { |
| 135 | announcerust-analyzer cargo bendy 0.6.1 MetaInfo#announce., |
| 136 | inforust-analyzer cargo bendy 0.6.1 MetaInfo#info., |
| 137 | commentrust-analyzer cargo bendy 0.6.1 MetaInfo#comment., |
| 138 | creation_daterust-analyzer cargo bendy 0.6.1 MetaInfo#creation_date., |
| 139 | http_seedsrust-analyzer cargo bendy 0.6.1 MetaInfo#http_seeds., |
| 140 | }) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | impl FromBencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode# for Inforust-analyzer cargo bendy 0.6.1 Info# { |
| 145 | const EXPECTED_RECURSION_DEPTHrust-analyzer cargo bendy 0.6.1 impl#[Info][FromBencode]EXPECTED_RECURSION_DEPTH.: usize = 1; |
| 146 | |
| 147 | /// Treats object as dictionary containing all fields for the info struct. |
| 148 | /// On success the dictionary is parsed for the fields of info which are |
| 149 | /// necessary for torrent. Any missing field will result in a missing field |
| 150 | /// error which will stop the decoding. |
| 151 | fn decode_bencode_objectrust-analyzer cargo bendy 0.6.1 impl#[Info][FromBencode]decode_bencode_object().(object: Objectrust-analyzer cargo bendy 0.6.1 decoding/object/Object#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo bendy 0.6.1 impl#[Info][FromBencode], Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#> |
| 152 | where |
| 153 | Selfrust-analyzer cargo bendy 0.6.1 impl#[Info][FromBencode]: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#, |
| 154 | { |
| 155 | let mut file_length = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 156 | let mut name = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 157 | let mut piece_length = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 158 | let mut pieces = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 159 | |
| 160 | let mut dict_dec = object.try_into_dictionaryrust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]try_into_dictionary().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 161 | while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(pair) = dict_dec.next_pairrust-analyzer cargo bendy 0.6.1 decoding/decoder/impl#[`DictDecoder<'obj, 'ser>`]next_pair().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). { |
| 162 | match pair { |
| 163 | (b"length", value) => { |
| 164 | file_length = value |
| 165 | .try_into_integerrust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]try_into_integer().() |
| 166 | .contextrust-analyzer cargo bendy 0.6.1 decoding/error/impl#[`Result<T, Error>`][ResultExt]context().("file.length") |
| 167 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(ToStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/ToString#::to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().) |
| 168 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 169 | }, |
| 170 | (b"name", value) => { |
| 171 | name = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::decode_bencode_objectrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/impl#[String][FromBencode]decode_bencode_object().(value) |
| 172 | .contextrust-analyzer cargo bendy 0.6.1 decoding/error/impl#[`Result<T, Error>`][ResultExt]context().("name") |
| 173 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 174 | }, |
| 175 | (b"piece length", value) => { |
| 176 | piece_length = value |
| 177 | .try_into_integerrust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]try_into_integer().() |
| 178 | .contextrust-analyzer cargo bendy 0.6.1 decoding/error/impl#[`Result<T, Error>`][ResultExt]context().("length") |
| 179 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(ToStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/ToString#::to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().) |
| 180 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 181 | }, |
| 182 | (b"pieces", value) => { |
| 183 | pieces = AsStringrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/AsString#::decode_bencode_objectrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/impl#[`AsString<Vec<u8>>`][FromBencode]decode_bencode_object().(value) |
| 184 | .contextrust-analyzer cargo bendy 0.6.1 decoding/error/impl#[`Result<T, Error>`][ResultExt]context().("pieces") |
| 185 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(|bytes| Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(bytes.0rust-analyzer cargo bendy 0.6.1 encoding/to_bencode/AsString#0.))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 186 | }, |
| 187 | (unknown_field, _) => { |
| 188 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#::unexpected_fieldrust-analyzer cargo bendy 0.6.1 decoding/error/impl#[Error]unexpected_field().(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::from_utf8_lossyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]from_utf8_lossy().( |
| 189 | unknown_field, |
| 190 | ))); |
| 191 | }, |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | let file_length = file_length.ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#::missing_fieldrust-analyzer cargo bendy 0.6.1 decoding/error/impl#[Error]missing_field().("file_length"))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 196 | let name = name.ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#::missing_fieldrust-analyzer cargo bendy 0.6.1 decoding/error/impl#[Error]missing_field().("name"))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 197 | let piece_length = piece_length.ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#::missing_fieldrust-analyzer cargo bendy 0.6.1 decoding/error/impl#[Error]missing_field().("piece_length"))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 198 | let pieces = pieces.ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#::missing_fieldrust-analyzer cargo bendy 0.6.1 decoding/error/impl#[Error]missing_field().("pieces"))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 199 | |
| 200 | // Check that we discovered all necessary fields |
| 201 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Inforust-analyzer cargo bendy 0.6.1 Info# { |
| 202 | file_lengthrust-analyzer cargo bendy 0.6.1 Info#file_length., |
| 203 | namerust-analyzer cargo bendy 0.6.1 Info#name., |
| 204 | piece_lengthrust-analyzer cargo bendy 0.6.1 Info#piece_length., |
| 205 | piecesrust-analyzer cargo bendy 0.6.1 Info#pieces., |
| 206 | }) |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | fn mainrust-analyzer cargo bendy 0.6.1 main().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#> { |
| 211 | let torrent = MetaInforust-analyzer cargo bendy 0.6.1 MetaInfo#::from_bencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#from_bencode().(EXAMPLE_TORRENTrust-analyzer cargo bendy 0.6.1 EXAMPLE_TORRENT.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 212 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("{:#?}", torrent); |
| 213 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 214 | } |