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:
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):
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
if type(value) == bytes:
if typ == 'Vec<u8>':
tv_vec_bytes_rust(name, value, pad)
else:
tv_bytes_rust(name, value, pad)
elif isinstance(value, Some) or value is None:
if config['rust_type'] == 'Option<Vec<u8>>':
tv_option_vec_bytes_rust(name, value, pad)
elif config['rust_type'] == 'Vec<u8>':
tv_vec_bytes_rust(name, value, pad)
elif config['rust_type'].startswith('Option<['):
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:
tv_int_rust(name, value, pad)
else:
@ -107,7 +133,7 @@ def tv_part_rust(name, value, typ, indent=3):
def tv_rust(filename, parts, vectors):
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(''' };
// 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({}):
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(' };')
elif type(vectors) == type([]):
print(' let test_vectors = vec![')
for vector in vectors:
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(' ];')
else:
@ -139,7 +165,7 @@ def render_args():
def render_tv(args, filename, parts, vectors):
# 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':
tv_rust(filename, parts, vectors)

View File

@ -8,7 +8,7 @@ from transaction import (
Script,
Transaction,
)
from tv_output import render_args, render_tv
from tv_output import render_args, render_tv, Some
from tv_rand import Rand
@ -17,7 +17,7 @@ SIGHASH_NONE = 2
SIGHASH_SINGLE = 3
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):
digest = blake2b(digest_size=32, person=b'ZcashPrevoutHash')
@ -149,9 +149,12 @@ def main():
args,
'zip_0143',
(
('tx', {'rust': 'Vec<u8>', 'bitcoin_flavoured': False}),
('tx', {'rust_type': 'Vec<u8>', 'bitcoin_flavoured': False}),
('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'),
('amount', 'u64'),
('consensus_branch_id', 'u32'),