Debian Codegraph @

tests/core_test.rs

raw symbols SCIP
1 //! Port of https://github.com/jamesleonis/bencode-cljc/blob/master/test/bencode_cljc/core_test.cljc
2 //!
3 //! Should only use #![no_std] compatible features but still requires the
4 //! `std` feature flag to avoid that we need to define a global allocator.
5
6 extern crate allocrust-analyzer cargo bendy 0.6.1 alloc/;
7 use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::collectionsrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/::BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#;
8
9 use bendyrust-analyzer cargo bendy 0.6.1 crate/::{
10 decodingrust-analyzer cargo bendy 0.6.1 decoding/::{Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error# as DecodingErrorrust-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#},
11 encodingrust-analyzer cargo bendy 0.6.1 encoding/::{Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error# as EncodingErrorrust-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#},
12 };
13
14 // -----------------------------------------------------------------------------
15 // Macros
16 // -----------------------------------------------------------------------------
17
18 macro_rules! listrust-analyzer cargo bendy 0.6.1 list!(
19 {} => { Vec::<Something>::new() };
20 { $($value:expr),+ } => {
21 {
22 let mut list = Vec::new();
23 $( list.push(Something::from($value)); )+
24
25 list
26 }
27 };
28 );
29
30 macro_rules! maprust-analyzer cargo bendy 0.6.1 map!(
31 { $($key:expr => $value:expr),+ } => {
32 {
33 let mut map = BTreeMap::new();
34 $( map.insert($key.to_owned(), Something::from($value)); )+
35
36 map
37 }
38 };
39 );
40
41 // -----------------------------------------------------------------------------
42 // Tests
43 // -----------------------------------------------------------------------------
44
45 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
46 fn string_test_pairsrust-analyzer cargo bendy 0.6.1 string_test_pairs().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 Error#> {
47 let pairs = [
48 ("", "0:"),
49 ("hello", "5:hello"),
50 ("goodbye", "7:goodbye"),
51 ("hello world", "11:hello world"),
52 ("1-5%3~]+=\\| []>.,`??", "20:1-5%3~]+=\\| []>.,`??"),
53 ];
54
55 for (original, expected_encoding) in &pairs {
56 let encoded = original.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().;
57 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(expected_encoding.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().(), encoded.as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().());
58
59 let decoded = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::from_bencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#from_bencode().(&encoded)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
60 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(original, &decoded);
61 }
62
63 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
64 }
65
66 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
67 fn integer_test_pairsrust-analyzer cargo bendy 0.6.1 integer_test_pairs().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 Error#> {
68 let pairs = [
69 (0, "i0e"),
70 (5, "i5e"),
71 (-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][Neg]neg().5, "i-5e"),
72 (005, "i5e"),
73 (-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][Neg]neg().005, "i-5e"),
74 (1234567890, "i1234567890e"),
75 (-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][Neg]neg().1234567890, "i-1234567890e"),
76 (i64::max_valuerust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i64]max_value().(), "i9223372036854775807e"),
77 (i64::min_valuerust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i64]min_value().(), "i-9223372036854775808e"),
78 ];
79 // Bendy currently doesn't contain a big number implementation..
80 //
81 // (
82 // 123456789012345678901234567890123456789012345678901234567890,
83 // "i123456789012345678901234567890123456789012345678901234567890e"
84 // ),
85 // (
86 // -123456789012345678901234567890123456789012345678901234567890,
87 // "i-123456789012345678901234567890123456789012345678901234567890e"
88 // )
89
90 for (original, expected_encoding) in &pairs {
91 let encoded = original.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().;
92 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(expected_encoding.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().(), encoded.as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().());
93
94 let decoded = i64::from_bencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#from_bencode().(&encoded)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
95 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(original, &decoded);
96 }
97
98 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
99 }
100
101 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
102 fn list_test_pairsrust-analyzer cargo bendy 0.6.1 list_test_pairs().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 Error#> {
103 let pairs = [
104 (listrust-analyzer cargo bendy 0.6.1 list!![], "le"),
105 (listrust-analyzer cargo bendy 0.6.1 list!!["abra", "cadabra"], "l4:abra7:cadabrae"),
106 (listrust-analyzer cargo bendy 0.6.1 list!!["spam", "eggs"], "l4:spam4:eggse"),
107 (
108 listrust-analyzer cargo bendy 0.6.1 list!![vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["list", "of", "lists"], vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["like", "omygawd!"]],
109 "ll4:list2:of5:listsel4:like8:omygawd!ee",
110 ),
111 ];
112
113 for (original, expected_encoding) in &pairs {
114 let encoded = original.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().;
115 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(expected_encoding.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().(), encoded.as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().());
116
117 let decoded = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::<Somethingrust-analyzer cargo bendy 0.6.1 Something#>::from_bencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#from_bencode().(&encoded)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
118 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(original, &decoded);
119 }
120
121 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
122 }
123
124 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
125 fn map_test_pairsrust-analyzer cargo bendy 0.6.1 map_test_pairs().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 Error#> {
126 let pairs = [
127 (BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V>`]new().(), "de"),
128 (
129 maprust-analyzer cargo bendy 0.6.1 map!! {"cow" => "moo", "spam" => "eggs"},
130 "d3:cow3:moo4:spam4:eggse",
131 ),
132 (
133 maprust-analyzer cargo bendy 0.6.1 map!! {"cow" => "moo", "dog" => "bark"},
134 "d3:cow3:moo3:dog4:barke",
135 ),
136 (
137 maprust-analyzer cargo bendy 0.6.1 map!! {"dog" => "bark", "cow" => "moo"},
138 "d3:cow3:moo3:dog4:barke",
139 ),
140 (
141 maprust-analyzer cargo bendy 0.6.1 map!! {"first" => "first", "2ace" => "second", "3ace" => "third"},
142 "d4:2ace6:second4:3ace5:third5:first5:firste",
143 ),
144 (
145 maprust-analyzer cargo bendy 0.6.1 map!! {"Goodbye" => maprust-analyzer cargo bendy 0.6.1 map!! {"maps" => "that don't work", "number" => 100}},
146 "d7:Goodbyed4:maps15:that don't work6:numberi100eee",
147 ),
148 (
149 maprust-analyzer cargo bendy 0.6.1 map!! {
150 "publisher" => "bob", "publisher-webpage" => "www.example.com",
151 "publisher.location" => "home"
152 },
153 "d9:publisher3:bob17:publisher-webpage15:www.example.com18:publisher.location4:homee",
154 ),
155 ];
156
157 for (original, expected_encoding) in &pairs {
158 let encoded = original.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().;
159 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(expected_encoding.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().(), encoded.as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().());
160
161 let decoded = BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#::<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Somethingrust-analyzer cargo bendy 0.6.1 Something#>::from_bencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#from_bencode().(&encoded)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
162 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(original, &decoded);
163 }
164
165 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
166 }
167
168 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
169 fn mixed_use_list_pairsrust-analyzer cargo bendy 0.6.1 mixed_use_list_pairs().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 Error#> {
170 let pairs = [(
171 listrust-analyzer cargo bendy 0.6.1 list!![0, "heterogeneous", -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][Neg]neg().5, "lists", 10, maprust-analyzer cargo bendy 0.6.1 map!! {"map" => "well"}],
172 "li0e13:heterogeneousi-5e5:listsi10ed3:map4:wellee",
173 )];
174
175 for (original, expected_encoding) in &pairs {
176 let encoded = original.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().;
177 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(expected_encoding.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().(), encoded.as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().());
178
179 let decoded = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::<Somethingrust-analyzer cargo bendy 0.6.1 Something#>::from_bencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#from_bencode().(&encoded)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
180 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(original, &decoded);
181 }
182
183 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
184 }
185
186 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
187 fn mixed_use_dict_pairsrust-analyzer cargo bendy 0.6.1 mixed_use_dict_pairs().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 Error#> {
188 let pairs = [
189 (
190 maprust-analyzer cargo bendy 0.6.1 map!! {
191 "hello" => listrust-analyzer cargo bendy 0.6.1 list!!["world!", "gaia!", "mother earth!"],
192 "Goodbye" => maprust-analyzer cargo bendy 0.6.1 map!! {"maps" => "that don't work", "number" => 100}
193 },
194 "d7:Goodbyed4:maps15:that don't work6:numberi100ee5:hellol6:world!5:gaia!13:mother earth!ee",
195 ),
196 (
197 maprust-analyzer cargo bendy 0.6.1 map!! {"hello" => listrust-analyzer cargo bendy 0.6.1 list!!["world!", "gaia!", "mother earth!"]},
198 "d5:hellol6:world!5:gaia!13:mother earth!ee",
199 ),
200 (maprust-analyzer cargo bendy 0.6.1 map!! {"spam" => listrust-analyzer cargo bendy 0.6.1 list!!["a", "b"]}, "d4:spaml1:a1:bee"),
201 (
202 maprust-analyzer cargo bendy 0.6.1 map!! {
203 "t" => "aa", "y" => "q", "q" => "ping",
204 "a" => maprust-analyzer cargo bendy 0.6.1 map!! { "id" => "abcdefghij0123456789" }
205 },
206 "d1:ad2:id20:abcdefghij0123456789e1:q4:ping1:t2:aa1:y1:qe",
207 ),
208 (
209 maprust-analyzer cargo bendy 0.6.1 map!! {
210 "t" => "aa", "y" => "q", "q" => "find_node",
211 "a" => maprust-analyzer cargo bendy 0.6.1 map!! { "id" => "abcdefghij0123456789", "target" => "mnopqrstuvwxyz123456" }
212 },
213 "d1:ad2:id20:abcdefghij01234567896:target20:mnopqrstuvwxyz123456e1:q9:find_node1:t2:aa1:y1:qe",
214 ),
215 (
216 maprust-analyzer cargo bendy 0.6.1 map!! {
217 "t" => "aa", "y" => "q", "q" => "get_peers",
218 "a" => maprust-analyzer cargo bendy 0.6.1 map!! { "id" => "abcdefghij0123456789", "info_hash" => "mnopqrstuvwxyz123456" }
219 },
220 "d1:ad2:id20:abcdefghij01234567899:info_hash20:mnopqrstuvwxyz123456e1:q9:get_peers1:t2:aa1:y1:qe",
221 ),
222 (
223 maprust-analyzer cargo bendy 0.6.1 map!! {
224 "t" => "aa",
225 "y" => "r",
226 "r" => maprust-analyzer cargo bendy 0.6.1 map!! {
227 "id" => "abcdefghij0123456789",
228 "token" => "aoeusnth", "values" => vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["axje.u", "idhtnm"]
229 }
230 },
231 "d1:rd2:id20:abcdefghij01234567895:token8:aoeusnth6:valuesl6:axje.u6:idhtnmee1:t2:aa1:y1:re",
232 ),
233 ];
234
235 for (original, expected_encoding) in &pairs {
236 let encoded = original.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().;
237 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(expected_encoding.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().(), encoded.as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().());
238
239 let decoded = BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#::<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Somethingrust-analyzer cargo bendy 0.6.1 Something#>::from_bencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#from_bencode().(&encoded)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
240 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(original, &decoded);
241 }
242
243 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
244 }
245
246 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
247 fn illegal_integer_encodingsrust-analyzer cargo bendy 0.6.1 illegal_integer_encodings().() {
248 let values = [
249 "i-0e", "i09e", "i-09e", "i-0123e", "i-00123e", "i0123e", "i00123e", "i12-345", "i-12-345",
250 "i-1", "i1",
251 ];
252 // Bendy currently doesn't fail if it encounters unused tokens
253 //
254 // "i12345ei10e5:eoeoee",
255 // "i-12345ei10e5:eoeoee"
256
257 for value in &values {
258 let error = i64::from_bencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#from_bencode().(value.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().()).unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().();
259 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(error.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]contains().("encoding corrupted"));
260 }
261 }
262
263 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
264 fn illegal_string_encodingsrust-analyzer cargo bendy 0.6.1 illegal_string_encodings().() {
265 let values = [":hello", "-5:hello", "-5:", "5:", "10:hello"];
266 // Bendy currently doesn't fail if it encounters unused tokens
267 //
268 // "5:hello5:hello",
269 // "5:helloi10e",
270 // 10:hello5:hello",
271 // "10:helloi0e",
272 // "10:helloi123456789e"
273
274 for value in &values {
275 let error = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::from_bencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#from_bencode().(value.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().()).unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().();
276 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(error.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]contains().("encoding corrupted"));
277 }
278 }
279
280 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
281 fn illegal_list_encodingsrust-analyzer cargo bendy 0.6.1 illegal_list_encodings().() {
282 let values = [
283 "l",
284 "lsde",
285 "li10e5hello",
286 "l10:helloi123456789ee",
287 "l10:helloi123456789e5:helloe",
288 "l5:helloi123456789e10:helloe",
289 ];
290 // Bendy currently doesn't fail if it encounters unused tokens
291 //
292 // "l5:hello5:worldei10e",
293
294 for value in &values {
295 let error = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::<Somethingrust-analyzer cargo bendy 0.6.1 Something#>::from_bencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#from_bencode().(value.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().()).unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().();
296 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(error.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]contains().("encoding corrupted"));
297 }
298 }
299
300 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
301 fn illegal_dictionary_encodingsrust-analyzer cargo bendy 0.6.1 illegal_dictionary_encodings().() {
302 let values = [
303 "d",
304 "duuuuure",
305 "d5:hello5:world",
306 "d10:helloi123456789ee",
307 "d5:helloi123456789e5:helloe",
308 "di10e5:hello5:worldi10ee",
309 "d5:worldi10ei10e5:helloe",
310 "dle5:hello5:worldi10ee",
311 "dli10ei11ee5:hello5:worldi10ee",
312 "dde5:hello5:worldi10ee",
313 "dd8:innermapi11ee5:hello5:worldi10ee",
314 ];
315 // Bendy currently doesn't fail if it encounters unused tokens
316 //
317 // "d5:hello5:worldei10e",
318
319 for value in &values {
320 let error = BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#::<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Somethingrust-analyzer cargo bendy 0.6.1 Something#>::from_bencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#from_bencode().(value.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().()).unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().();
321 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(error.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]contains().("encoding corrupted"));
322 }
323 }
324
325 // -----------------------------------------------------------------------------
326 // Dynamic Typing Utility
327 // -----------------------------------------------------------------------------
328
329 #[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:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)]
330 enum Somethingrust-analyzer cargo bendy 0.6.1 Something# {
331 Bytesrust-analyzer cargo bendy 0.6.1 Something#Bytes#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#),
332 Dictrust-analyzer cargo bendy 0.6.1 Something#Dict#(BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Somethingrust-analyzer cargo bendy 0.6.1 Something#>),
333 Integerrust-analyzer cargo bendy 0.6.1 Something#Integer#(i64),
334 Listrust-analyzer cargo bendy 0.6.1 Something#List#(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Somethingrust-analyzer cargo bendy 0.6.1 Something#>),
335 }
336
337 impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<&str> for Somethingrust-analyzer cargo bendy 0.6.1 Something# {
338 fn fromrust-analyzer cargo bendy 0.6.1 impl#[Something][`From<&str>`]from().(content: &str) -> Selfrust-analyzer cargo bendy 0.6.1 impl#[Something][`From<&str>`] {
339 Somethingrust-analyzer cargo bendy 0.6.1 Something#::Bytesrust-analyzer cargo bendy 0.6.1 Something#Bytes#(content.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().())
340 }
341 }
342
343 impl<ContentT> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, ContentT>> for Somethingrust-analyzer cargo bendy 0.6.1 Something#
344 where
345 Somethingrust-analyzer cargo bendy 0.6.1 Something#: Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<ContentT>,
346 {
347 fn fromrust-analyzer cargo bendy 0.6.1 impl#[Something][`From<BTreeMap<String, ContentT>>`]from().(content: BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, ContentT>) -> Selfrust-analyzer cargo bendy 0.6.1 impl#[Something][`From<BTreeMap<String, ContentT>>`] {
348 let content = content
349 .into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`][IntoIterator]into_iter().()
350 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(key, value)| (key, value.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()))
351 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
352
353 Somethingrust-analyzer cargo bendy 0.6.1 Something#::Dictrust-analyzer cargo bendy 0.6.1 Something#Dict#(content)
354 }
355 }
356
357 impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<i64> for Somethingrust-analyzer cargo bendy 0.6.1 Something# {
358 fn fromrust-analyzer cargo bendy 0.6.1 impl#[Something][`From<i64>`]from().(content: i64) -> Selfrust-analyzer cargo bendy 0.6.1 impl#[Something][`From<i64>`] {
359 Somethingrust-analyzer cargo bendy 0.6.1 Something#::Integerrust-analyzer cargo bendy 0.6.1 Something#Integer#(content)
360 }
361 }
362
363 impl<ContentT> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<ContentT>> for Somethingrust-analyzer cargo bendy 0.6.1 Something#
364 where
365 Somethingrust-analyzer cargo bendy 0.6.1 Something#: Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<ContentT>,
366 {
367 fn fromrust-analyzer cargo bendy 0.6.1 impl#[Something][`From<Vec<ContentT>>`]from().(content: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<ContentT>) -> Selfrust-analyzer cargo bendy 0.6.1 impl#[Something][`From<Vec<ContentT>>`] {
368 let content = content.into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#::intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
369 Somethingrust-analyzer cargo bendy 0.6.1 Something#::Listrust-analyzer cargo bendy 0.6.1 Something#List#(content)
370 }
371 }
372
373 impl FromBencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode# for Somethingrust-analyzer cargo bendy 0.6.1 Something# {
374 fn decode_bencode_objectrust-analyzer cargo bendy 0.6.1 impl#[Something][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#[Something][FromBencode], DecodingErrorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#>
375 where
376 Selfrust-analyzer cargo bendy 0.6.1 impl#[Something][FromBencode]: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#,
377 {
378 let something = match object {
379 Objectrust-analyzer cargo bendy 0.6.1 decoding/object/Object#::Bytesrust-analyzer cargo bendy 0.6.1 decoding/object/Object#Bytes#(content) => {
380 Somethingrust-analyzer cargo bendy 0.6.1 Something#::Bytesrust-analyzer cargo bendy 0.6.1 Something#Bytes#(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().(content).to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())
381 },
382 Objectrust-analyzer cargo bendy 0.6.1 decoding/object/Object#::Integerrust-analyzer cargo bendy 0.6.1 decoding/object/Object#Integer#(number) => Somethingrust-analyzer cargo bendy 0.6.1 Something#::Integerrust-analyzer cargo bendy 0.6.1 Something#Integer#(number.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()),
383 object @ Objectrust-analyzer cargo bendy 0.6.1 decoding/object/Object#::Dictrust-analyzer cargo bendy 0.6.1 decoding/object/Object#Dict#(_) => {
384 Somethingrust-analyzer cargo bendy 0.6.1 Something#::Dictrust-analyzer cargo bendy 0.6.1 Something#Dict#(BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#::decode_bencode_objectrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/impl#[`BTreeMap<K, V>`][FromBencode]decode_bencode_object().(object).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().())
385 },
386 object @ Objectrust-analyzer cargo bendy 0.6.1 decoding/object/Object#::Listrust-analyzer cargo bendy 0.6.1 decoding/object/Object#List#(_) => {
387 Somethingrust-analyzer cargo bendy 0.6.1 Something#::Listrust-analyzer cargo bendy 0.6.1 Something#List#(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().(object).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().())
388 },
389 };
390
391 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(something)
392 }
393 }
394
395 impl ToBencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode# for Somethingrust-analyzer cargo bendy 0.6.1 Something# {
396 const MAX_DEPTHrust-analyzer cargo bendy 0.6.1 impl#[Something][ToBencode]MAX_DEPTH.: usize = 999;
397
398 fn encoderust-analyzer cargo bendy 0.6.1 impl#[Something][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#<(), EncodingErrorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> {
399 match self {
400 Somethingrust-analyzer cargo bendy 0.6.1 Something#::Bytesrust-analyzer cargo bendy 0.6.1 Something#Bytes#(content) => encoder.emitrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SingleItemEncoder<'a>`]emit().(content),
401 Somethingrust-analyzer cargo bendy 0.6.1 Something#::Dictrust-analyzer cargo bendy 0.6.1 Something#Dict#(content) => encoder.emitrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SingleItemEncoder<'a>`]emit().(content),
402 Somethingrust-analyzer cargo bendy 0.6.1 Something#::Integerrust-analyzer cargo bendy 0.6.1 Something#Integer#(content) => encoder.emitrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SingleItemEncoder<'a>`]emit().(content),
403 Somethingrust-analyzer cargo bendy 0.6.1 Something#::Listrust-analyzer cargo bendy 0.6.1 Something#List#(content) => encoder.emitrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SingleItemEncoder<'a>`]emit().(content),
404 }
405 }
406 }
407
408 // -----------------------------------------------------------------------------
409 // Error
410 // -----------------------------------------------------------------------------
411
412 #[allow(dead_code)]
413 #[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:)]
414 enum Errorrust-analyzer cargo bendy 0.6.1 Error# {
415 DecodingErrorrust-analyzer cargo bendy 0.6.1 Error#DecodingError#(DecodingErrorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#),
416 EncodingErrorrust-analyzer cargo bendy 0.6.1 Error#EncodingError#(EncodingErrorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#),
417 }
418
419 impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<DecodingErrorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#> for Errorrust-analyzer cargo bendy 0.6.1 Error# {
420 fn fromrust-analyzer cargo bendy 0.6.1 impl#[Error][`From<DecodingError>`]from().(error: DecodingErrorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#) -> Selfrust-analyzer cargo bendy 0.6.1 impl#[Error][`From<DecodingError>`] {
421 Errorrust-analyzer cargo bendy 0.6.1 Error#::DecodingErrorrust-analyzer cargo bendy 0.6.1 Error#DecodingError#(error)
422 }
423 }
424
425 impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<EncodingErrorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> for Errorrust-analyzer cargo bendy 0.6.1 Error# {
426 fn fromrust-analyzer cargo bendy 0.6.1 impl#[Error][`From<EncodingError>`]from().(error: EncodingErrorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#) -> Selfrust-analyzer cargo bendy 0.6.1 impl#[Error][`From<EncodingError>`] {
427 Errorrust-analyzer cargo bendy 0.6.1 Error#::EncodingErrorrust-analyzer cargo bendy 0.6.1 Error#EncodingError#(error)
428 }
429 }