diff --git a/README.md b/README.md index 16f5aad..66e67ed 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ So we can attach it to anywhere like this: ```diff Input { - output_reference: OutputReference(TransactionId(tx_0), 0), + output_reference: OutputReference(TransactionId('tx_0'), 0), output: Output { address: Address { payment_credential: ScriptCredential('script_hash') @@ -105,17 +105,21 @@ Input { + |> log(stringify.input) ``` +> You can use defined loggers inside the `debug` module + ## Supported serializers - [x] input - [x] output - [x] credential +- [x] address - [x] value - [x] minted_value - [x] data - [x] out_ref - [x] tx - [x] redeemers +- [ ] any ## License diff --git a/lib/debug.ak b/lib/debug.ak new file mode 100644 index 0000000..6f0f22d --- /dev/null +++ b/lib/debug.ak @@ -0,0 +1,153 @@ +use aiken/builtin +use aiken/dict.{Dict} +use aiken/string +use aiken/transaction.{ + Datum, Input, Output, OutputReference, Redeemer, ScriptPurpose, Transaction, +} +use aiken/transaction/credential.{Address, Credential} +use aiken/transaction/value.{MintedValue, Value} +use stringify + +fn debug(self: a, name: String, serializer: fn(a) -> String) -> a { + builtin.debug(string.concat(name, serializer(self)), self) +} + +pub fn diagnostic(self: a, name: String, d: String) -> a { + debug( + self, + string.concat( + name, + @"\n", + ), + fn(_) { stringify.format_cbor(d) |> stringify.indent }, + ) +} + +pub fn data(self: Data, name: String) { + debug( + self, + string.concat( + name, + @"\n", + ), + stringify.data, + ) +} + +pub fn tx(self: Transaction) { + debug( + self, + @"\n", + stringify.tx, + ) +} + +pub fn input(self: Input, name: String) { + debug( + self, + string.concat( + name, + @"\n", + ), + stringify.input, + ) +} + +pub fn output(self: Output, name: String) { + debug( + self, + string.concat( + name, + @"\n", + ), + stringify.output, + ) +} + +pub fn value(self: Value, name: String) { + debug( + self, + string.concat( + name, + @"\n", + ), + stringify.value, + ) +} + +pub fn minted_value(self: MintedValue, name: String) { + debug( + self, + string.concat( + name, + @"\n", + ), + stringify.minted_value, + ) +} + +pub fn int(self: Int, name: String) { + debug( + self, + string.concat( + name, + @"\n", + ), + string.from_int, + ) +} + +pub fn redeemers(self: Dict, name: String) { + debug( + self, + string.concat( + name, + @"\n", + ), + stringify.redeemers, + ) +} + +pub fn credential(self: Credential, name: String) { + debug( + self, + string.concat( + name, + @"\n", + ), + stringify.credential, + ) +} + +pub fn out_ref(self: OutputReference, name: String) { + debug( + self, + string.concat( + name, + @"\n", + ), + stringify.out_ref, + ) +} + +pub fn address(self: Address, name: String) { + debug( + self, + string.concat( + name, + @"\n", + ), + stringify.address, + ) +} + +pub fn datum(self: Datum, name: String) { + debug( + self, + string.concat( + name, + @"\n", + ), + stringify.datum, + ) +} diff --git a/lib/stringify.ak b/lib/stringify.ak index e5cdc0b..45fa571 100644 --- a/lib/stringify.ak +++ b/lib/stringify.ak @@ -19,7 +19,7 @@ pub fn int(val) -> String { string.from_int(val) } -fn credential(cred: Credential) -> String { +pub fn credential(cred: Credential) -> String { when cred is { VerificationKeyCredential(hash) -> string.concat( @@ -246,10 +246,6 @@ pub fn indent(str: String) { do_indent(string.to_bytearray(str), 0) |> bytearray.to_string } -pub fn log(self: a, msg: String, serializer: fn(a) -> String) -> a { - builtin.debug(string.concat(msg, serializer(self)), self) -} - fn do_indent(bytes: ByteArray, lvl: Int) { let next_newline_cursor = bytearray.index_of(bytes, "\n") when next_newline_cursor is { @@ -329,30 +325,30 @@ fn as_block(builder: String) { |> string.join(builder) } -pub fn first_of_quad(quad: (a, b, c, d)) { +fn first_of_quad(quad: (a, b, c, d)) { quad.1st } -pub fn first_of_triple(tuple: (a, b, c)) { +fn first_of_triple(tuple: (a, b, c)) { tuple.1st } -pub fn pop_back(self: ByteArray, qty: Int) { +fn pop_back(self: ByteArray, qty: Int) { bytearray.take(self, builtin.length_of_bytearray(self) - qty) } -pub fn last_char(self: ByteArray) { +fn last_char(self: ByteArray) { bytearray.drop(self, builtin.length_of_bytearray(self) - 1) } -pub fn is_open(chr: ByteArray) { +fn is_open(chr: ByteArray) { or { chr == "[", chr == "{", } } -pub fn is_close(chr: ByteArray) { +fn is_close(chr: ByteArray) { or { chr == "]", chr == "}", @@ -363,7 +359,7 @@ pub fn newline(self: ByteArray) { self |> bytearray.concat("\n") } -pub fn to_decimal(byte: Int) { +fn to_decimal(byte: Int) { if byte >= 97 && byte <= 102 { byte - 97 + 10 } else if byte >= 65 && byte <= 70 { @@ -375,7 +371,7 @@ pub fn to_decimal(byte: Int) { } } -pub fn hex_to_string(hex: ByteArray) { +fn hex_to_string(hex: ByteArray) { bytearray.foldl( // pushing a dummy byte to flush the last character hex |> bytearray.concat("0"), @@ -398,18 +394,18 @@ pub fn hex_to_string(hex: ByteArray) { |> first_of_triple } -fn format_cbor(cbr: String) { +pub fn format_cbor(cbr: String) { cbr |> bytearray.from_string |> do_format_cbor |> bytearray.to_string } -pub fn to_bytearray(byte: Int) { +fn to_bytearray(byte: Int) { #"" |> bytearray.push(byte) } -pub fn do_format_cbor(bytes) { +fn do_format_cbor(bytes) { bytes |> bytearray.foldl( ("", 0, False, ""), diff --git a/lib/stringify_test.ak b/lib/stringify_test.ak index f1af827..49bddab 100644 --- a/lib/stringify_test.ak +++ b/lib/stringify_test.ak @@ -10,15 +10,10 @@ use aiken/transaction/credential.{ Address, Inline, ScriptCredential, VerificationKeyCredential, } use aiken/transaction/value -use stringify - -fn log(self: a, serializer: fn(a) -> String) { - trace serializer(self) - self -} +use debug test log_int() { - ( 1 |> log(stringify.int) ) == 1 + ( 1 |> debug.int(@"int") ) == 1 } test log_value() { @@ -27,7 +22,7 @@ test log_value() { |> value.add("pid", "name", 1) ( my_value - |> log(stringify.value) + |> debug.value(@"value") ) == my_value } @@ -45,7 +40,10 @@ test log_input() { reference_script: None, }, } - ( my_input |> log(stringify.input) ) == my_input + ( + my_input + |> debug.input(@"input") + ) == my_input } test log_output() { @@ -59,7 +57,7 @@ test log_output() { datum: NoDatum, reference_script: None, } - ( my_output |> log(stringify.output) ) == my_output + ( my_output |> debug.output(@"output") ) == my_output } type MyDatum { @@ -89,7 +87,7 @@ test log_output_2() { ), reference_script: None, } - ( my_output |> log(stringify.output) ) == my_output + ( my_output |> debug.output(@"output with datum") ) == my_output } test log_data() { @@ -101,7 +99,10 @@ test log_data() { int_list: [1, 2, 3, 4], bytes_list: ["1", "2", "3"], } - ( struct |> log(fn(d) { stringify.data(d) }) ) == struct + ( + struct + |> debug.diagnostic(@"data", cbor.diagnostic(struct)) + ) == struct } fn script_purpose_compare(a: ScriptPurpose, b: ScriptPurpose) { @@ -184,5 +185,5 @@ test log_tx() { }, ], } - ( tx |> log(stringify.tx) ) == tx + ( tx |> debug.tx ) == tx }