My agentic slop goes here. Not intended for anyone else!
at main 2.0 kB view raw
1(* 2 * Copyright (c) 2014, OCaml.org project 3 * Copyright (c) 2015 KC Sivaramakrishnan <sk826@cl.cam.ac.uk> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 *) 17 18(** Custom categories for organizing posts. *) 19 20type t = { 21 id : string; 22 name : string; 23 description : string option; 24} 25 26let create ~id ~name ?description () = 27 { id; name; description } 28 29let id t = t.id 30let name t = t.name 31let description t = t.description 32 33(* Jsont codec *) 34let jsont = 35 let make id name description = { id; name; description } in 36 Jsont.Object.map ~kind:"Category" make 37 |> Jsont.Object.mem "id" Jsont.string ~enc:id 38 |> Jsont.Object.mem "name" Jsont.string ~enc:name 39 |> Jsont.Object.mem "description" (Jsont.option Jsont.string) ~enc:description 40 |> Jsont.Object.finish 41 42let to_json t = 43 match Jsont_bytesrw.encode_string jsont t with 44 | Ok json_str -> 45 (match Jsont_bytesrw.decode_string Jsont.json json_str with 46 | Ok json -> json 47 | Error err -> failwith ("Failed to decode encoded category: " ^ err)) 48 | Error err -> failwith ("Failed to encode category: " ^ err) 49 50let of_json json = 51 match Jsont_bytesrw.encode_string Jsont.json json with 52 | Ok json_str -> 53 (match Jsont_bytesrw.decode_string jsont json_str with 54 | Ok t -> Ok t 55 | Error err -> Error err) 56 | Error err -> Error err