OCaml library for JSONfeed parsing and creation

further code cleanup using Option combinators

+24 -36
+8 -8
lib/attachment.ml
··· 46 46 47 47 Format.fprintf ppf "%s (%s" filename t.mime_type; 48 48 49 - (match t.size_in_bytes with 50 - | Some size -> 49 + Option.iter 50 + (fun size -> 51 51 let mb = Int64.to_float size /. (1024. *. 1024.) in 52 - Format.fprintf ppf ", %.1f MB" mb 53 - | None -> ()); 52 + Format.fprintf ppf ", %.1f MB" mb) 53 + t.size_in_bytes; 54 54 55 - (match t.duration_in_seconds with 56 - | Some duration -> 55 + Option.iter 56 + (fun duration -> 57 57 let mins = duration / 60 in 58 58 let secs = duration mod 60 in 59 - Format.fprintf ppf ", %dm%ds" mins secs 60 - | None -> ()); 59 + Format.fprintf ppf ", %dm%ds" mins secs) 60 + t.duration_in_seconds; 61 61 62 62 Format.fprintf ppf ")" 63 63
+2 -8
lib/item.ml
··· 85 85 let equal a b = a.id = b.id 86 86 87 87 let compare a b = 88 - match (a.date_published, b.date_published) with 89 - | None, None -> 0 90 - | None, Some _ -> -1 91 - | Some _, None -> 1 92 - | Some da, Some db -> Ptime.compare da db 88 + Option.compare Ptime.compare a.date_published b.date_published 93 89 94 90 let pp ppf t = 95 91 match (t.date_published, t.title) with ··· 103 99 | None, None -> Format.fprintf ppf "%s" t.id 104 100 105 101 let pp_summary ppf t = 106 - match t.title with 107 - | Some title -> Format.fprintf ppf "%s" title 108 - | None -> Format.fprintf ppf "%s" t.id 102 + Format.fprintf ppf "%s" (Option.value ~default:t.id t.title) 109 103 110 104 (* Jsont type *) 111 105
+12 -18
lib/jsonfeed.ml
··· 163 163 add_error "items must have unique IDs"; 164 164 165 165 (* Validate authors *) 166 - (match feed.authors with 167 - | Some authors -> 168 - List.iteri 169 - (fun i author -> 170 - if not (Author.is_valid author) then 171 - add_error 172 - (Printf.sprintf 173 - "feed author %d is invalid (needs at least one field)" i)) 174 - authors 175 - | None -> ()); 166 + Option.iter 167 + (List.iteri (fun i author -> 168 + if not (Author.is_valid author) then 169 + add_error 170 + (Printf.sprintf "feed author %d is invalid (needs at least one field)" 171 + i))) 172 + feed.authors; 176 173 177 174 (* Validate items *) 178 175 List.iteri ··· 181 178 add_error (Printf.sprintf "item %d has empty ID" i); 182 179 183 180 (* Validate item authors *) 184 - match Item.authors item with 185 - | Some authors -> 186 - List.iteri 187 - (fun j author -> 188 - if not (Author.is_valid author) then 189 - add_error (Printf.sprintf "item %d author %d is invalid" i j)) 190 - authors 191 - | None -> ()) 181 + Option.iter 182 + (List.iteri (fun j author -> 183 + if not (Author.is_valid author) then 184 + add_error (Printf.sprintf "item %d author %d is invalid" i j))) 185 + (Item.authors item)) 192 186 feed.items; 193 187 194 188 match !errors with [] -> Ok () | errs -> Error (List.rev errs)
+1 -1
lib/reference.ml
··· 29 29 let pp ppf t = 30 30 let open Format in 31 31 fprintf ppf "%s" t.url; 32 - match t.doi with Some d -> fprintf ppf " [DOI: %s]" d | None -> () 32 + Option.iter (fprintf ppf " [DOI: %s]") t.doi 33 33 34 34 let jsont = 35 35 let kind = "Reference" in
+1 -1
lib/rfc3339.ml
··· 4 4 ---------------------------------------------------------------------------*) 5 5 6 6 let parse s = 7 - match Ptime.of_rfc3339 s with Ok (t, _, _) -> Some t | Error _ -> None 7 + Ptime.of_rfc3339 s |> Result.to_option |> Option.map (fun (t, _, _) -> t) 8 8 9 9 let format t = Ptime.to_rfc3339 ~frac_s:6 ~tz_offset_s:0 t 10 10 let pp ppf t = Format.pp_print_string ppf (format t)