Skip to content

Commit

Permalink
Prefer pattern matching over try-with.
Browse files Browse the repository at this point in the history
pattern matching seems more readable for exception handling than using a
try-with block.
  • Loading branch information
zoj613 committed Sep 13, 2024
1 parent a3fc2a2 commit 54a6aa5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
8 changes: 4 additions & 4 deletions zarr-eio/src/storage.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ module FilesystemStore = struct
Eio.Path.with_open_in (key_to_fspath t key) @@ fun flow ->
Optint.Int63.to_int @@ Eio.File.size flow

let get t key =
try Eio.Path.load @@ key_to_fspath t key with
| Eio.Io (Eio.Fs.E Not_found Eio_unix.Unix_error _, _) ->
raise (Zarr.Storage.Key_not_found key)
let get t key = match Eio.Path.load @@ key_to_fspath t key with
| exception Eio.Io (Eio.Fs.E Not_found Eio_unix.Unix_error _, _) ->
raise @@ Zarr.Storage.Key_not_found key
| v -> v

let get_partial_values t key ranges =
Eio.Path.with_open_in (key_to_fspath t key) @@ fun flow ->
Expand Down
5 changes: 3 additions & 2 deletions zarr/src/codecs/array_to_array.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ module TransposeCodec = struct
| _ -> Error "transpose order values must be integers.") o (Ok []))
(fun od ->
let order = Array.of_list od in
try parse ~order chunk_shape; Ok (`Transpose order) with
| Invalid_transpose_order -> Error "Invalid_transpose_order")
match parse ~order chunk_shape with
| () -> Ok (`Transpose order)
| exception Invalid_transpose_order -> Error "Invalid_transpose_order")
| _ -> Error "Invalid transpose configuration."
end

Expand Down
5 changes: 3 additions & 2 deletions zarr/src/metadata.ml
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,9 @@ module ArrayMetadata = struct
| _ ->
Error "chunk_shape must only contain positive ints.") l (Ok []) in
let cs = Array.of_list v in
let+ r = try Ok (RegularGrid.create ~array_shape:shape cs) with
| RegularGrid.Grid_shape_mismatch -> Error "grid shape mismatch."
let+ r = match RegularGrid.create ~array_shape:shape cs with
| exception RegularGrid.Grid_shape_mismatch -> Error "grid shape mismatch."
| g -> Ok g
in cs, r
| _ -> Error "Invalid Chunk grid name or configuration."
in
Expand Down
12 changes: 6 additions & 6 deletions zarr/src/storage/storage.ml
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ module Make (Io : Types.IO) = struct
let* b = get t @@ ArrayNode.to_metakey node in
let meta = ArrayMetadata.decode b in
let shape = ArrayMetadata.shape meta in
let slice_shape =
try Indexing.slice_shape slice shape with
| Assert_failure _ -> raise Invalid_array_slice in
let slice_shape = match Indexing.slice_shape slice shape with
| exception Assert_failure _ -> raise Invalid_array_slice
| s -> s in
if Ndarray.shape x <> slice_shape then raise Invalid_array_slice else
let kind = Ndarray.data_type x in
if not @@ ArrayMetadata.is_valid_kind meta kind then raise Invalid_data_type else
Expand Down Expand Up @@ -148,9 +148,9 @@ module Make (Io : Types.IO) = struct
if not @@ ArrayMetadata.is_valid_kind meta kind
then raise Invalid_data_type else
let shape = ArrayMetadata.shape meta in
let slice_shape =
try Indexing.slice_shape slice shape with
| Assert_failure _ -> raise Invalid_array_slice in
let slice_shape = match Indexing.slice_shape slice shape with
| exception Assert_failure _ -> raise Invalid_array_slice
| s -> s in
let ic = Array.mapi (fun i v -> i, v) (Indexing.coords_of_slice slice shape) in
let m =
Array.fold_left
Expand Down

0 comments on commit 54a6aa5

Please sign in to comment.