let ( / ) = Eio.Path.( / ) let dev_null = Unix.openfile "/dev/null" Unix.[ O_WRONLY ] 0 let map_fst f (v1, v2) = (f v1, v2) let map_snd f (v1, v2) = (v1, f v2) module Log = struct let name s = let ctx = Effect.perform Eio.Private.Effects.Get_context in Eio.Private.Trace.name (Eio.Private.Fiber_context.tid ctx) s end module Fmt = struct include Fmt let lst pp ppf = (brackets @@ list ~sep:comma pp) ppf let ifs ppf v = let pp_char ppf = function | '\n' -> Fmt.string ppf "" | ' ' -> Fmt.string ppf "" | '\t' -> Fmt.string ppf "" | c -> Fmt.char ppf c in lst pp_char ppf (String.fold_left (fun acc c -> acc @ [ c ]) [] v) let string ?(max = 80) ppf v = Fmt.truncated ~max ppf v let text c = Logger.pp_colored c Fmt.string let commit fmt (hash, msg) = Fmt.pf fmt "[%a]: %s" (text `Yellow) hash msg let cid fmt cid = Fmt.pf fmt "%a" (text `Yellow) (String.sub (Cid.to_string cid) 0 12 ^ "...") end module List = struct include List let rec assoc_opt ~compare x = function | [] -> None | (a, b) :: l -> if compare a x = 0 then Some b else assoc_opt ~compare x l let rec remove_assoc ~compare x = function | [] -> [] | ((a, _) as pair) :: l -> if compare a x = 0 then l else pair :: remove_assoc ~compare x l let rec mem ~compare x = function | [] -> false | a :: l -> compare a x = 0 || mem ~compare x l let assoc_replace_or_add (k, v) lst = let rec replace (added, lst) = function | [] -> (added, List.rev lst) | (k', v') :: vs -> if k = k' then (true, List.rev ((k, v) :: lst) @ vs) else replace (added, (k', v') :: lst) vs in let added, new_list = replace (false, []) lst in if added then new_list else (k, v) :: lst end module Array = struct include Array let get ~label arr i = match Array.get arr i with | v -> v | exception Invalid_argument msg -> raise (Invalid_argument (label ^ ": " ^ msg)) end module Promise = struct include Eio.Promise let map ~sw f v = Eio.Fiber.fork_promise ~sw (fun () -> Eio.Promise.await_exn v |> f) end module Nlist = struct type 'a t = Singleton of 'a | Cons of 'a * 'a t [@@deriving yojson] let hd = function Singleton s -> s | Cons (s, _) -> s let rec length = function | Singleton _ -> 1 | Cons (_, rest) -> 1 + length rest let rec of_list = function | [] -> invalid_arg "Empty list" | [ x ] -> Singleton x | x :: xs -> Cons (x, of_list xs) let rec to_list = function | Singleton x -> [ x ] | Cons (x, xs) -> x :: to_list xs let rev vs = to_list vs |> List.rev |> of_list let cons v vs = Cons (v, vs) let rec map f = function | Singleton x -> Singleton (f x) | Cons (x, xs) -> Cons (f x, map f xs) let append v1 v2 = let rec loop = function | Singleton x -> Cons (x, v2) | Cons (x, xs) -> Cons (x, loop xs) in loop v1 let fold_left f acc v = to_list v |> List.fold_left f acc let ( @ ) = append let pp elt ppf v = to_list v |> Fmt.(list elt) ppf let flatten v = to_list v |> List.flatten |> of_list end (* Nonempty, separated lists *) module Nslist = struct type ('v, 'sep) t = ('v * 'sep) Nlist.t [@@deriving yojson] let singleton sep i = Nlist.Singleton (i, sep) let cons sep i v = Nlist.cons (i, sep) v let append a b = Nlist.append a b let map f g (v : ('a, 'b) t) : ('c, 'd) t = Nlist.map (fun (a, b) -> (f a, g b)) v let fold_left f acc (vs : ('a, 'b) t) = Nlist.fold_left (fun acc (a, b) -> f acc a b) acc vs end let yojson_pp = Yojson.Safe.pretty_print ~std:true module String = struct include String let get ~label s i = match String.get s i with | v -> v | exception Invalid_argument msg -> raise (Invalid_argument (label ^ " for " ^ s ^ ": " ^ msg)) let cut_prefix ~prefix s = match Astring.String.cut ~sep:prefix s with | Some ("", rest) -> Some rest | _ -> None let cut_suffix ~suffix s = let exception Found of int in let s_len = String.length s in try for i = s_len - 1 downto 0 do let v = String.sub s i (s_len - i) in if String.equal suffix v then raise (Found i) done; None with Found i -> Some (String.sub s 0 i) let jsont = Jsont.string end module Glob = struct let glob_dir fs pattern = match Glob.of_string pattern with | Error m -> Fmt.failwith "Please contact the maintainers: %s" m | Ok glob -> let v = Glob_eio.glob glob fs in (* Eio.traceln "%a" Fmt.(lst string) v; *) v let test ~pattern s = let pat = Re.Glob.glob ~anchored:true ~pathname:false pattern |> Re.compile in Re.execp pat s end module Map = struct module Make (A : sig include Map.OrderedType val jsont : t Jsont.t end) = struct include Map.Make (A) let encode' j v = Jsont.Json.encode j v |> Result.get_ok' let decode' j v = Jsont.Json.decode j v |> Result.get_ok' let jsont vjsont = let enc m = let l = to_list m in let assoc = List.map (fun (k, v) -> Jsont.Array ([ encode' A.jsont k; encode' vjsont v ], Jsont.Meta.none)) l in Jsont.Array (assoc, Jsont.Meta.none) in let dec m = let assoc = match m with | Jsont.Array (assoc, _) -> List.map (function Jsont.Array (lst, _) -> lst | _ -> assert false) assoc | _ -> assert false in List.map (function | [ key; value ] -> (decode' A.jsont key, decode' vjsont value) | _ -> assert false) assoc |> of_list in Jsont.map Jsont.json ~enc ~dec end end module Set = struct module Make (A : sig include Set.OrderedType val jsont : t Jsont.t end) = struct include Set.Make (A) let jsont = Jsont.map (Jsont.list A.jsont) ~enc:to_list ~dec:of_list end end module Fpath = struct include Fpath let jsont = Jsont.map Jsont.string ~enc:Fpath.to_string ~dec:(fun t -> Fpath.of_string t |> function | Ok v -> v | Error (`Msg s) -> Fmt.invalid_arg "Jsont decoding fpath: %s" s) end module Int = struct include Int let jsont = Jsont.int end let jsont_of_yojson = let dec json = let rec loop = function | Jsont.Null ((), _) -> `Null | Jsont.String (s, _) -> `String s | Jsont.Number (f, _) -> `Int (Float.to_int f) | Jsont.Bool (b, _) -> `Bool b | Jsont.Array (lst, _) -> `List (List.map loop lst) | Jsont.Object (obj, _) -> `Assoc (List.map (fun ((s, _), v) -> (s, loop v)) obj) in loop json in let enc (json : Yojson.Safe.t) = let rec loop = function | `Null -> Jsont.Null ((), Jsont.Meta.none) | `String s -> Jsont.String (s, Jsont.Meta.none) | `Bool b -> Jsont.Bool (b, Jsont.Meta.none) | `List lst -> Jsont.Array (List.map loop lst, Jsont.Meta.none) | `Assoc lst -> Jsont.Object ( List.map (fun (k, v) -> ((k, Jsont.Meta.none), loop v)) lst, Jsont.Meta.none ) | `Int i -> Jsont.Number (Float.of_int i, Jsont.Meta.none) (* | `Tuple ts -> Jsont.Array (List.map loop ts, Jsont.Meta.none) *) | _ -> Jsont.Error.msg Jsont.Meta.none "Unexpected yojson value!" in loop json in Jsont.map Jsont.json ~enc ~dec let get_yojson : 'a Ppx_deriving_yojson_runtime.error_or -> 'a = function | Ok v -> v | Error s -> Fmt.failwith "Failed to deserialise yojson: %s" s let repr_of_jsont jsont = let dec x = match Jsont_bytesrw.decode_string' jsont x with | Ok json_str -> json_str | Error err -> Fmt.failwith "Repr jsont decoding error: %a" Jsont.Error.pp err in let enc s = match Jsont_bytesrw.encode_string' jsont s with | Ok x -> x | Error err -> Fmt.failwith "Repr jsont encoding error: %a" Jsont.Error.pp err in Repr.map Repr.string dec enc