| 1 | #[cfg(not(feature = "std"))] |
| 2 | 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#, rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/::Rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/Rc#, stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/::Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/::Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#}; |
| 3 | |
| 4 | #[cfg(feature = "std")] |
| 5 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{ |
| 6 | collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::{BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#, HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#}, |
| 7 | hashrust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/::{BuildHasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/BuildHasher#, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash#}, |
| 8 | rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/::Rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/Rc#, |
| 9 | }; |
| 10 | |
| 11 | use craterust-analyzer cargo bendy 0.6.1 crate/::{ |
| 12 | decodingrust-analyzer cargo bendy 0.6.1 decoding/::{Decoderrust-analyzer cargo bendy 0.6.1 decoding/decoder/Decoder#, Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#, Objectrust-analyzer cargo bendy 0.6.1 decoding/object/Object#}, |
| 13 | encodingrust-analyzer cargo bendy 0.6.1 encoding/::AsStringrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/AsString#, |
| 14 | state_trackerrust-analyzer cargo bendy 0.6.1 state_tracker/::StructureErrorrust-analyzer cargo bendy 0.6.1 state_tracker/structure_error/StructureError#, |
| 15 | }; |
| 16 | |
| 17 | /// Basic trait for bencode based value deserialization. |
| 18 | pub trait FromBencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode# { |
| 19 | /// Maximum allowed depth of nested structures before the decoding should be aborted. |
| 20 | const EXPECTED_RECURSION_DEPTHrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#EXPECTED_RECURSION_DEPTH.: usize = 2048; |
| 21 | |
| 22 | /// Deserialize an object from its byte representation. |
| 23 | fn from_bencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#from_bencode().(bytes: &[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Self, Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#> |
| 24 | where |
| 25 | Self: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#, |
| 26 | { |
| 27 | let mut decoder = Decoderrust-analyzer cargo bendy 0.6.1 decoding/decoder/Decoder#::newrust-analyzer cargo bendy 0.6.1 decoding/decoder/impl#[`Decoder<'ser>`]new().(bytes).with_max_depthrust-analyzer cargo bendy 0.6.1 decoding/decoder/impl#[`Decoder<'ser>`]with_max_depth().(Self::EXPECTED_RECURSION_DEPTHrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#EXPECTED_RECURSION_DEPTH.); |
| 28 | let object = decoder.next_objectrust-analyzer cargo bendy 0.6.1 decoding/decoder/impl#[`Decoder<'ser>`]next_object().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 29 | |
| 30 | object.map_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map_or().( |
| 31 | 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#::fromrust-analyzer cargo bendy 0.6.1 decoding/error/impl#[Error][`From<state_tracker::StructureError>`]from().(StructureErrorrust-analyzer cargo bendy 0.6.1 state_tracker/structure_error/StructureError#::UnexpectedEofrust-analyzer cargo bendy 0.6.1 state_tracker/structure_error/StructureError#UnexpectedEof#)), |
| 32 | Self::decode_bencode_objectrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#decode_bencode_object()., |
| 33 | ) |
| 34 | } |
| 35 | |
| 36 | /// Deserialize an object from its intermediate bencode representation. |
| 37 | fn decode_bencode_objectrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/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#<Self, Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#> |
| 38 | where |
| 39 | Self: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#; |
| 40 | } |
| 41 | |
| 42 | macro_rules! impl_from_bencode_for_integerrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/impl_from_bencode_for_integer! { |
| 43 | ($($type:ty)*) => {$( |
| 44 | impl FromBencode for $type { |
| 45 | const EXPECTED_RECURSION_DEPTH: usize = 0; |
| 46 | |
| 47 | fn decode_bencode_object(object: Object) -> Result<Self, Error> |
| 48 | where |
| 49 | Self: Sized, |
| 50 | { |
| 51 | let content = object.try_into_integer()?; |
| 52 | let number = content.parse::<$type>()?; |
| 53 | |
| 54 | Ok(number) |
| 55 | } |
| 56 | } |
| 57 | )*} |
| 58 | } |
| 59 | |
| 60 | impl_from_bencode_for_integerrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/impl_from_bencode_for_integer!!(u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize); |
| 61 | |
| 62 | impl<ContentT: FromBencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#> FromBencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode# for Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<ContentT> { |
| 63 | const EXPECTED_RECURSION_DEPTHrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/impl#[`Vec<ContentT>`][FromBencode]EXPECTED_RECURSION_DEPTH.: usize = ContentT::EXPECTED_RECURSION_DEPTHrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/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; |
| 64 | |
| 65 | fn decode_bencode_objectrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/impl#[`Vec<ContentT>`][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 decoding/from_bencode/impl#[`Vec<ContentT>`][FromBencode], Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#> |
| 66 | where |
| 67 | Selfrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/impl#[`Vec<ContentT>`][FromBencode]: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#, |
| 68 | { |
| 69 | let mut list = object.try_into_listrust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]try_into_list().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 70 | let mut results = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 71 | |
| 72 | while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(object) = list.next_objectrust-analyzer cargo bendy 0.6.1 decoding/decoder/impl#[`ListDecoder<'obj, 'ser>`]next_object().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). { |
| 73 | let item = ContentT::decode_bencode_objectrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#decode_bencode_object().(object)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 74 | results.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(item); |
| 75 | } |
| 76 | |
| 77 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(results) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | impl FromBencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode# for Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 82 | const EXPECTED_RECURSION_DEPTHrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/impl#[String][FromBencode]EXPECTED_RECURSION_DEPTH.: usize = 0; |
| 83 | |
| 84 | fn decode_bencode_objectrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/impl#[String][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 decoding/from_bencode/impl#[String][FromBencode], Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#> |
| 85 | where |
| 86 | Selfrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/impl#[String][FromBencode]: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#, |
| 87 | { |
| 88 | let content = object.try_into_bytesrust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]try_into_bytes().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 89 | let content = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::from_utf8rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]from_utf8().(content.to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 90 | |
| 91 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(content) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | impl<K, V> FromBencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode# for BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#<K, V> |
| 96 | where |
| 97 | K: FromBencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode# + Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#, |
| 98 | V: FromBencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#, |
| 99 | { |
| 100 | const EXPECTED_RECURSION_DEPTHrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/impl#[`BTreeMap<K, V>`][FromBencode]EXPECTED_RECURSION_DEPTH.: usize = V::EXPECTED_RECURSION_DEPTHrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/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; |
| 101 | |
| 102 | fn decode_bencode_objectrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/impl#[`BTreeMap<K, V>`][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 decoding/from_bencode/impl#[`BTreeMap<K, V>`][FromBencode], Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#> |
| 103 | where |
| 104 | Selfrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/impl#[`BTreeMap<K, V>`][FromBencode]: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#, |
| 105 | { |
| 106 | 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().; |
| 107 | let mut result = BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#::defaultrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V>`][Default]default().(); |
| 108 | |
| 109 | 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(). { |
| 110 | let key = K::decode_bencode_objectrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#decode_bencode_object().(Objectrust-analyzer cargo bendy 0.6.1 decoding/object/Object#::Bytesrust-analyzer cargo bendy 0.6.1 decoding/object/Object#Bytes#(key))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 111 | let value = V::decode_bencode_objectrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#decode_bencode_object().(value)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 112 | |
| 113 | result.insertrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]insert().(key, value); |
| 114 | } |
| 115 | |
| 116 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(result) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | #[cfg(feature = "std")] |
| 121 | impl<K, V, H> FromBencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode# for HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<K, V, H> |
| 122 | where |
| 123 | K: FromBencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode# + Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash# + Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq#, |
| 124 | V: FromBencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#, |
| 125 | H: BuildHasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/BuildHasher# + Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#, |
| 126 | { |
| 127 | const EXPECTED_RECURSION_DEPTHrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/impl#[`HashMap<K, V, H>`][FromBencode]EXPECTED_RECURSION_DEPTH.: usize = V::EXPECTED_RECURSION_DEPTHrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/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; |
| 128 | |
| 129 | fn decode_bencode_objectrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/impl#[`HashMap<K, V, H>`][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 decoding/from_bencode/impl#[`HashMap<K, V, H>`][FromBencode], Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#> |
| 130 | where |
| 131 | Selfrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/impl#[`HashMap<K, V, H>`][FromBencode]: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#, |
| 132 | { |
| 133 | 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().; |
| 134 | let mut result = HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#::defaultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S>`][Default]default().(); |
| 135 | |
| 136 | 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(). { |
| 137 | let key = K::decode_bencode_objectrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#decode_bencode_object().(Objectrust-analyzer cargo bendy 0.6.1 decoding/object/Object#::Bytesrust-analyzer cargo bendy 0.6.1 decoding/object/Object#Bytes#(key))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 138 | let value = V::decode_bencode_objectrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#decode_bencode_object().(value)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 139 | |
| 140 | result.insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]insert().(key, value); |
| 141 | } |
| 142 | |
| 143 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(result) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | impl<T: FromBencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#> FromBencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode# for Rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/Rc#<T> { |
| 148 | const EXPECTED_RECURSION_DEPTHrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/impl#[`Rc<T>`][FromBencode]EXPECTED_RECURSION_DEPTH.: usize = T::EXPECTED_RECURSION_DEPTHrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#EXPECTED_RECURSION_DEPTH.; |
| 149 | |
| 150 | fn decode_bencode_objectrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/impl#[`Rc<T>`][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 decoding/from_bencode/impl#[`Rc<T>`][FromBencode], Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#> |
| 151 | where |
| 152 | Selfrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/impl#[`Rc<T>`][FromBencode]: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#, |
| 153 | { |
| 154 | T::decode_bencode_objectrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#decode_bencode_object().(object).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(Rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/Rc#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/impl#[`Rc<T>`]new().) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | impl FromBencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode# for AsStringrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/AsString#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>> { |
| 159 | const EXPECTED_RECURSION_DEPTHrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/impl#[`AsString<Vec<u8>>`][FromBencode]EXPECTED_RECURSION_DEPTH.: usize = 0; |
| 160 | |
| 161 | fn decode_bencode_objectrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/impl#[`AsString<Vec<u8>>`][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 decoding/from_bencode/impl#[`AsString<Vec<u8>>`][FromBencode], Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#> |
| 162 | where |
| 163 | Selfrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/impl#[`AsString<Vec<u8>>`][FromBencode]: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#, |
| 164 | { |
| 165 | object.try_into_bytesrust-analyzer cargo bendy 0.6.1 decoding/object/impl#[`Object<'obj, 'ser>`]try_into_bytes().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`][`From<&[T]>`]from().).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(AsStringrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/AsString#) |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | #[cfg(test)] |
| 170 | mod testrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/test/ { |
| 171 | |
| 172 | #[cfg(not(feature = "std"))] |
| 173 | use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::{formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/::Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#}; |
| 174 | |
| 175 | use craterust-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#; |
| 176 | |
| 177 | use superrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/::*; |
| 178 | |
| 179 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 180 | fn from_bencode_to_string_should_work_with_valid_inputrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/test/from_bencode_to_string_should_work_with_valid_input().() { |
| 181 | let expected_message = "hello"; |
| 182 | let serialized_message = |
| 183 | formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{}:{}", expected_message.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().(), expected_message).into_bytesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]into_bytes().(); |
| 184 | |
| 185 | let decoded_message = 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().(&serialized_message).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 186 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(expected_message, decoded_message); |
| 187 | } |
| 188 | |
| 189 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 190 | fn from_bencode_to_as_string_should_work_with_valid_inputrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/test/from_bencode_to_as_string_should_work_with_valid_input().() { |
| 191 | let expected_message = "hello"; |
| 192 | let serialized_message = |
| 193 | formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{}:{}", expected_message.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().(), expected_message).into_bytesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]into_bytes().(); |
| 194 | |
| 195 | let decoded_vector = AsStringrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/AsString#::from_bencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#from_bencode().(&serialized_message).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 196 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(expected_message.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().(), &decoded_vector.0rust-analyzer cargo bendy 0.6.1 encoding/to_bencode/AsString#0.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()...]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().); |
| 197 | } |
| 198 | |
| 199 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 200 | #[should_panic(expected = "Num")] |
| 201 | fn from_bencode_to_as_string_should_fail_for_integerrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/test/from_bencode_to_as_string_should_fail_for_integer().() { |
| 202 | AsStringrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/AsString#::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>>::from_bencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#from_bencode().(&b"i1e"[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 203 | } |
| 204 | |
| 205 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 206 | #[should_panic(expected = "NestingTooDeep")] |
| 207 | fn from_bencode_to_as_string_should_fail_for_listrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/test/from_bencode_to_as_string_should_fail_for_list().() { |
| 208 | AsStringrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/AsString#::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>>::from_bencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#from_bencode().(&b"l1:ae"[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 209 | } |
| 210 | |
| 211 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 212 | #[should_panic(expected = "NestingTooDeep")] |
| 213 | fn from_bencode_to_as_string_should_fail_for_dictionaryrust-analyzer cargo bendy 0.6.1 decoding/from_bencode/test/from_bencode_to_as_string_should_fail_for_dictionary().() { |
| 214 | AsStringrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/AsString#::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>>::from_bencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#from_bencode().(&b"d1:a1:ae"[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 215 | } |
| 216 | } |