From 1c10ad5f4a7f6653fd7c9d5b6fec554bc7557ab6 Mon Sep 17 00:00:00 2001 From: Daira Hopwood Date: Sat, 12 Feb 2022 20:46:02 +0000 Subject: [PATCH] Avoid the need to specify a `rust_fmt` function for Options. This does not change test vectors. Signed-off-by: Daira Hopwood --- zcash_test_vectors/output.py | 2 + zcash_test_vectors/unified_address.py | 34 ++++--------- .../unified_full_viewing_keys.py | 27 +++------- .../unified_incoming_viewing_keys.py | 27 +++------- zcash_test_vectors/zip_0244.py | 50 +++++-------------- 5 files changed, 38 insertions(+), 102 deletions(-) diff --git a/zcash_test_vectors/output.py b/zcash_test_vectors/output.py index 2c6d54f..93a7072 100644 --- a/zcash_test_vectors/output.py +++ b/zcash_test_vectors/output.py @@ -132,6 +132,8 @@ def tv_option_int_rust(name, value, pad): def tv_part_rust(name, value, config, indent=3): if 'rust_fmt' in config: value = config['rust_fmt'](value) + elif config['rust_type'].startswith('Option<') and not (value is None or isinstance(value, Some)): + value = Some(value) pad = ' ' * indent if config['rust_type'] == 'Option>': diff --git a/zcash_test_vectors/unified_address.py b/zcash_test_vectors/unified_address.py index 28f01e0..38765c2 100755 --- a/zcash_test_vectors/unified_address.py +++ b/zcash_test_vectors/unified_address.py @@ -118,31 +118,15 @@ def main(): args, 'unified_address', ( - ('p2pkh_bytes', { - 'rust_type': 'Option<[u8; 20]>', - 'rust_fmt': lambda x: None if x is None else Some(x), - }), - ('p2sh_bytes', { - 'rust_type': 'Option<[u8; 20]>', - 'rust_fmt': lambda x: None if x is None else Some(x), - }), - ('sapling_raw_addr', { - 'rust_type': 'Option<[u8; 43]>', - 'rust_fmt': lambda x: None if x is None else Some(x), - }), - ('orchard_raw_addr', { - 'rust_type': 'Option<[u8; 43]>', - 'rust_fmt': lambda x: None if x is None else Some(x), - }), - ('unknown_typecode', 'u32'), - ('unknown_bytes', { - 'rust_type': 'Option>', - 'rust_fmt': lambda x: None if x is None else Some(x), - 'bitcoin_flavoured': False, - }), - ('unified_addr', {'rust_type': 'Vec', 'bitcoin_flavoured': False}), - ('root_seed', {'rust_type': 'Vec', 'bitcoin_flavoured': False}), - ('account', 'u32'), + ('p2pkh_bytes', 'Option<[u8; 20]>'), + ('p2sh_bytes', 'Option<[u8; 20]>'), + ('sapling_raw_addr', 'Option<[u8; 43]>'), + ('orchard_raw_addr', 'Option<[u8; 43]>'), + ('unknown_typecode', 'u32'), + ('unknown_bytes', {'rust_type': 'Option>', 'bitcoin_flavoured': False}), + ('unified_addr', {'rust_type': 'Vec', 'bitcoin_flavoured': False}), + ('root_seed', {'rust_type': 'Vec', 'bitcoin_flavoured': False}), + ('account', 'u32'), ('diversifier_index', 'u32'), ), test_vectors, diff --git a/zcash_test_vectors/unified_full_viewing_keys.py b/zcash_test_vectors/unified_full_viewing_keys.py index f865586..f8affac 100755 --- a/zcash_test_vectors/unified_full_viewing_keys.py +++ b/zcash_test_vectors/unified_full_viewing_keys.py @@ -119,27 +119,14 @@ def main(): args, 'unified_full_viewing_keys', ( - ('t_key_bytes', { - 'rust_type': 'Option<[u8; 65]>', - 'rust_fmt': lambda x: None if x is None else Some(x), - }), - ('sapling_fvk_bytes', { - 'rust_type': 'Option<[u8; 128]>', - 'rust_fmt': lambda x: None if x is None else Some(x), - }), - ('orchard_fvk_bytes', { - 'rust_type': 'Option<[u8; 96]>', - 'rust_fmt': lambda x: None if x is None else Some(x), - }), + ('t_key_bytes', 'Option<[u8; 65]>'), + ('sapling_fvk_bytes', 'Option<[u8; 128]>'), + ('orchard_fvk_bytes', 'Option<[u8; 96]>'), ('unknown_fvk_typecode', 'u32'), - ('unknown_fvk_bytes', { - 'rust_type': 'Option>', - 'rust_fmt': lambda x: None if x is None else Some(x), - 'bitcoin_flavoured': False, - }), - ('unified_fvk', {'rust_type': 'Vec', 'bitcoin_flavoured': False}), - ('root_seed', {'rust_type': 'Vec', 'bitcoin_flavoured': False}), - ('account', 'u32'), + ('unknown_fvk_bytes', {'rust_type': 'Option>', 'bitcoin_flavoured': False}), + ('unified_fvk', {'rust_type': 'Vec', 'bitcoin_flavoured': False}), + ('root_seed', {'rust_type': 'Vec', 'bitcoin_flavoured': False}), + ('account', 'u32'), ), test_vectors, ) diff --git a/zcash_test_vectors/unified_incoming_viewing_keys.py b/zcash_test_vectors/unified_incoming_viewing_keys.py index 1d41891..b8b7970 100755 --- a/zcash_test_vectors/unified_incoming_viewing_keys.py +++ b/zcash_test_vectors/unified_incoming_viewing_keys.py @@ -115,27 +115,14 @@ def main(): args, 'unified_incoming_viewing_keys', ( - ('t_key_bytes', { - 'rust_type': 'Option<[u8; 65]>', - 'rust_fmt': lambda x: None if x is None else Some(x), - }), - ('sapling_ivk_bytes', { - 'rust_type': 'Option<[u8; 64]>', - 'rust_fmt': lambda x: None if x is None else Some(x), - }), - ('orchard_ivk_bytes', { - 'rust_type': 'Option<[u8; 64]>', - 'rust_fmt': lambda x: None if x is None else Some(x), - }), + ('t_key_bytes', 'Option<[u8; 65]>'), + ('sapling_ivk_bytes', 'Option<[u8; 64]>'), + ('orchard_ivk_bytes', 'Option<[u8; 64]>'), ('unknown_ivk_typecode', 'u32'), - ('unknown_ivk_bytes', { - 'rust_type': 'Option>', - 'rust_fmt': lambda x: None if x is None else Some(x), - 'bitcoin_flavoured': False, - }), - ('unified_ivk', {'rust_type': 'Vec', 'bitcoin_flavoured': False}), - ('root_seed', {'rust_type': 'Vec', 'bitcoin_flavoured': False}), - ('account', 'u32'), + ('unknown_ivk_bytes', {'rust_type': 'Option>', 'bitcoin_flavoured': False}), + ('unified_ivk', {'rust_type': 'Vec', 'bitcoin_flavoured': False}), + ('root_seed', {'rust_type': 'Vec', 'bitcoin_flavoured': False}), + ('account', 'u32'), ), test_vectors, ) diff --git a/zcash_test_vectors/zip_0244.py b/zcash_test_vectors/zip_0244.py index be2684f..ffeed6b 100755 --- a/zcash_test_vectors/zip_0244.py +++ b/zcash_test_vectors/zip_0244.py @@ -406,43 +406,19 @@ def main(): args, 'zip_0244', ( - ('tx', {'rust_type': 'Vec', 'bitcoin_flavoured': False}), - ('txid', '[u8; 32]'), - ('auth_digest', '[u8; 32]'), - ('amounts', {'rust_type': 'Vec'}), - ('script_pubkeys', { - 'rust_type': 'Vec>', - 'bitcoin_flavoured': False, - }), - ('transparent_input', { - 'rust_type': 'Option', - 'rust_fmt': lambda x: None if x is None else Some(x), - }), - ('sighash_shielded', '[u8; 32]'), - ('sighash_all', { - 'rust_type': 'Option<[u8; 32]>', - 'rust_fmt': lambda x: None if x is None else Some(x), - }), - ('sighash_none', { - 'rust_type': 'Option<[u8; 32]>', - 'rust_fmt': lambda x: None if x is None else Some(x), - }), - ('sighash_single', { - 'rust_type': 'Option<[u8; 32]>', - 'rust_fmt': lambda x: None if x is None else Some(x), - }), - ('sighash_all_anyone', { - 'rust_type': 'Option<[u8; 32]>', - 'rust_fmt': lambda x: None if x is None else Some(x), - }), - ('sighash_none_anyone', { - 'rust_type': 'Option<[u8; 32]>', - 'rust_fmt': lambda x: None if x is None else Some(x), - }), - ('sighash_single_anyone', { - 'rust_type': 'Option<[u8; 32]>', - 'rust_fmt': lambda x: None if x is None else Some(x), - }), + ('tx', {'rust_type': 'Vec', 'bitcoin_flavoured': False}), + ('txid', '[u8; 32]'), + ('auth_digest', '[u8; 32]'), + ('amounts', 'Vec'), + ('script_pubkeys', {'rust_type': 'Vec>', 'bitcoin_flavoured': False}), + ('transparent_input', 'Option'), + ('sighash_shielded', '[u8; 32]'), + ('sighash_all', 'Option<[u8; 32]>'), + ('sighash_none', 'Option<[u8; 32]>'), + ('sighash_single', 'Option<[u8; 32]>'), + ('sighash_all_anyone', 'Option<[u8; 32]>'), + ('sighash_none_anyone', 'Option<[u8; 32]>'), + ('sighash_single_anyone', 'Option<[u8; 32]>'), ), test_vectors, )