Avoid the need to specify a `rust_fmt` function for Options.

This does not change test vectors.

Signed-off-by: Daira Hopwood <daira@jacaranda.org>
This commit is contained in:
Daira Hopwood 2022-02-12 20:46:02 +00:00
parent 54f168a30b
commit 1c10ad5f4a
5 changed files with 38 additions and 102 deletions

View File

@ -132,6 +132,8 @@ def tv_option_int_rust(name, value, pad):
def tv_part_rust(name, value, config, indent=3): def tv_part_rust(name, value, config, indent=3):
if 'rust_fmt' in config: if 'rust_fmt' in config:
value = config['rust_fmt'](value) 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 pad = ' ' * indent
if config['rust_type'] == 'Option<Vec<u8>>': if config['rust_type'] == 'Option<Vec<u8>>':

View File

@ -118,28 +118,12 @@ def main():
args, args,
'unified_address', 'unified_address',
( (
('p2pkh_bytes', { ('p2pkh_bytes', 'Option<[u8; 20]>'),
'rust_type': 'Option<[u8; 20]>', ('p2sh_bytes', 'Option<[u8; 20]>'),
'rust_fmt': lambda x: None if x is None else Some(x), ('sapling_raw_addr', 'Option<[u8; 43]>'),
}), ('orchard_raw_addr', 'Option<[u8; 43]>'),
('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_typecode', 'u32'),
('unknown_bytes', { ('unknown_bytes', {'rust_type': 'Option<Vec<u8>>', 'bitcoin_flavoured': False}),
'rust_type': 'Option<Vec<u8>>',
'rust_fmt': lambda x: None if x is None else Some(x),
'bitcoin_flavoured': False,
}),
('unified_addr', {'rust_type': 'Vec<u8>', 'bitcoin_flavoured': False}), ('unified_addr', {'rust_type': 'Vec<u8>', 'bitcoin_flavoured': False}),
('root_seed', {'rust_type': 'Vec<u8>', 'bitcoin_flavoured': False}), ('root_seed', {'rust_type': 'Vec<u8>', 'bitcoin_flavoured': False}),
('account', 'u32'), ('account', 'u32'),

View File

@ -119,24 +119,11 @@ def main():
args, args,
'unified_full_viewing_keys', 'unified_full_viewing_keys',
( (
('t_key_bytes', { ('t_key_bytes', 'Option<[u8; 65]>'),
'rust_type': 'Option<[u8; 65]>', ('sapling_fvk_bytes', 'Option<[u8; 128]>'),
'rust_fmt': lambda x: None if x is None else Some(x), ('orchard_fvk_bytes', 'Option<[u8; 96]>'),
}),
('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),
}),
('unknown_fvk_typecode', 'u32'), ('unknown_fvk_typecode', 'u32'),
('unknown_fvk_bytes', { ('unknown_fvk_bytes', {'rust_type': 'Option<Vec<u8>>', 'bitcoin_flavoured': False}),
'rust_type': 'Option<Vec<u8>>',
'rust_fmt': lambda x: None if x is None else Some(x),
'bitcoin_flavoured': False,
}),
('unified_fvk', {'rust_type': 'Vec<u8>', 'bitcoin_flavoured': False}), ('unified_fvk', {'rust_type': 'Vec<u8>', 'bitcoin_flavoured': False}),
('root_seed', {'rust_type': 'Vec<u8>', 'bitcoin_flavoured': False}), ('root_seed', {'rust_type': 'Vec<u8>', 'bitcoin_flavoured': False}),
('account', 'u32'), ('account', 'u32'),

View File

@ -115,24 +115,11 @@ def main():
args, args,
'unified_incoming_viewing_keys', 'unified_incoming_viewing_keys',
( (
('t_key_bytes', { ('t_key_bytes', 'Option<[u8; 65]>'),
'rust_type': 'Option<[u8; 65]>', ('sapling_ivk_bytes', 'Option<[u8; 64]>'),
'rust_fmt': lambda x: None if x is None else Some(x), ('orchard_ivk_bytes', 'Option<[u8; 64]>'),
}),
('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),
}),
('unknown_ivk_typecode', 'u32'), ('unknown_ivk_typecode', 'u32'),
('unknown_ivk_bytes', { ('unknown_ivk_bytes', {'rust_type': 'Option<Vec<u8>>', 'bitcoin_flavoured': False}),
'rust_type': 'Option<Vec<u8>>',
'rust_fmt': lambda x: None if x is None else Some(x),
'bitcoin_flavoured': False,
}),
('unified_ivk', {'rust_type': 'Vec<u8>', 'bitcoin_flavoured': False}), ('unified_ivk', {'rust_type': 'Vec<u8>', 'bitcoin_flavoured': False}),
('root_seed', {'rust_type': 'Vec<u8>', 'bitcoin_flavoured': False}), ('root_seed', {'rust_type': 'Vec<u8>', 'bitcoin_flavoured': False}),
('account', 'u32'), ('account', 'u32'),

View File

@ -409,40 +409,16 @@ def main():
('tx', {'rust_type': 'Vec<u8>', 'bitcoin_flavoured': False}), ('tx', {'rust_type': 'Vec<u8>', 'bitcoin_flavoured': False}),
('txid', '[u8; 32]'), ('txid', '[u8; 32]'),
('auth_digest', '[u8; 32]'), ('auth_digest', '[u8; 32]'),
('amounts', {'rust_type': 'Vec<i64>'}), ('amounts', 'Vec<i64>'),
('script_pubkeys', { ('script_pubkeys', {'rust_type': 'Vec<Vec<u8>>', 'bitcoin_flavoured': False}),
'rust_type': 'Vec<Vec<u8>>', ('transparent_input', 'Option<u32>'),
'bitcoin_flavoured': False,
}),
('transparent_input', {
'rust_type': 'Option<u32>',
'rust_fmt': lambda x: None if x is None else Some(x),
}),
('sighash_shielded', '[u8; 32]'), ('sighash_shielded', '[u8; 32]'),
('sighash_all', { ('sighash_all', 'Option<[u8; 32]>'),
'rust_type': 'Option<[u8; 32]>', ('sighash_none', 'Option<[u8; 32]>'),
'rust_fmt': lambda x: None if x is None else Some(x), ('sighash_single', 'Option<[u8; 32]>'),
}), ('sighash_all_anyone', 'Option<[u8; 32]>'),
('sighash_none', { ('sighash_none_anyone', 'Option<[u8; 32]>'),
'rust_type': 'Option<[u8; 32]>', ('sighash_single_anyone', '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),
}),
), ),
test_vectors, test_vectors,
) )