Refactor Rust output rendering to support reformatting

Used to convert a -1 for JSON to Option::None in Rust
This commit is contained in:
Jack Grigg 2018-08-21 20:13:11 +01:00
parent a9676cc9b4
commit 17d146b5a9
No known key found for this signature in database
GPG Key ID: 1B8D649257DB0829
2 changed files with 44 additions and 15 deletions

View File

@ -88,18 +88,44 @@ def tv_option_bytes_rust(name, value, pad):
else: else:
print('%s%s: None,' % (pad, name)) print('%s%s: None,' % (pad, name))
def tv_option_vec_bytes_rust(name, value, pad):
if value:
print('''%s%s: Some(vec![
%s%s
%s]),''' % (
pad,
name,
pad,
chunk(hexlify(value.thing)),
pad,
))
else:
print('%s%s: None,' % (pad, name))
def tv_int_rust(name, value, pad): def tv_int_rust(name, value, pad):
print('%s%s: %d,' % (pad, name, value)) print('%s%s: %d,' % (pad, name, value))
def tv_part_rust(name, value, typ, indent=3): def tv_option_int_rust(name, value, pad):
if value:
print('%s%s: Some(%d),' % (pad, name, value.thing))
else:
print('%s%s: None,' % (pad, name))
def tv_part_rust(name, value, config, indent=3):
if 'rust_fmt' in config:
value = config['rust_fmt'](value)
pad = ' ' * indent pad = ' ' * indent
if type(value) == bytes: if config['rust_type'] == 'Option<Vec<u8>>':
if typ == 'Vec<u8>': tv_option_vec_bytes_rust(name, value, pad)
tv_vec_bytes_rust(name, value, pad) elif config['rust_type'] == 'Vec<u8>':
else: tv_vec_bytes_rust(name, value, pad)
tv_bytes_rust(name, value, pad) elif config['rust_type'].startswith('Option<['):
elif isinstance(value, Some) or value is None:
tv_option_bytes_rust(name, value, pad) tv_option_bytes_rust(name, value, pad)
elif type(value) == bytes:
tv_bytes_rust(name, value, pad)
elif config['rust_type'].startswith('Option<'):
tv_option_int_rust(name, value, pad)
elif type(value) == int: elif type(value) == int:
tv_int_rust(name, value, pad) tv_int_rust(name, value, pad)
else: else:
@ -107,7 +133,7 @@ def tv_part_rust(name, value, typ, indent=3):
def tv_rust(filename, parts, vectors): def tv_rust(filename, parts, vectors):
print(' struct TestVector {') print(' struct TestVector {')
for p in parts: print(' %s: %s,' % (p[0], p[1]['rust'])) for p in parts: print(' %s: %s,' % (p[0], p[1]['rust_type']))
print(''' }; print(''' };
// From https://github.com/zcash-hackworks/zcash-test-vectors/blob/master/%s.py''' % ( // From https://github.com/zcash-hackworks/zcash-test-vectors/blob/master/%s.py''' % (
@ -115,13 +141,13 @@ def tv_rust(filename, parts, vectors):
)) ))
if type(vectors) == type({}): if type(vectors) == type({}):
print(' let test_vector = TestVector {') print(' let test_vector = TestVector {')
for p in parts: tv_part_rust(p[0], vectors[p[0]], p[1]['rust']) for p in parts: tv_part_rust(p[0], vectors[p[0]], p[1])
print(' };') print(' };')
elif type(vectors) == type([]): elif type(vectors) == type([]):
print(' let test_vectors = vec![') print(' let test_vectors = vec![')
for vector in vectors: for vector in vectors:
print(' TestVector {') print(' TestVector {')
for p in parts: tv_part_rust(p[0], vector[p[0]], p[1]['rust'], 4) for p in parts: tv_part_rust(p[0], vector[p[0]], p[1], 4)
print(' },') print(' },')
print(' ];') print(' ];')
else: else:
@ -139,7 +165,7 @@ def render_args():
def render_tv(args, filename, parts, vectors): def render_tv(args, filename, parts, vectors):
# Convert older format # Convert older format
parts = [(p[0], p[1] if type(p[1]) == type({}) else {'rust': p[1]}) for p in parts] parts = [(p[0], p[1] if type(p[1]) == type({}) else {'rust_type': p[1]}) for p in parts]
if args.target == 'rust': if args.target == 'rust':
tv_rust(filename, parts, vectors) tv_rust(filename, parts, vectors)

View File

@ -8,7 +8,7 @@ from transaction import (
Script, Script,
Transaction, Transaction,
) )
from tv_output import render_args, render_tv from tv_output import render_args, render_tv, Some
from tv_rand import Rand from tv_rand import Rand
@ -17,7 +17,7 @@ SIGHASH_NONE = 2
SIGHASH_SINGLE = 3 SIGHASH_SINGLE = 3
SIGHASH_ANYONECANPAY = 0x80 SIGHASH_ANYONECANPAY = 0x80
NOT_AN_INPUT = -1 # For portability of the test vectors NOT_AN_INPUT = -1 # For portability of the test vectors; replaced with None for Rust
def getHashPrevouts(tx): def getHashPrevouts(tx):
digest = blake2b(digest_size=32, person=b'ZcashPrevoutHash') digest = blake2b(digest_size=32, person=b'ZcashPrevoutHash')
@ -149,9 +149,12 @@ def main():
args, args,
'zip_0143', 'zip_0143',
( (
('tx', {'rust': 'Vec<u8>', 'bitcoin_flavoured': False}), ('tx', {'rust_type': 'Vec<u8>', 'bitcoin_flavoured': False}),
('script_code', 'Vec<u8>'), ('script_code', 'Vec<u8>'),
('transparent_input', 'u32'), ('transparent_input', {
'rust_type': 'Option<u32>',
'rust_fmt': lambda x: None if x == -1 else Some(x),
}),
('hash_type', 'u32'), ('hash_type', 'u32'),
('amount', 'u64'), ('amount', 'u64'),
('consensus_branch_id', 'u32'), ('consensus_branch_id', 'u32'),