OCaml library for JSONfeed parsing and creation

further code cleanup using Option combinators

+8 -8
lib/attachment.ml
···
Format.fprintf ppf "%s (%s" filename t.mime_type;
-
(match t.size_in_bytes with
-
| Some size ->
+
Option.iter
+
(fun size ->
let mb = Int64.to_float size /. (1024. *. 1024.) in
-
Format.fprintf ppf ", %.1f MB" mb
-
| None -> ());
+
Format.fprintf ppf ", %.1f MB" mb)
+
t.size_in_bytes;
-
(match t.duration_in_seconds with
-
| Some duration ->
+
Option.iter
+
(fun duration ->
let mins = duration / 60 in
let secs = duration mod 60 in
-
Format.fprintf ppf ", %dm%ds" mins secs
-
| None -> ());
+
Format.fprintf ppf ", %dm%ds" mins secs)
+
t.duration_in_seconds;
Format.fprintf ppf ")"
+2 -8
lib/item.ml
···
let equal a b = a.id = b.id
let compare a b =
-
match (a.date_published, b.date_published) with
-
| None, None -> 0
-
| None, Some _ -> -1
-
| Some _, None -> 1
-
| Some da, Some db -> Ptime.compare da db
+
Option.compare Ptime.compare a.date_published b.date_published
let pp ppf t =
match (t.date_published, t.title) with
···
| None, None -> Format.fprintf ppf "%s" t.id
let pp_summary ppf t =
-
match t.title with
-
| Some title -> Format.fprintf ppf "%s" title
-
| None -> Format.fprintf ppf "%s" t.id
+
Format.fprintf ppf "%s" (Option.value ~default:t.id t.title)
(* Jsont type *)
+12 -18
lib/jsonfeed.ml
···
add_error "items must have unique IDs";
(* Validate authors *)
-
(match feed.authors with
-
| Some authors ->
-
List.iteri
-
(fun i author ->
-
if not (Author.is_valid author) then
-
add_error
-
(Printf.sprintf
-
"feed author %d is invalid (needs at least one field)" i))
-
authors
-
| None -> ());
+
Option.iter
+
(List.iteri (fun i author ->
+
if not (Author.is_valid author) then
+
add_error
+
(Printf.sprintf "feed author %d is invalid (needs at least one field)"
+
i)))
+
feed.authors;
(* Validate items *)
List.iteri
···
add_error (Printf.sprintf "item %d has empty ID" i);
(* Validate item authors *)
-
match Item.authors item with
-
| Some authors ->
-
List.iteri
-
(fun j author ->
-
if not (Author.is_valid author) then
-
add_error (Printf.sprintf "item %d author %d is invalid" i j))
-
authors
-
| None -> ())
+
Option.iter
+
(List.iteri (fun j author ->
+
if not (Author.is_valid author) then
+
add_error (Printf.sprintf "item %d author %d is invalid" i j)))
+
(Item.authors item))
feed.items;
match !errors with [] -> Ok () | errs -> Error (List.rev errs)
+1 -1
lib/reference.ml
···
let pp ppf t =
let open Format in
fprintf ppf "%s" t.url;
-
match t.doi with Some d -> fprintf ppf " [DOI: %s]" d | None -> ()
+
Option.iter (fprintf ppf " [DOI: %s]") t.doi
let jsont =
let kind = "Reference" in
+1 -1
lib/rfc3339.ml
···
---------------------------------------------------------------------------*)
let parse s =
-
match Ptime.of_rfc3339 s with Ok (t, _, _) -> Some t | Error _ -> None
+
Ptime.of_rfc3339 s |> Result.to_option |> Option.map (fun (t, _, _) -> t)
let format t = Ptime.to_rfc3339 ~frac_s:6 ~tz_offset_s:0 t
let pp ppf t = Format.pp_print_string ppf (format t)