diff --git a/tv_output.py b/tv_output.py index 47e9168..f4ecae5 100644 --- a/tv_output.py +++ b/tv_output.py @@ -10,6 +10,12 @@ def chunk(h): hstr = str(h, 'utf-8') return '0x' + ', 0x'.join([hstr[i:i+2] for i in range(0, len(hstr), 2)]) +class Some(object): + def __init__(self, thing): + self.thing = thing + +def option(x): + return Some(x) if x else None # # JSON (with string comments) @@ -17,6 +23,9 @@ def chunk(h): # def tv_value_json(value, bitcoin_flavoured): + if isinstance(value, Some): + value = value.thing + if type(value) == bytes: if bitcoin_flavoured and len(value) == 32: value = value[::-1] @@ -54,6 +63,20 @@ def tv_bytes_rust(name, value, pad): pad, )) +def tv_option_bytes_rust(name, value, pad): + if value: + print('''%s%s: Some([ + %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)) @@ -61,6 +84,8 @@ def tv_part_rust(name, value, indent=3): pad = ' ' * indent if type(value) == bytes: tv_bytes_rust(name, value, pad) + elif isinstance(value, Some) or value is None: + tv_option_bytes_rust(name, value, pad) elif type(value) == int: tv_int_rust(name, value, pad) else: