1
1
use std:: iter:: ExactSizeIterator ;
2
2
3
- use bip_bencode:: { BencodeMut , BMutAccess } ;
3
+ use bip_bencode:: { BencodeMut , BMutAccess , BRefAccess } ;
4
4
use bip_util:: sha:: { self , ShaHash } ;
5
5
6
6
use accessor:: { Accessor , IntoAccessor } ;
@@ -103,9 +103,12 @@ impl<'a> MetainfoBuilder<'a> {
103
103
pub fn set_main_tracker ( mut self , opt_tracker_url : Option < & ' a str > ) -> MetainfoBuilder < ' a > {
104
104
{
105
105
let dict_access = self . root . dict_mut ( ) . unwrap ( ) ;
106
- opt_tracker_url
107
- . and_then ( |tracker_url| dict_access. insert ( parse:: ANNOUNCE_URL_KEY . into ( ) , ben_bytes ! ( tracker_url) ) )
108
- . or_else ( || dict_access. remove ( parse:: ANNOUNCE_URL_KEY ) ) ;
106
+
107
+ if let Some ( tracker_url) = opt_tracker_url {
108
+ dict_access. insert ( parse:: ANNOUNCE_URL_KEY . into ( ) , ben_bytes ! ( tracker_url) ) ;
109
+ } else {
110
+ dict_access. remove ( parse:: ANNOUNCE_URL_KEY ) ;
111
+ }
109
112
}
110
113
111
114
self
@@ -115,9 +118,12 @@ impl<'a> MetainfoBuilder<'a> {
115
118
pub fn set_creation_date ( mut self , opt_secs_epoch : Option < i64 > ) -> MetainfoBuilder < ' a > {
116
119
{
117
120
let dict_access = self . root . dict_mut ( ) . unwrap ( ) ;
118
- opt_secs_epoch
119
- . and_then ( |secs_epoch| dict_access. insert ( parse:: CREATION_DATE_KEY . into ( ) , ben_int ! ( secs_epoch) ) )
120
- . or_else ( || dict_access. remove ( parse:: CREATION_DATE_KEY ) ) ;
121
+
122
+ if let Some ( secs_epoch) = opt_secs_epoch {
123
+ dict_access. insert ( parse:: CREATION_DATE_KEY . into ( ) , ben_int ! ( secs_epoch) ) ;
124
+ } else {
125
+ dict_access. remove ( parse:: CREATION_DATE_KEY ) ;
126
+ }
121
127
}
122
128
123
129
self
@@ -127,9 +133,12 @@ impl<'a> MetainfoBuilder<'a> {
127
133
pub fn set_comment ( mut self , opt_comment : Option < & ' a str > ) -> MetainfoBuilder < ' a > {
128
134
{
129
135
let dict_access = self . root . dict_mut ( ) . unwrap ( ) ;
130
- opt_comment
131
- . and_then ( |comment| dict_access. insert ( parse:: COMMENT_KEY . into ( ) , ben_bytes ! ( comment) ) )
132
- . or_else ( || dict_access. remove ( parse:: COMMENT_KEY ) ) ;
136
+
137
+ if let Some ( comment) = opt_comment {
138
+ dict_access. insert ( parse:: COMMENT_KEY . into ( ) , ben_bytes ! ( comment) ) ;
139
+ } else {
140
+ dict_access. remove ( parse:: COMMENT_KEY ) ;
141
+ }
133
142
}
134
143
135
144
self
@@ -139,9 +148,12 @@ impl<'a> MetainfoBuilder<'a> {
139
148
pub fn set_created_by ( mut self , opt_created_by : Option < & ' a str > ) -> MetainfoBuilder < ' a > {
140
149
{
141
150
let dict_access = self . root . dict_mut ( ) . unwrap ( ) ;
142
- opt_created_by
143
- . and_then ( |created_by| dict_access. insert ( parse:: CREATED_BY_KEY . into ( ) , ben_bytes ! ( created_by) ) )
144
- . or_else ( || dict_access. remove ( parse:: CREATED_BY_KEY ) ) ;
151
+
152
+ if let Some ( created_by) = opt_created_by {
153
+ dict_access. insert ( parse:: CREATED_BY_KEY . into ( ) , ben_bytes ! ( created_by) ) ;
154
+ } else {
155
+ dict_access. remove ( parse:: CREATED_BY_KEY ) ;
156
+ }
145
157
}
146
158
147
159
self
@@ -161,6 +173,41 @@ impl<'a> MetainfoBuilder<'a> {
161
173
self
162
174
}
163
175
176
+ /// Get decoded value of announce-list key
177
+ pub fn get_trackers ( & self ) -> Option < Vec < Vec < String > > > {
178
+ let dict_access = self . root . dict ( ) . unwrap ( ) ;
179
+
180
+ parse:: parse_announce_list ( dict_access) . map ( parse:: convert_announce_list)
181
+ }
182
+
183
+ /// Get decoded value of announce-url key
184
+ pub fn get_main_tracker ( & self ) -> Option < String > {
185
+ let dict_access = self . root . dict ( ) . unwrap ( ) ;
186
+
187
+ parse:: parse_announce_url ( dict_access) . map ( String :: from)
188
+ }
189
+
190
+ /// Get decoded value of creation-date key
191
+ pub fn get_creation_date ( & self ) -> Option < i64 > {
192
+ let dict_access = self . root . dict ( ) . unwrap ( ) ;
193
+
194
+ parse:: parse_creation_date ( dict_access)
195
+ }
196
+
197
+ /// Get decoded value of comment key
198
+ pub fn get_comment ( & self ) -> Option < String > {
199
+ let dict_access = self . root . dict ( ) . unwrap ( ) ;
200
+
201
+ parse:: parse_comment ( dict_access) . map ( String :: from)
202
+ }
203
+
204
+ /// Get decoded value of created-by key
205
+ pub fn get_created_by ( & self ) -> Option < String > {
206
+ let dict_access = self . root . dict ( ) . unwrap ( ) ;
207
+
208
+ parse:: parse_created_by ( dict_access) . map ( String :: from)
209
+ }
210
+
164
211
/// Build the metainfo file from the given accessor and the number of worker threads.
165
212
///
166
213
/// Panics if threads is equal to zero.
@@ -192,7 +239,7 @@ impl<'a> InfoBuilder<'a> {
192
239
/// Set or unset the private flag for the torrent file.
193
240
pub fn set_private_flag ( mut self , opt_is_private : Option < bool > ) -> InfoBuilder < ' a > {
194
241
let opt_numeric_is_private = opt_is_private. map ( |is_private| if is_private{ 1 } else { 0 } ) ;
195
-
242
+
196
243
{
197
244
let dict_access = self . info . dict_mut ( ) . unwrap ( ) ;
198
245
opt_numeric_is_private
@@ -236,7 +283,7 @@ fn build_with_accessor<'a, A, C>(threads: usize,
236
283
if threads == 0 {
237
284
panic ! ( "bip_metainfo: Cannot Build Metainfo File With threads == 0" ) ;
238
285
}
239
-
286
+
240
287
// Collect all of the file information into a list
241
288
let mut files_info = Vec :: new ( ) ;
242
289
try!( accessor. access_metadata ( |len, path| {
0 commit comments