upstream: https://github.com/mirage/ocaml-gpt
0
fork

Configure Feed

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

ocaml-gpt: rewrite README examples to typecheck

The blocks chained [match ... with | Error -> ...] expressions at module
top level (which doesn't parse) and referenced unbound [reader],
[writer], [header_bytes], [read_sectors]. Recast each block as a
function or a top-level binding ladder so the snippets typecheck and
are self-contained.

+25 -24
+25 -24
README.md
··· 40 40 ### Creating partitions and GPT 41 41 42 42 ```ocaml 43 - (* Create a partition entry *) 44 43 let type_guid = 45 44 Uuidm.of_string "C12A7328-F81F-11D2-BA4B-00A0C93EC93B" |> Option.get 46 - in 47 - match Gpt.Partition.make ~type_guid ~attributes:0L 2048L 1048575L with 48 - | Error e -> failwith e 49 - | Ok part -> 50 - (* Build the GPT *) 51 - 52 - match Gpt.v ~disk_sectors:2097152L ~sector_size:512 [ part ] with 45 + 46 + let part = 47 + match Gpt.Partition.make ~type_guid ~attributes:0L 2048L 1048575L with 48 + | Ok p -> p 49 + | Error e -> failwith e 50 + 51 + let gpt = 52 + match Gpt.v ~disk_sectors:2097152L ~sector_size:512 [ part ] with 53 + | Ok g -> g 53 54 | Error e -> failwith e 54 - | Ok gpt -> Fmt.pr "%a@." Gpt.pp gpt 55 + 56 + let () = Fmt.pr "%a@." Gpt.pp gpt 55 57 ``` 56 58 57 59 ### Reading from bytes 58 60 59 61 ```ocaml 60 - match Gpt.of_string header_bytes ~sector_size:512 with 61 - | Error e -> failwith e 62 - | Ok (`Read_partition_table (lba, num_sectors), k) -> 63 - (* Read partition table at the given LBA *) 64 - 65 - let table_bytes = read_sectors lba num_sectors 66 - match k table_bytes with 67 - | Ok gpt -> Fmt.pr "%a@." Gpt.pp gpt 62 + let parse_gpt ~read_sectors header_bytes = 63 + match Gpt.of_string header_bytes ~sector_size:512 with 68 64 | Error e -> failwith e 65 + | Ok (`Read_partition_table (lba, num_sectors), k) -> 66 + let table_bytes = read_sectors lba num_sectors in 67 + (match k table_bytes with 68 + | Ok gpt -> gpt 69 + | Error e -> failwith e) 69 70 ``` 70 71 71 72 ### Streaming I/O with bytesrw 72 73 73 74 ```ocaml 74 - (* Read *) 75 - match Gpt.read reader ~sector_size:512 with 76 - | Ok gpt -> Fmt.pr "%a@." Gpt.pp gpt 77 - | Error e -> failwith e 75 + let read_gpt reader = 76 + match Gpt.read reader ~sector_size:512 with 77 + | Ok gpt -> gpt 78 + | Error e -> failwith e 78 79 79 - (* Write *) 80 - Gpt.write_header writer ~sector_size:512 ~primary:true gpt; 81 - Gpt.write_partition_table writer ~sector_size:512 gpt 80 + let write_gpt writer gpt = 81 + Gpt.write_header writer ~sector_size:512 ~primary:true gpt; 82 + Gpt.write_partition_table writer ~sector_size:512 gpt 82 83 ``` 83 84 84 85 ### Protective MBR