package stdune
Source file result.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204type ('a, 'error) t = ('a, 'error) result = | Ok of 'a | Error of 'error let ok x = Ok x let return = ok let value r ~default = match r with | Ok v -> v | Error _ -> default ;; let is_ok = function | Ok _ -> true | Error _ -> false ;; let is_error = function | Ok _ -> false | Error _ -> true ;; let ok_exn = function | Ok x -> x | Error e -> raise e ;; let try_with f = match f () with | s -> Ok s | exception e -> Error e ;; let bind t ~f = match t with | Ok x -> f x | Error _ as t -> t ;; let ( >>= ) x f = bind x ~f let map x ~f = match x with | Ok x -> Ok (f x) | Error _ as x -> x ;; let map_error x ~f = match x with | Ok _ as res -> res | Error x -> Error (f x) ;; let iter t ~f = match t with | Ok x -> f x | Error _ -> () ;; let to_option = function | Ok p -> Some p | Error _ -> None ;; let errorf fmt = Printf.ksprintf (fun x -> Error x) fmt let both a b = match a with | Error e -> Error e | Ok a -> (match b with | Error e -> Error e | Ok b -> Ok (a, b)) ;; module O = struct let ( >>= ) t f = bind t ~f let ( >>| ) t f = map t ~f let ( let* ) = ( >>= ) let ( let+ ) = ( >>| ) let ( and* ) = both let ( and+ ) = both end open O type ('a, 'error) result = ('a, 'error) t module List = struct let map t ~f = let rec loop acc = function | [] -> Ok (List.rev acc) | x :: xs -> (match f x with | Error _ as error -> error | Ok x -> loop (x :: acc) xs) in loop [] t ;; let all = let rec loop acc = function | [] -> Ok (List.rev acc) | (Error _ as error) :: _ -> error | Ok x :: l -> loop (x :: acc) l in fun l -> loop [] l ;; let concat_map = let rec loop f acc = function | [] -> Ok (List.rev acc) | x :: l -> (match f x with | Error _ as error -> error | Ok y -> loop f (List.rev_append y acc) l) in fun l ~f -> loop f [] l ;; let rec iter t ~f = match t with | [] -> Ok () | x :: xs -> (match f x with | Error _ as error -> error | Ok () -> iter xs ~f) ;; let rec fold_left t ~f ~init = match t with | [] -> Ok init | x :: xs -> (match f init x with | Error _ as error -> error | Ok init -> fold_left xs ~f ~init) ;; let filter_map t ~f = let rec loop acc = function | [] -> Ok (List.rev acc) | x :: xs -> (match f x with | Error _ as error -> error | Ok None -> loop acc xs | Ok (Some y) -> loop (y :: acc) xs) in loop [] t ;; end let hash h1 h2 = function | Ok s -> let acc = Hash.create () in let acc = Hash.feed acc 0 in let acc = Hash.feed acc (h1 s) in Hash.hash acc | Error e -> let acc = Hash.create () in let acc = Hash.feed acc 1 in let acc = Hash.feed acc (h2 e) in Hash.hash acc ;; let equal e1 e2 x y = match x, y with | Ok x, Ok y -> e1 x y | Error x, Error y -> e2 x y | _, _ -> false ;; let repr ok error = Repr.variant "result" [ Repr.case "Ok" ok ~proj:(function | Ok value -> Some value | Error _ -> None) ; Repr.case "Error" error ~proj:(function | Ok _ -> None | Error value -> Some value) ] ;; module Option = struct let iter t ~f = match t with | None -> Ok () | Some x -> x >>= f ;; end module Repr_derived = Repr.Make2 (struct type nonrec ('a, 'error) t = ('a, 'error) t let repr = repr end) let to_dyn = Repr_derived.to_dyn let to_either = function | Ok e -> Either.Right e | Error e -> Left e ;;