(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*) type Loc.Error.kind += | Magic_mismatch of string | Truncated of string | Corrupt of string | Invalid of string let pp_hex ppf s = String.iteri (fun i c -> if i > 0 then Fmt.string ppf " "; Fmt.pf ppf "%02x" (Char.code c)) s let () = Loc.Error.register_kind_printer (function | Magic_mismatch fnd -> Some (fun ppf -> Fmt.pf ppf "not a BSDIFF40 patch: expected magic %S, found %a" "BSDIFF40" pp_hex fnd) | Truncated what -> Some (fun ppf -> Fmt.pf ppf "truncated patch while reading %s" what) | Corrupt what -> Some (fun ppf -> Fmt.pf ppf "corrupt patch: %s" what) | Invalid what -> Some (fun ppf -> Fmt.pf ppf "invalid patch: %s" what) | _ -> None) let ctx_with_triple n = Loc.Context.push ~sort:("control", Loc.Meta.none) (Loc.Path.Nth (n, Loc.Meta.none)) Loc.Context.empty let fail ?(ctx = Loc.Context.empty) kind = raise (Loc.Error (Loc.Error.v ~ctx ~meta:Loc.Meta.none kind)) let fail_magic_mismatch ?ctx fnd = fail ?ctx (Magic_mismatch fnd) let fail_truncated ?ctx what = fail ?ctx (Truncated what) let fail_corrupt ?ctx what = fail ?ctx (Corrupt what) let fail_invalid ?ctx what = fail ?ctx (Invalid what)