My own corner of monopam
2
fork

Configure Feed

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

ocaml-mbr: rewrite README examples as top-level bindings

The blocks chained match expressions at module top level (which
doesn't parse) and referenced unbound [reader] / [writer]. Recast
each block: extract intermediate values via [let part = match ...],
wrap streaming examples as functions taking [reader] / [writer], and
swap [Printf.printf] for [Fmt.pr] to match the rest of the README.

+34 -27
+34 -27
ocaml-mbr/README.md
··· 41 41 ### Creating an MBR 42 42 43 43 ```ocaml 44 - (* Create a partition: FAT16 (type 6), starting at sector 2048, 32MB *) 45 - match Mbr.Partition.make ~active:true ~partition_type:6 2048l 65536l with 46 - | Error e -> failwith e 47 - | Ok part -> 48 - 49 - match Mbr.v [ part ] with 44 + (* FAT16 (type 6), starting at sector 2048, 32MB. *) 45 + let part = 46 + match Mbr.Partition.make ~active:true ~partition_type:6 2048l 65536l with 47 + | Ok p -> p 50 48 | Error e -> failwith e 51 - | Ok mbr -> Fmt.pr "%a@." Mbr.pp mbr 49 + 50 + let mbr = 51 + match Mbr.v [ part ] with 52 + | Ok m -> m 53 + | Error e -> failwith e 54 + 55 + let () = Fmt.pr "%a@." Mbr.pp mbr 52 56 ``` 53 57 54 58 ### Serialization 55 59 56 60 ```ocaml 57 - (* Write to string *) 58 61 let bytes = Mbr.to_string mbr 59 62 60 - (* Read from string *) 61 - match Mbr.of_string bytes with 62 - | Ok mbr -> List.iter (fun p -> 63 - Printf.printf "Partition: start=%Ld, size=%Ld\n" 64 - (Mbr.Partition.sector_start p) 65 - (Mbr.Partition.size_sectors p)) 66 - (Mbr.partitions mbr) 67 - | Error e -> failwith e 63 + let () = 64 + match Mbr.of_string bytes with 65 + | Ok mbr -> 66 + List.iter 67 + (fun p -> 68 + Fmt.pr "Partition: start=%Ld, size=%Ld@." 69 + (Mbr.Partition.sector_start p) 70 + (Mbr.Partition.size_sectors p)) 71 + (Mbr.partitions mbr) 72 + | Error e -> failwith e 68 73 ``` 69 74 70 75 ### Streaming I/O with bytesrw 71 76 72 77 ```ocaml 73 - (* Read *) 74 - match Mbr.read reader with 75 - | Ok mbr -> Fmt.pr "%a@." Mbr.pp mbr 76 - | Error e -> failwith e 78 + let read_mbr reader = 79 + match Mbr.read reader with 80 + | Ok mbr -> 81 + Fmt.pr "%a@." Mbr.pp mbr; 82 + mbr 83 + | Error e -> failwith e 77 84 78 - (* Write *) 79 - Mbr.write writer mbr 85 + let write_mbr writer mbr = Mbr.write writer mbr 80 86 ``` 81 87 82 88 ### CHS Geometry 83 89 84 90 ```ocaml 85 - (* Synthesise geometry for a 4GB disk *) 86 - match Mbr.Geometry.of_lba_size 8388608L with 87 - | Ok geom -> Printf.printf "C=%d H=%d S=%d\n" 88 - geom.cylinders geom.heads geom.sectors 89 - | Error e -> failwith e 91 + (* Synthesise geometry for a 4GB disk. *) 92 + let () = 93 + match Mbr.Geometry.of_lba_size 8388608L with 94 + | Ok geom -> 95 + Fmt.pr "C=%d H=%d S=%d@." geom.cylinders geom.heads geom.sectors 96 + | Error e -> failwith e 90 97 ``` 91 98 92 99 ## API