123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169(*
* Copyright (c) 2022-2022 Tarides <contact@tarides.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)openImport(** Finaliser for a function that returns a result and doesn't raise exceptions.
If the finaliser fails, it is recommended to log the error. *)letfinalisefinaliserf=letres=f()infinaliserres;res(** Finaliser for a function that might raise exceptions. *)letfinalise_exnfinaliserf=tryletres=f()infinaliser(Someres);reswithexn->finaliserNone;raiseexntypebase_error=[`Double_close|`File_existsofstring|`Invalid_parent_directory|`No_such_file_or_directoryofstring|`Not_a_file|`Read_out_of_bounds|`Invalid_argument|`Decoding_error|`Not_a_directoryofstring|`Index_failureofstring|`Invalid_layout|`Corrupted_legacy_file|`Corrupted_mapping_fileofstring|`Pending_flush|`Rw_not_allowed|`Migration_needed|`Migration_to_lower_not_allowed|`Corrupted_control_fileofstring|`Sys_errorofstring|`V3_store_from_the_future|`Gc_forbidden_during_batch|`Unknown_major_pack_versionofstring|`Only_minimal_indexing_strategy_allowed|`Commit_key_is_danglingofstring|`Dangling_keyofstring|`Gc_disallowedofstring|`Node_or_contents_key_is_indexedofstring|`Gc_process_errorofstring|`Corrupted_gc_result_fileofstring|`Gc_process_died_without_result_fileofstring|`Gc_forbidden_on_32bit_platforms|`Invalid_prefix_readofstring|`Invalid_sparse_readof[`After|`Before|`Hole]*int63|`Invalid_volume_readof[`Empty|`Closed]*int63|`Inconsistent_store|`Split_forbidden_during_batch|`Split_disallowed|`Multiple_empty_chunks|`Forbidden_during_gc|`Multiple_empty_volumes|`Volume_missingofstring|`Add_volume_forbidden_during_gc|`Add_volume_requires_lower|`Volume_history_newer_than_archived_dataofint63*int63|`Lower_has_no_volume|`Volume_not_foundofstring|`No_tmp_path_provided][@@derivingirmin~pp](** [base_error] is the type of most errors that can occur in a [result], except
for errors that have associated exceptions (see below) and backend-specific
errors (see {!Io_errors}). *)typeclosed_error=[`Closed][@@derivingirmin~pp]typeread_only_error=[`Ro_not_allowed][@@derivingirmin~pp]typeerror=[base_error|closed_error|read_only_error]exceptionPack_errorofbase_errorexceptionClosed=Irmin.ClosedexceptionRO_not_allowed=Irmin_pack.RO_not_allowed(** Error manager *)moduletypeS=sigtypet=errorvalpp:Format.formatter->[<t]->unitvalraise_error:[<t]->'avallog_error:string->[<t]->unitvalcatch:(unit->'a)->('a,[>t])resultvalraise_if_error:('a,[<t])result->'avallog_if_error:string->(unit,[<t])result->unitvalto_json_string:(int63,[<t])result->stringvalof_json_string:string->(int63,[>t])resultendmoduleBase:Swithtypet=error=structtypet=errorletppppf=function|#read_only_errorase->pp_read_only_errorppfe|#closed_errorase->pp_closed_errorppfe|#base_errorase->pp_base_errorppfeletraise_error=function|#read_only_error->raiseRO_not_allowed|#closed_error->raiseClosed|#base_errorase->raise(Pack_errore)letlog_errorcontexte=[%log.err"%s failed: %a"contextppe]letcatchf=tryOk(f())with|Pack_errore->Error(e:base_error:>[>t])|RO_not_allowed->Error`Ro_not_allowed|Closed->Error`Closedletraise_if_error=functionOkx->x|Errore->raise_erroreletlog_if_errorcontext=function|Ok_->()|Errore->log_errorcontextetypeerr=Pack_errorofbase_error|Ro_not_allowed|Closed[@@derivingirmin]lett_to_err=function|#read_only_error->Ro_not_allowed|#closed_error->Closed|#base_errorase->Pack_erroreleterr_to_t=function|Closed->`Closed|Ro_not_allowed->`Ro_not_allowed|Pack_errore->(e:base_error:>[>t])leterr_result=Irmin.Type.(resultint63err_t)letto_json_stringresult=letconvert=Result.map_errort_to_errinconvertresult|>Irmin.Type.to_json_stringerr_resultletof_json_stringstring=match(Irmin.Type.of_json_stringerr_result)stringwith|Error(`Msg_)->Error`Decoding_error|Okresult->Result.map_errorerr_to_tresultendlet()=Printexc.register_printer(function|Pack_errore->Some(Fmt.str"Pack_error: %a"pp_base_errore)|RO_not_allowed->Some"RO_not_allowed"|Closed->Some"Closed"|_->None)