22use crate :: access:: bencode:: { BRefAccess , BRefAccessExt } ;
33use crate :: access:: dict:: BDictAccess ;
44use crate :: access:: list:: BListAccess ;
5- use crate :: { BencodeConvertError , BencodeConvertErrorKind } ;
5+ use crate :: BencodeConvertError ;
66
77/// Trait for extended casting of bencode objects and converting conversion errors into application specific errors.
88pub trait BConvertExt : BConvert {
@@ -12,12 +12,10 @@ pub trait BConvertExt: BConvert {
1212 B : BRefAccessExt < ' a > ,
1313 E : AsRef < [ u8 ] > ,
1414 {
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+ } ) )
2119 }
2220
2321 /// See `BConvert::convert_str`.
@@ -26,12 +24,10 @@ pub trait BConvertExt: BConvert {
2624 B : BRefAccessExt < ' a > ,
2725 E : AsRef < [ u8 ] > ,
2826 {
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+ } ) )
3531 }
3632
3733 /// See `BConvert::lookup_and_convert_bytes`.
@@ -77,12 +73,10 @@ pub trait BConvert {
7773 B : BRefAccess ,
7874 E : AsRef < [ u8 ] > ,
7975 {
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+ } ) )
8680 }
8781
8882 /// Attempt to convert the given bencode value into bytes.
@@ -93,12 +87,10 @@ pub trait BConvert {
9387 B : BRefAccess ,
9488 E : AsRef < [ u8 ] > ,
9589 {
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+ } ) )
10294 }
10395
10496 /// Attempt to convert the given bencode value into a UTF-8 string.
@@ -109,12 +101,10 @@ pub trait BConvert {
109101 B : BRefAccess ,
110102 E : AsRef < [ u8 ] > ,
111103 {
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+ } ) )
118108 }
119109
120110 /// Attempt to convert the given bencode value into a list.
@@ -125,12 +115,10 @@ pub trait BConvert {
125115 B : BRefAccess ,
126116 E : AsRef < [ u8 ] > ,
127117 {
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+ } ) )
134122 }
135123
136124 /// Attempt to convert the given bencode value into a dictionary.
@@ -141,12 +129,10 @@ pub trait BConvert {
141129 B : BRefAccess ,
142130 E : AsRef < [ u8 ] > ,
143131 {
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+ } ) )
150136 }
151137
152138 /// Look up a value in a dictionary of bencoded values using the given key.
@@ -159,11 +145,7 @@ pub trait BConvert {
159145
160146 match dictionary. lookup ( key_ref) {
161147 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 ( ) } ) ) ,
167149 }
168150 }
169151
0 commit comments