My own corner of monopam
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

ocaml-cbor: enable MDX on lib/cbor.mli, fix broken doc examples

Run mdx on lib/cbor.mli so the two {[ ... ]} odoc blocks now
type-check.

Both blocks referenced `Value.X` paths (`Value.Obj`, `Value.fix`,
`Value.Variant`, `Value.encode_string`) -- those don't exist; the
codec API lives at the top level (`Cbor.Obj`, `Cbor.fix`,
`Cbor.Variant`, `Cbor.encode_string`).

Cbor.Obj.mem also takes a getter `('o -> 'a)` between the field name
and the codec -- the example was missing that argument. Restructured
the record-codec block as a real Obj.seal expression with concrete
getters, and added a roundtrip assert (decode of encode equals the
input). The Variant fix block now uses the right module path; the
flow was already correct.

+19 -19
+17 -17
ocaml-cbor/lib/cbor.mli
··· 16 16 {2 Quick Start} 17 17 18 18 {[ 19 - (* Define a codec for a record type *) 20 19 type person = { name : string; age : int } 21 20 22 - let person_codec = 23 - let open Value.Obj in 24 - let* name = mem "name" Value.string in 25 - let* age = mem "age" Value.int in 26 - return { name; age } ~enc:(fun p -> 27 - field "name" p.name @@ field "age" p.age @@ empty) 21 + let person_codec : person Cbor.t = 22 + let open Cbor.Obj in 23 + seal 24 + (let* name = mem "name" (fun p -> p.name) Cbor.string in 25 + let* age = mem "age" (fun p -> p.age) Cbor.int in 26 + return { name; age }) 28 27 29 - (* Encode to CBOR bytes *) 30 - let cbor_bytes = 31 - Value.encode_string person_codec { name = "Alice"; age = 30 } 28 + let alice = { name = "Alice"; age = 30 } 29 + let cbor_bytes = Cbor.encode_string person_codec alice 32 30 33 - (* Decode from CBOR bytes *) 34 - let person = Value.decode_string person_codec cbor_bytes 31 + let () = 32 + match Cbor.decode_string person_codec cbor_bytes with 33 + | Ok p -> assert (p = alice) 34 + | Error e -> Fmt.failwith "decode: %a" Cbor.Error.pp e 35 35 ]} 36 36 37 37 {2 Data Model} ··· 310 310 {[ 311 311 type tree = Leaf of int | Node of tree * tree 312 312 313 - let tree_codec = 314 - Value.fix @@ fun self -> 315 - Value.Variant.( 313 + let tree_codec : tree Cbor.t = 314 + Cbor.fix @@ fun self -> 315 + Cbor.Variant.( 316 316 variant 317 317 [ 318 - case 0 Value.int 318 + case 0 Cbor.int 319 319 (fun x -> Leaf x) 320 320 (function Leaf x -> Some x | _ -> None); 321 - case 1 (Value.tuple2 self self) 321 + case 1 (Cbor.tuple2 self self) 322 322 (fun (l, r) -> Node (l, r)) 323 323 (function Node (l, r) -> Some (l, r) | _ -> None); 324 324 ])
+2 -2
ocaml-cbor/lib/dune
··· 8 8 (re_export nox-loc))) 9 9 10 10 (mdx 11 - (files value.mli binary.mli) 12 - (libraries nox-cbor zarith bytesrw)) 11 + (files value.mli binary.mli cbor.mli) 12 + (libraries nox-cbor zarith bytesrw fmt))