Debian Codegraph @

tests/struct_codec.rs

raw symbols SCIP
1 use bendyrust-analyzer cargo bendy 0.6.1 crate/::{
2 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#},
3 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#},
4 };
5
6 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
7 struct Examplerust-analyzer cargo bendy 0.6.1 Example# {
8 foorust-analyzer cargo bendy 0.6.1 Example#foo.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<i64>,
9 barrust-analyzer cargo bendy 0.6.1 Example#bar.: i64,
10 }
11
12 impl ToBencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode# for Examplerust-analyzer cargo bendy 0.6.1 Example# {
13 const MAX_DEPTHrust-analyzer cargo bendy 0.6.1 impl#[Example][ToBencode]MAX_DEPTH.: usize = 2;
14
15 fn encoderust-analyzer cargo bendy 0.6.1 impl#[Example][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#> {
16 encoder.emit_dictrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SingleItemEncoder<'a>`]emit_dict().(|mut dict| {
17 dict.emit_pairrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SortedDictEncoder<'a>`]emit_pair().(b"bar", &self.barrust-analyzer cargo bendy 0.6.1 Example#bar.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
18 dict.emit_pairrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SortedDictEncoder<'a>`]emit_pair().(b"foo", &self.foorust-analyzer cargo bendy 0.6.1 Example#foo.)
19 })
20 }
21 }
22
23 impl FromBencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode# for Examplerust-analyzer cargo bendy 0.6.1 Example# {
24 const EXPECTED_RECURSION_DEPTHrust-analyzer cargo bendy 0.6.1 impl#[Example][FromBencode]EXPECTED_RECURSION_DEPTH.: usize = 2;
25
26 fn decode_bencode_objectrust-analyzer cargo bendy 0.6.1 impl#[Example][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#[Example][FromBencode], DecodingErrorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#>
27 where
28 Selfrust-analyzer cargo bendy 0.6.1 impl#[Example][FromBencode]: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#,
29 {
30 let mut foo = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
31 let mut bar = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
32
33 let mut dict = 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().;
34 while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((key, value)) = dict.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(). {
35 match key {
36 b"foo" => {
37 foo = 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).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().;
38 },
39 b"bar" => {
40 bar = i64::decode_bencode_objectrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/impl#[i64][FromBencode]decode_bencode_object().(value).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().;
41 },
42 _ => (), // ignore unknown keys
43 }
44 }
45
46 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Examplerust-analyzer cargo bendy 0.6.1 Example# {
47 foorust-analyzer cargo bendy 0.6.1 Example#foo.: foo.ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| DecodingErrorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#::missing_fieldrust-analyzer cargo bendy 0.6.1 decoding/error/impl#[Error]missing_field().("foo"))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
48 barrust-analyzer cargo bendy 0.6.1 Example#bar.: bar.ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| DecodingErrorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#::missing_fieldrust-analyzer cargo bendy 0.6.1 decoding/error/impl#[Error]missing_field().("bar"))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
49 })
50 }
51 }
52
53 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
54 fn should_encode_structrust-analyzer cargo bendy 0.6.1 should_encode_struct().() {
55 let example = Examplerust-analyzer cargo bendy 0.6.1 Example# {
56 foorust-analyzer cargo bendy 0.6.1 Example#foo.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![2, 3],
57 barrust-analyzer cargo bendy 0.6.1 Example#bar.: 1,
58 };
59 let encoded = example.to_bencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#to_bencode().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("example encoding is broken");
60
61 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(encoded, b"d3:bari1e3:fooli2ei3eee".to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().(),)
62 }
63
64 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
65 fn should_decode_structrust-analyzer cargo bendy 0.6.1 should_decode_struct().() {
66 let encoded = b"d3:bari1e3:fooli2ei3eee".to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().();
67 let example = Examplerust-analyzer cargo bendy 0.6.1 Example#::from_bencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#from_bencode().(&encoded).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("example decoding is broken");
68
69 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
70 example,
71 Examplerust-analyzer cargo bendy 0.6.1 Example# {
72 foorust-analyzer cargo bendy 0.6.1 Example#foo.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![2, 3],
73 barrust-analyzer cargo bendy 0.6.1 Example#bar.: 1,
74 }
75 )
76 }
77
78 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
79 fn validate_round_triprust-analyzer cargo bendy 0.6.1 validate_round_trip().() {
80 let example = Examplerust-analyzer cargo bendy 0.6.1 Example# {
81 foorust-analyzer cargo bendy 0.6.1 Example#foo.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![2, 3],
82 barrust-analyzer cargo bendy 0.6.1 Example#bar.: 1,
83 };
84
85 let encoded = example.to_bencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#to_bencode().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("example encoding is broken");
86 let decoded = Examplerust-analyzer cargo bendy 0.6.1 Example#::from_bencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#from_bencode().(&encoded).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("example decoding is broken");
87
88 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(example, decoded);
89 }