34
34
35
35
use crate :: prelude:: * ;
36
36
37
+ use core:: convert:: TryFrom ;
37
38
use core:: fmt;
38
39
use core:: num:: ParseIntError ;
39
40
use core:: str:: FromStr ;
@@ -243,8 +244,8 @@ impl FromStr for WitnessVersion {
243
244
type Err = Error ;
244
245
245
246
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
246
- let version = s. parse ( ) . map_err ( Error :: UnparsableWitnessVersion ) ?;
247
- WitnessVersion :: from_num ( version)
247
+ let version: u8 = s. parse ( ) . map_err ( Error :: UnparsableWitnessVersion ) ?;
248
+ WitnessVersion :: try_from ( version)
248
249
}
249
250
}
250
251
@@ -258,8 +259,9 @@ impl WitnessVersion {
258
259
/// # Errors
259
260
/// If the integer does not correspond to any witness version, errors with
260
261
/// [`Error::InvalidWitnessVersion`].
262
+ #[ deprecated( since = "0.29.0" , note = "use try_from instead" ) ]
261
263
pub fn from_u5 ( value : :: bech32:: u5 ) -> Result < Self , Error > {
262
- WitnessVersion :: from_num ( value. to_u8 ( ) )
264
+ Self :: try_from ( value)
263
265
}
264
266
265
267
/// Converts an 8-bit unsigned integer value into [`WitnessVersion`] variant.
@@ -270,27 +272,9 @@ impl WitnessVersion {
270
272
/// # Errors
271
273
/// If the integer does not correspond to any witness version, errors with
272
274
/// [`Error::InvalidWitnessVersion`].
275
+ #[ deprecated( since = "0.29.0" , note = "use try_from instead" ) ]
273
276
pub fn from_num ( no : u8 ) -> Result < Self , Error > {
274
- Ok ( match no {
275
- 0 => WitnessVersion :: V0 ,
276
- 1 => WitnessVersion :: V1 ,
277
- 2 => WitnessVersion :: V2 ,
278
- 3 => WitnessVersion :: V3 ,
279
- 4 => WitnessVersion :: V4 ,
280
- 5 => WitnessVersion :: V5 ,
281
- 6 => WitnessVersion :: V6 ,
282
- 7 => WitnessVersion :: V7 ,
283
- 8 => WitnessVersion :: V8 ,
284
- 9 => WitnessVersion :: V9 ,
285
- 10 => WitnessVersion :: V10 ,
286
- 11 => WitnessVersion :: V11 ,
287
- 12 => WitnessVersion :: V12 ,
288
- 13 => WitnessVersion :: V13 ,
289
- 14 => WitnessVersion :: V14 ,
290
- 15 => WitnessVersion :: V15 ,
291
- 16 => WitnessVersion :: V16 ,
292
- wrong => return Err ( Error :: InvalidWitnessVersion ( wrong) ) ,
293
- } )
277
+ Self :: try_from ( no)
294
278
}
295
279
296
280
/// Converts bitcoin script opcode into [`WitnessVersion`] variant.
@@ -301,13 +285,9 @@ impl WitnessVersion {
301
285
/// # Errors
302
286
/// If the opcode does not correspond to any witness version, errors with
303
287
/// [`Error::MalformedWitnessVersion`].
288
+ #[ deprecated( since = "0.29.0" , note = "use try_from instead" ) ]
304
289
pub fn from_opcode ( opcode : opcodes:: All ) -> Result < Self , Error > {
305
- match opcode. to_u8 ( ) {
306
- 0 => Ok ( WitnessVersion :: V0 ) ,
307
- version if version >= opcodes:: all:: OP_PUSHNUM_1 . to_u8 ( ) && version <= opcodes:: all:: OP_PUSHNUM_16 . to_u8 ( ) =>
308
- WitnessVersion :: from_num ( version - opcodes:: all:: OP_PUSHNUM_1 . to_u8 ( ) + 1 ) ,
309
- _ => Err ( Error :: MalformedWitnessVersion )
310
- }
290
+ Self :: try_from ( opcode)
311
291
}
312
292
313
293
/// Converts bitcoin script [`Instruction`] (parsed opcode) into [`WitnessVersion`] variant.
@@ -319,12 +299,9 @@ impl WitnessVersion {
319
299
/// # Errors
320
300
/// If the opcode does not correspond to any witness version, errors with
321
301
/// [`Error::MalformedWitnessVersion`] for the rest of opcodes.
302
+ #[ deprecated( since = "0.29.0" , note = "use try_from instead" ) ]
322
303
pub fn from_instruction ( instruction : Instruction ) -> Result < Self , Error > {
323
- match instruction {
324
- Instruction :: Op ( op) => WitnessVersion :: from_opcode ( op) ,
325
- Instruction :: PushBytes ( bytes) if bytes. is_empty ( ) => Ok ( WitnessVersion :: V0 ) ,
326
- Instruction :: PushBytes ( _) => Err ( Error :: MalformedWitnessVersion ) ,
327
- }
304
+ Self :: try_from ( instruction)
328
305
}
329
306
330
307
/// Returns integer version number representation for a given [`WitnessVersion`] value.
@@ -355,6 +332,102 @@ impl WitnessVersion {
355
332
}
356
333
}
357
334
335
+ impl TryFrom < bech32:: u5 > for WitnessVersion {
336
+ type Error = Error ;
337
+
338
+ /// Converts 5-bit unsigned integer value matching single symbol from Bech32(m) address encoding
339
+ /// ([`bech32::u5`]) into [`WitnessVersion`] variant.
340
+ ///
341
+ /// # Returns
342
+ /// Version of the Witness program.
343
+ ///
344
+ /// # Errors
345
+ /// If the integer does not correspond to any witness version, errors with
346
+ /// [`Error::InvalidWitnessVersion`].
347
+ fn try_from ( value : bech32:: u5 ) -> Result < Self , Self :: Error > {
348
+ Self :: try_from ( value. to_u8 ( ) )
349
+ }
350
+ }
351
+
352
+ impl TryFrom < u8 > for WitnessVersion {
353
+ type Error = Error ;
354
+
355
+ /// Converts an 8-bit unsigned integer value into [`WitnessVersion`] variant.
356
+ ///
357
+ /// # Returns
358
+ /// Version of the Witness program.
359
+ ///
360
+ /// # Errors
361
+ /// If the integer does not correspond to any witness version, errors with
362
+ /// [`Error::InvalidWitnessVersion`].
363
+ fn try_from ( no : u8 ) -> Result < Self , Self :: Error > {
364
+ use WitnessVersion :: * ;
365
+
366
+ Ok ( match no {
367
+ 0 => V0 ,
368
+ 1 => V1 ,
369
+ 2 => V2 ,
370
+ 3 => V3 ,
371
+ 4 => V4 ,
372
+ 5 => V5 ,
373
+ 6 => V6 ,
374
+ 7 => V7 ,
375
+ 8 => V8 ,
376
+ 9 => V9 ,
377
+ 10 => V10 ,
378
+ 11 => V11 ,
379
+ 12 => V12 ,
380
+ 13 => V13 ,
381
+ 14 => V14 ,
382
+ 15 => V15 ,
383
+ 16 => V16 ,
384
+ wrong => return Err ( Error :: InvalidWitnessVersion ( wrong) ) ,
385
+ } )
386
+ }
387
+ }
388
+
389
+ impl TryFrom < opcodes:: All > for WitnessVersion {
390
+ type Error = Error ;
391
+
392
+ /// Converts bitcoin script opcode into [`WitnessVersion`] variant.
393
+ ///
394
+ /// # Returns
395
+ /// Version of the Witness program (for opcodes in range of `OP_0`..`OP_16`).
396
+ ///
397
+ /// # Errors
398
+ /// If the opcode does not correspond to any witness version, errors with
399
+ /// [`Error::MalformedWitnessVersion`].
400
+ fn try_from ( opcode : opcodes:: All ) -> Result < Self , Self :: Error > {
401
+ match opcode. to_u8 ( ) {
402
+ 0 => Ok ( WitnessVersion :: V0 ) ,
403
+ version if version >= opcodes:: all:: OP_PUSHNUM_1 . to_u8 ( ) && version <= opcodes:: all:: OP_PUSHNUM_16 . to_u8 ( ) =>
404
+ WitnessVersion :: try_from ( version - opcodes:: all:: OP_PUSHNUM_1 . to_u8 ( ) + 1 ) ,
405
+ _ => Err ( Error :: MalformedWitnessVersion )
406
+ }
407
+ }
408
+ }
409
+
410
+ impl < ' a > TryFrom < Instruction < ' a > > for WitnessVersion {
411
+ type Error = Error ;
412
+
413
+ /// Converts bitcoin script [`Instruction`] (parsed opcode) into [`WitnessVersion`] variant.
414
+ ///
415
+ /// # Returns
416
+ /// Version of the Witness program for [`Instruction::Op`] and [`Instruction::PushBytes`] with
417
+ /// byte value within `1..=16` range.
418
+ ///
419
+ /// # Errors
420
+ /// If the opcode does not correspond to any witness version, errors with
421
+ /// [`Error::MalformedWitnessVersion`] for the rest of opcodes.
422
+ fn try_from ( instruction : Instruction ) -> Result < Self , Self :: Error > {
423
+ match instruction {
424
+ Instruction :: Op ( op) => WitnessVersion :: try_from ( op) ,
425
+ Instruction :: PushBytes ( bytes) if bytes. is_empty ( ) => Ok ( WitnessVersion :: V0 ) ,
426
+ Instruction :: PushBytes ( _) => Err ( Error :: MalformedWitnessVersion ) ,
427
+ }
428
+ }
429
+ }
430
+
358
431
impl From < WitnessVersion > for :: bech32:: u5 {
359
432
/// Converts [`WitnessVersion`] instance into corresponding Bech32(m) u5-value ([`bech32::u5`]).
360
433
fn from ( version : WitnessVersion ) -> Self {
@@ -405,7 +478,7 @@ impl Payload {
405
478
}
406
479
407
480
Payload :: WitnessProgram {
408
- version : WitnessVersion :: from_opcode ( opcodes:: All :: from ( script[ 0 ] ) ) ?,
481
+ version : WitnessVersion :: try_from ( opcodes:: All :: from ( script[ 0 ] ) ) ?,
409
482
program : script[ 2 ..] . to_vec ( ) ,
410
483
}
411
484
} else {
@@ -856,7 +929,7 @@ impl FromStr for Address {
856
929
// Get the script version and program (converted from 5-bit to 8-bit)
857
930
let ( version, program) : ( WitnessVersion , Vec < u8 > ) = {
858
931
let ( v, p5) = payload. split_at ( 1 ) ;
859
- ( WitnessVersion :: from_u5 ( v[ 0 ] ) ?, bech32:: FromBase32 :: from_base32 ( p5) ?)
932
+ ( WitnessVersion :: try_from ( v[ 0 ] ) ?, bech32:: FromBase32 :: from_base32 ( p5) ?)
860
933
} ;
861
934
862
935
if program. len ( ) < 2 || program. len ( ) > 40 {
@@ -1277,7 +1350,7 @@ mod tests {
1277
1350
] ;
1278
1351
let segwit_payload = ( 0 ..=16 ) . map ( |version| {
1279
1352
Payload :: WitnessProgram {
1280
- version : WitnessVersion :: from_num ( version) . unwrap ( ) ,
1353
+ version : WitnessVersion :: try_from ( version) . unwrap ( ) ,
1281
1354
program : vec ! [ ]
1282
1355
}
1283
1356
} ) . collect :: < Vec < _ > > ( ) ;
0 commit comments