(** Error handling for geographic coordinates *) type t = | Invalid_latitude of float | Invalid_longitude of float | Parse_error of string | Invalid_format of string | Invalid_hemisphere of string exception Coordinate_error of t let to_string = function | Invalid_latitude f -> Printf.sprintf "Invalid latitude: %.6f (must be between -90 and 90)" f | Invalid_longitude f -> Printf.sprintf "Invalid longitude: %.6f" f | Parse_error s -> Printf.sprintf "Parse error: %s" s | Invalid_format s -> Printf.sprintf "Invalid format: %s" s | Invalid_hemisphere s -> Printf.sprintf "Invalid hemisphere: %s" s let catch f x = try Ok (f x) with Coordinate_error e -> Error e let catch_exn f x = try Some (f x) with Coordinate_error _ -> None let unwrap = function | Ok x -> x | Error e -> raise (Coordinate_error e) let unwrap_or default = function | Ok x -> x | Error _ -> default