nupp.serde.debug
Debug rendering turns any bound value into readable text for logs, assertions, and error messages. See nupp.derive for the Debug derive and its field annotations.
local serde = require("nupp.serde")
local debug = require("nupp.serde.debug")
local record Login
user: string
(redact = true)
password: string
end
assert(debug.render(debug.binding(Login), new Login(user = "ada", password = "x"))
== 'Login { user = "ada", password = <redacted> }')The text is for people, not for reading back: it has no decoder and its format may change. binding honours the Debug derive's field annotations and a declaration's own Debug method; any other binding renders through its adapter as written, so a model library's values render without a derive.
Module contents
Types
| Type | Kind | Description |
|---|---|---|
Renderer | interface | Renders values, keeping what it selected for each binding. |
Functions
| Function | Kind | Description |
|---|---|---|
binding | function | Builds the binding Debug renders a declaration through. |
derived | function | Renders a value through its declaration's Debug binding. |
render | function | Renders a value as text. |
renderer | function | Makes a Renderer. |
write | function | Appends a value's text to a Buffer. |
Types#
Rendererinterface#
interface Renderer ...Renders values, keeping what it selected for each binding.
The module-level functions share one; hold a renderer of your own to bound or clear what it retains.
Members
| Name | Kind | Description |
|---|---|---|
render | method | Renders a value. |
write | method | Appends a rendered value. |
clear | method | Forgets every binding's selections. |
cachedBindings | method | Returns how many bindings have selections retained. |
rendermethod#
Renders a value. See render.
Arguments
| Name | Type | Description |
|---|---|---|
? | self | |
selectedBinding | core.Binding<T> | |
borrows value | T |
Returns
| Type | Description |
|---|---|
string |
writemethod#
write: function<T>(self, selectedBinding: core.Binding<T>, borrows value: T, exclusive buffer: text.Buffer): nilAppends a rendered value. See write.
Arguments
| Name | Type | Description |
|---|---|---|
? | self | |
selectedBinding | core.Binding<T> | |
borrows value | T | |
exclusive buffer | text.Buffer |
Returns
| Type | Description |
|---|---|
nil |
clearmethod#
clear: function(self): nilForgets every binding's selections.
Arguments
| Name | Type | Description |
|---|---|---|
? | self |
Returns
| Type | Description |
|---|---|
nil |
cachedBindingsmethod#
cachedBindings: function(self): integerReturns how many bindings have selections retained.
Arguments
| Name | Type | Description |
|---|---|---|
? | self |
Returns
| Type | Description |
|---|---|
integer |
Functions#
bindingfunction#
Builds the binding Debug renders a declaration through.
Like nupp.serde.binding, with Debug's own selection: skipped and redacted fields are never read, so a field that has no other representation can still be rendered around.
Type parameters
| Name | Description |
|---|---|
T |
Arguments
| Name | Type | Description |
|---|---|---|
target | Type<T> | the record or struct declaration |
Returns
| Type | Description |
|---|---|
core.Binding<T> | the binding, cached on the declaration |
derivedfunction#
Renders a value through its declaration's Debug binding. The Debug derive forwards to this; call render instead.
Arguments
| Name | Type | Description |
|---|---|---|
target | Type<unknown> | the declaration |
borrows value | unknown | the value |
Returns
| Type | Description |
|---|---|
string | the text |
renderfunction#
function render<T>(selectedBinding: core.Binding<T>, borrows value: T): stringRenders a value as text.
Type parameters
| Name | Description |
|---|---|
T |
Arguments
| Name | Type | Description |
|---|---|---|
selectedBinding | core.Binding<T> | the value's binding |
borrows value | T | the value |
Returns
| Type | Description |
|---|---|
string | the text |
rendererfunction#
function renderer(capacity: integer?): RendererMakes a Renderer.
Arguments
| Name | Type | Description |
|---|---|---|
capacity | integer? | the most bindings to retain selections for; nil is 256 |
Returns
| Type | Description |
|---|---|
Renderer | the renderer |
Raises
| Type | Condition |
|---|---|
string | when capacity is not positive |
writefunction#
function write<T>(selectedBinding: core.Binding<T>, borrows value: T, exclusive buffer: text.Buffer): nilAppends a value's text to a Buffer.
local serde = require("nupp.serde")
local debug = require("nupp.serde.debug")
local struct Vec2
x: float
y: float
end
local output = nupp.text.newBuffer()
debug.write(serde.binding(Vec2), new Vec2(1.25, 2.5), output)
assert(output:tostring() == "Vec2 { x = 1.25, y = 2.5 }")Type parameters
| Name | Description |
|---|---|
T |
Arguments
| Name | Type | Description |
|---|---|---|
selectedBinding | core.Binding<T> | the value's binding |
borrows value | T | the value |
exclusive buffer | text.Buffer | the buffer to append to |
Returns
| Type | Description |
|---|---|
nil |