2
2
use crate :: access:: bencode:: { BRefAccess , BRefAccessExt } ;
3
3
use crate :: access:: dict:: BDictAccess ;
4
4
use crate :: access:: list:: BListAccess ;
5
- use crate :: { BencodeConvertError , BencodeConvertErrorKind } ;
5
+ use crate :: BencodeConvertError ;
6
6
7
7
/// Trait for extended casting of bencode objects and converting conversion errors into application specific errors.
8
8
pub trait BConvertExt : BConvert {
@@ -12,12 +12,10 @@ pub trait BConvertExt: BConvert {
12
12
B : BRefAccessExt < ' a > ,
13
13
E : AsRef < [ u8 ] > ,
14
14
{
15
- bencode. bytes_ext ( ) . ok_or (
16
- self . handle_error ( BencodeConvertError :: from_kind ( BencodeConvertErrorKind :: WrongType {
17
- key : error_key. as_ref ( ) . to_owned ( ) ,
18
- expected_type : "Bytes" . to_owned ( ) ,
19
- } ) ) ,
20
- )
15
+ bencode. bytes_ext ( ) . ok_or ( self . handle_error ( BencodeConvertError :: WrongType {
16
+ key : error_key. as_ref ( ) . to_owned ( ) ,
17
+ expected_type : "Bytes" . to_owned ( ) ,
18
+ } ) )
21
19
}
22
20
23
21
/// See `BConvert::convert_str`.
@@ -26,12 +24,10 @@ pub trait BConvertExt: BConvert {
26
24
B : BRefAccessExt < ' a > ,
27
25
E : AsRef < [ u8 ] > ,
28
26
{
29
- bencode. str_ext ( ) . ok_or (
30
- self . handle_error ( BencodeConvertError :: from_kind ( BencodeConvertErrorKind :: WrongType {
31
- key : error_key. as_ref ( ) . to_owned ( ) ,
32
- expected_type : "UTF-8 Bytes" . to_owned ( ) ,
33
- } ) ) ,
34
- )
27
+ bencode. str_ext ( ) . ok_or ( self . handle_error ( BencodeConvertError :: WrongType {
28
+ key : error_key. as_ref ( ) . to_owned ( ) ,
29
+ expected_type : "UTF-8 Bytes" . to_owned ( ) ,
30
+ } ) )
35
31
}
36
32
37
33
/// See `BConvert::lookup_and_convert_bytes`.
@@ -77,12 +73,10 @@ pub trait BConvert {
77
73
B : BRefAccess ,
78
74
E : AsRef < [ u8 ] > ,
79
75
{
80
- bencode. int ( ) . ok_or (
81
- self . handle_error ( BencodeConvertError :: from_kind ( BencodeConvertErrorKind :: WrongType {
82
- key : error_key. as_ref ( ) . to_owned ( ) ,
83
- expected_type : "Integer" . to_owned ( ) ,
84
- } ) ) ,
85
- )
76
+ bencode. int ( ) . ok_or ( self . handle_error ( BencodeConvertError :: WrongType {
77
+ key : error_key. as_ref ( ) . to_owned ( ) ,
78
+ expected_type : "Integer" . to_owned ( ) ,
79
+ } ) )
86
80
}
87
81
88
82
/// Attempt to convert the given bencode value into bytes.
@@ -93,12 +87,10 @@ pub trait BConvert {
93
87
B : BRefAccess ,
94
88
E : AsRef < [ u8 ] > ,
95
89
{
96
- bencode. bytes ( ) . ok_or (
97
- self . handle_error ( BencodeConvertError :: from_kind ( BencodeConvertErrorKind :: WrongType {
98
- key : error_key. as_ref ( ) . to_owned ( ) ,
99
- expected_type : "Bytes" . to_owned ( ) ,
100
- } ) ) ,
101
- )
90
+ bencode. bytes ( ) . ok_or ( self . handle_error ( BencodeConvertError :: WrongType {
91
+ key : error_key. as_ref ( ) . to_owned ( ) ,
92
+ expected_type : "Bytes" . to_owned ( ) ,
93
+ } ) )
102
94
}
103
95
104
96
/// Attempt to convert the given bencode value into a UTF-8 string.
@@ -109,12 +101,10 @@ pub trait BConvert {
109
101
B : BRefAccess ,
110
102
E : AsRef < [ u8 ] > ,
111
103
{
112
- bencode. str ( ) . ok_or (
113
- self . handle_error ( BencodeConvertError :: from_kind ( BencodeConvertErrorKind :: WrongType {
114
- key : error_key. as_ref ( ) . to_owned ( ) ,
115
- expected_type : "UTF-8 Bytes" . to_owned ( ) ,
116
- } ) ) ,
117
- )
104
+ bencode. str ( ) . ok_or ( self . handle_error ( BencodeConvertError :: WrongType {
105
+ key : error_key. as_ref ( ) . to_owned ( ) ,
106
+ expected_type : "UTF-8 Bytes" . to_owned ( ) ,
107
+ } ) )
118
108
}
119
109
120
110
/// Attempt to convert the given bencode value into a list.
@@ -125,12 +115,10 @@ pub trait BConvert {
125
115
B : BRefAccess ,
126
116
E : AsRef < [ u8 ] > ,
127
117
{
128
- bencode. list ( ) . ok_or (
129
- self . handle_error ( BencodeConvertError :: from_kind ( BencodeConvertErrorKind :: WrongType {
130
- key : error_key. as_ref ( ) . to_owned ( ) ,
131
- expected_type : "List" . to_owned ( ) ,
132
- } ) ) ,
133
- )
118
+ bencode. list ( ) . ok_or ( self . handle_error ( BencodeConvertError :: WrongType {
119
+ key : error_key. as_ref ( ) . to_owned ( ) ,
120
+ expected_type : "List" . to_owned ( ) ,
121
+ } ) )
134
122
}
135
123
136
124
/// Attempt to convert the given bencode value into a dictionary.
@@ -141,12 +129,10 @@ pub trait BConvert {
141
129
B : BRefAccess ,
142
130
E : AsRef < [ u8 ] > ,
143
131
{
144
- bencode. dict ( ) . ok_or (
145
- self . handle_error ( BencodeConvertError :: from_kind ( BencodeConvertErrorKind :: WrongType {
146
- key : error_key. as_ref ( ) . to_owned ( ) ,
147
- expected_type : "Dictionary" . to_owned ( ) ,
148
- } ) ) ,
149
- )
132
+ bencode. dict ( ) . ok_or ( self . handle_error ( BencodeConvertError :: WrongType {
133
+ key : error_key. as_ref ( ) . to_owned ( ) ,
134
+ expected_type : "Dictionary" . to_owned ( ) ,
135
+ } ) )
150
136
}
151
137
152
138
/// Look up a value in a dictionary of bencoded values using the given key.
@@ -159,11 +145,7 @@ pub trait BConvert {
159
145
160
146
match dictionary. lookup ( key_ref) {
161
147
Some ( n) => Ok ( n) ,
162
- None => Err (
163
- self . handle_error ( BencodeConvertError :: from_kind ( BencodeConvertErrorKind :: MissingKey {
164
- key : key_ref. to_owned ( ) ,
165
- } ) ) ,
166
- ) ,
148
+ None => Err ( self . handle_error ( BencodeConvertError :: MissingKey { key : key_ref. to_owned ( ) } ) ) ,
167
149
}
168
150
}
169
151
0 commit comments