{"name":"msgpack_demo.nu","source":"// examples/msgpack_demo.nu — round-trip a user struct through\n// MessagePack.\n//\n// MessagePack and JSON share a data model, so a type that already has\n// a `% JsonSerialize` impl needs NO MsgPack-specific code: encode by\n// composing `to_json` with `msgpack_encode`, decode with\n// `msgpack_decode` plus the type's `from_json` helper. Scalars have\n// the `from_msgpack_<T>` shortcut. Mirror this shape for your types.\n//\n// Build:\n//   ./nurl.sh examples/msgpack_demo.nu /tmp/msgpack_demo\n//   /tmp/msgpack_demo\n\n$ `stdlib/ext/serde.nu`\n\n: Point { i x i y }\n\n// The SAME JsonSerialize impl JSON uses — MsgPack reuses it.\n% JsonSerialize Point {\n    @ to_json Point p → Json {\n        : Json j ( json_obj_new )\n        ( json_obj_set j `x` ( to_json . p x ) )\n        ( json_obj_set j `y` ( to_json . p y ) )\n        ^ j\n    }\n}\n\n@ point_from_json Json j → !Point ParseErr {\n    : ?Json jxo ( json_obj_get j `x` )\n    : ?Json jyo ( json_obj_get j `y` )\n    ?? jxo {\n        T jx → {\n            ?? jyo {\n                T jy → {\n                    : !i ParseErr xr ( from_json_i jx )\n                    : !i ParseErr yr ( from_json_i jy )\n                    ?? xr {\n                        T xv → {\n                            ?? yr {\n                                T yv → ^ @ !Point ParseErr { T @ Point { xv yv } }\n                                F e → ^ @ !Point ParseErr { F e }\n                            }\n                        }\n                        F e → ^ @ !Point ParseErr { F e }\n                    }\n                }\n                F → ^ @ !Point ParseErr { F @ ParseErr { BadFormat } }\n            }\n        }\n        F → ^ @ !Point ParseErr { F @ ParseErr { BadFormat } }\n    }\n}\n\n@ main → v {\n    : Point p @ Point { 3 4 }\n\n    // Encode: trait dispatch routes ( to_json p ) to the Point impl;\n    // msgpack_encode turns the Json into MessagePack bytes.\n    : Json j ( to_json p )\n    // Bind the option to a named variable first — passing a call result\n    // directly to `??` with a wide `Vec` payload tickles a known nurlc\n    // generic-monomorphize IR bug (see nurl_lang_gotchas memory). The\n    // scrutinee binding works around it.\n    : !( Vec u ) MsgpackErr _enc_res ( msgpack_encode j )\n    ?? _enc_res {\n        T bytes → {\n            ( nurl_print `encoded:  ` )\n            ( nurl_print ( nurl_str_int ( vec_len [u] bytes ) ) )\n            ( nurl_print ` bytes\\n` )\n\n            // Decode: bytes → Json → Point.\n            ?? ( msgpack_decode bytes ) {\n                T j2 → {\n                    ?? ( point_from_json j2 ) {\n                        T p2 → {\n                            ( nurl_print `decoded:  Point { ` )\n                            ( nurl_print ( nurl_str_int . p2 x ) )\n                            ( nurl_print ` ` )\n                            ( nurl_print ( nurl_str_int . p2 y ) )\n                            ( nurl_print ` }\\n` )\n                        }\n                        F _ → ( nurl_print `decode FAIL\\n` )\n                    }\n                    ( json_free j2 )\n                }\n                F _ → ( nurl_print `msgpack decode FAIL\\n` )\n            }\n            ( vec_free [u] bytes )\n        }\n        F _ → ( nurl_print `msgpack encode FAIL\\n` )\n    }\n    ( json_free j )\n\n    // A scalar needs no struct plumbing — from_msgpack_i decodes\n    // straight to an i.\n    : Json sj ( json_int 1729 )\n    : !( Vec u ) MsgpackErr _enc_sj ( msgpack_encode sj )\n    ?? _enc_sj {\n        T sb → {\n            : !i MsgpackErr _dec_sj ( from_msgpack_i sb )\n            ?? _dec_sj {\n                T n → {\n                    ( nurl_print `scalar:   ` )\n                    ( nurl_print ( nurl_str_int n ) )\n                    ( nurl_print `\\n` )\n                }\n                F _ → ( nurl_print `scalar FAIL\\n` )\n            }\n            ( vec_free [u] sb )\n        }\n        F _ → ( nurl_print `scalar encode FAIL\\n` )\n    }\n    ( json_free sj )\n}\n","bytes":3988}