OCaml library for JSONfeed parsing and creation
1(*---------------------------------------------------------------------------
2 Copyright (c) 2024 Anil Madhavapeddy. All rights reserved.
3 SPDX-License-Identifier: ISC
4 ---------------------------------------------------------------------------*)
5
6module Unknown = struct
7 type t = (string * Jsont.json) list
8
9 let empty = []
10 let is_empty = function [] -> true | _ -> false
11end
12
13type t = {
14 url : string;
15 mime_type : string;
16 title : string option;
17 size_in_bytes : int64 option;
18 duration_in_seconds : int option;
19 unknown : Unknown.t;
20}
21
22let create ~url ~mime_type ?title ?size_in_bytes ?duration_in_seconds
23 ?(unknown = Unknown.empty) () =
24 { url; mime_type; title; size_in_bytes; duration_in_seconds; unknown }
25
26let url t = t.url
27let mime_type t = t.mime_type
28let title t = t.title
29let size_in_bytes t = t.size_in_bytes
30let duration_in_seconds t = t.duration_in_seconds
31let unknown t = t.unknown
32
33let equal a b =
34 a.url = b.url && a.mime_type = b.mime_type && a.title = b.title
35 && a.size_in_bytes = b.size_in_bytes
36 && a.duration_in_seconds = b.duration_in_seconds
37
38let pp ppf t =
39 (* Extract filename from URL *)
40 let filename =
41 try
42 let parts = String.split_on_char '/' t.url in
43 List.nth parts (List.length parts - 1)
44 with _ -> t.url
45 in
46
47 Format.fprintf ppf "%s (%s" filename t.mime_type;
48
49 (match t.size_in_bytes with
50 | Some size ->
51 let mb = Int64.to_float size /. (1024. *. 1024.) in
52 Format.fprintf ppf ", %.1f MB" mb
53 | None -> ());
54
55 (match t.duration_in_seconds with
56 | Some duration ->
57 let mins = duration / 60 in
58 let secs = duration mod 60 in
59 Format.fprintf ppf ", %dm%ds" mins secs
60 | None -> ());
61
62 Format.fprintf ppf ")"
63
64let jsont =
65 let kind = "Attachment" in
66 let doc = "An attachment object" in
67 let unknown_mems :
68 (Unknown.t, Jsont.json, Jsont.mem list) Jsont.Object.Mems.map =
69 let open Jsont.Object.Mems in
70 let dec_empty () = [] in
71 let dec_add _meta (name : string) value acc =
72 ((name, Jsont.Meta.none), value) :: acc
73 in
74 let dec_finish _meta mems =
75 List.rev_map (fun ((name, _meta), value) -> (name, value)) mems
76 in
77 let enc =
78 {
79 enc =
80 (fun (type acc)
81 (f : Jsont.Meta.t -> string -> Jsont.json -> acc -> acc)
82 unknown
83 (acc : acc)
84 ->
85 List.fold_left
86 (fun acc (name, value) -> f Jsont.Meta.none name value acc)
87 acc unknown);
88 }
89 in
90 map ~kind:"Unknown members" Jsont.json ~dec_empty ~dec_add ~dec_finish ~enc
91 in
92 let create_obj url mime_type title size_in_bytes duration_in_seconds unknown =
93 create ~url ~mime_type ?title ?size_in_bytes ?duration_in_seconds ~unknown
94 ()
95 in
96 Jsont.Object.map ~kind ~doc create_obj
97 |> Jsont.Object.mem "url" Jsont.string ~enc:url
98 |> Jsont.Object.mem "mime_type" Jsont.string ~enc:mime_type
99 |> Jsont.Object.opt_mem "title" Jsont.string ~enc:title
100 |> Jsont.Object.opt_mem "size_in_bytes" Jsont.int64 ~enc:size_in_bytes
101 |> Jsont.Object.opt_mem "duration_in_seconds" Jsont.int
102 ~enc:duration_in_seconds
103 |> Jsont.Object.keep_unknown unknown_mems ~enc:unknown
104 |> Jsont.Object.finish