Skip to content

Commit

Permalink
Really delete the rresult package
Browse files Browse the repository at this point in the history
  • Loading branch information
dinosaure committed Mar 4, 2023
1 parent d418caa commit 875ecc4
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .ocamlformat
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = 0.23.0
version = 0.24.1
break-infix = fit-or-vertical
parse-docstrings = true
indicate-multiline-delimiters=no
Expand Down
18 changes: 10 additions & 8 deletions bin/sign.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module Unix_scheduler = Dkim.Sigs.Make (struct
type +'a t = 'a
end)

let error_msgf fmt = Format.kasprintf (fun msg -> Error (`Msg msg)) fmt

module Caml_flow = struct
type backend = Unix_scheduler.t
type flow = { ic : in_channel; buf : Buffer.t }
Expand Down Expand Up @@ -129,7 +131,7 @@ let input =
match Fpath.of_string v with
| Ok path when Sys.file_exists v && not (Sys.is_directory v) ->
Ok (`Path path)
| Ok path -> Rresult.R.error_msgf "%a does not exist" Fpath.pp path
| Ok path -> error_msgf "%a does not exist" Fpath.pp path
| Error _ as err -> err in
let pp ppf = function
| `Input -> Fmt.string ppf "-"
Expand All @@ -138,7 +140,7 @@ let input =

let output =
let parser str =
let open Rresult in
let ( >>| ) x f = Result.map f x in
Fpath.of_string str >>| fun v -> `Path v in
let pp ppf = function
| `Output -> Fmt.string ppf "-"
Expand All @@ -150,15 +152,16 @@ let newline =
match String.lowercase_ascii str with
| "lf" -> Ok Dkim.LF
| "crlf" -> Ok Dkim.CRLF
| _ -> Rresult.R.error_msgf "Invalid newline specification: %S" str in
| _ -> error_msgf "Invalid newline specification: %S" str in
let pp ppf = function
| Dkim.LF -> Fmt.string ppf "lf"
| Dkim.CRLF -> Fmt.string ppf "crlf" in
Arg.conv (parser, pp)

let private_key =
let parser str =
let open Rresult in
let ( >>= ) = Result.bind in
let ( >>| ) x f = Result.map f x in
match
Base64.decode ~pad:true str
>>| Cstruct.of_string
Expand All @@ -170,7 +173,7 @@ let private_key =
| Ok path when Sys.file_exists str && not (Sys.is_directory str) ->
let contents = contents_of_path path in
X509.Private_key.decode_pem (Cstruct.of_string contents)
| Ok path -> R.error_msgf "%a does not exist" Fpath.pp path
| Ok path -> error_msgf "%a does not exist" Fpath.pp path
| Error _ as err -> err in
let pp ppf pk =
let contents = X509.Private_key.encode_pem pk in
Expand All @@ -187,7 +190,7 @@ let hash =
match Astring.String.trim (String.lowercase_ascii str) with
| "sha1" -> Ok `SHA1
| "sha256" -> Ok `SHA256
| _ -> Rresult.R.error_msgf "Invalid hash: %S" str in
| _ -> error_msgf "Invalid hash: %S" str in
let pp ppf = function
| `SHA1 -> Fmt.string ppf "sha1"
| `SHA256 -> Fmt.string ppf "sha256" in
Expand All @@ -202,8 +205,7 @@ let canon =
| Some ("simple", "relaxed"), _ -> Ok (`Simple, `Relaxed)
| Some ("relaxed", "simple"), _ -> Ok (`Relaxed, `Simple)
| Some ("relaxed", "relaxed"), _ | None, "relaxed" -> Ok (`Relaxed, `Relaxed)
| _ -> Rresult.R.error_msgf "Invalid canonicalization specification: %S" str
in
| _ -> error_msgf "Invalid canonicalization specification: %S" str in
let pp ppf = function
| `Simple, `Simple -> Fmt.string ppf "simple"
| `Relaxed, `Relaxed -> Fmt.string ppf "relaxed"
Expand Down
13 changes: 7 additions & 6 deletions bin/verify.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module Unix_scheduler = Dkim.Sigs.Make (struct
type +'a t = 'a
end)

let error_msgf fmt = Format.kasprintf (fun msg -> Error (`Msg msg)) fmt

module Caml_flow = struct
type backend = Unix_scheduler.t
type flow = { ic : in_channel; buf : Buffer.t }
Expand Down Expand Up @@ -78,7 +80,7 @@ let run quiet src newline nameserver =
| None -> None in
let dns = Dns_client_unix.create ?nameservers () in
let flow = Flow.of_input src in
let open Rresult in
let ( >>= ) = Result.bind in
Unix_scheduler.prj (Dkim.extract_dkim flow unix (module Flow))
>>= fun extracted ->
let r = Queue.create () in
Expand Down Expand Up @@ -131,7 +133,7 @@ let input =
match Fpath.of_string v with
| Ok path when Sys.file_exists v && not (Sys.is_directory v) ->
Ok (`Path path)
| Ok path -> Rresult.R.error_msgf "%a does not exist" Fpath.pp path
| Ok path -> error_msgf "%a does not exist" Fpath.pp path
| Error _ as err -> err in
let pp ppf = function
| `Input -> Fmt.string ppf "-"
Expand All @@ -143,7 +145,7 @@ let newline =
match String.lowercase_ascii str with
| "lf" -> Ok Dkim.LF
| "crlf" -> Ok Dkim.CRLF
| _ -> Rresult.R.error_msgf "Invalid newline specification: %S" str in
| _ -> error_msgf "Invalid newline specification: %S" str in
let pp ppf = function
| Dkim.LF -> Fmt.string ppf "lf"
| Dkim.CRLF -> Fmt.string ppf "crlf" in
Expand Down Expand Up @@ -189,9 +191,8 @@ let inet_addr =
match String.split_on_char ':' str with
| [ ns ] -> Ok (Unix.inet_addr_of_string ns, 53)
| [ ns; port ] -> Ok (Unix.inet_addr_of_string ns, int_of_string port)
| _ -> Rresult.R.error_msgf "Invalid nameserver IP: %S" str
with _exn ->
Rresult.R.error_msgf "Nameserver must be a valid IPv4: %S" str in
| _ -> error_msgf "Invalid nameserver IP: %S" str
with _exn -> error_msgf "Nameserver must be a valid IPv4: %S" str in
let pp ppf (inet_addr, port) =
Fmt.pf ppf "%s:%d" (Unix.string_of_inet_addr inet_addr) port in
Arg.conv (parser, pp)
Expand Down
2 changes: 1 addition & 1 deletion lib/dkim.mli
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Sigs = Sigs

type (+'a, 'err) or_err = ('a, ([> Rresult.R.msg ] as 'err)) result
type (+'a, 'err) or_err = ('a, ([> `Msg of string ] as 'err)) result
type newline = CRLF | LF
type map
type signed
Expand Down
7 changes: 4 additions & 3 deletions test/test.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
let () = Mirage_crypto_rng_unix.initialize (module Mirage_crypto_rng.Fortuna)
let ( <.> ) f g x = f (g x)
let error_msgf fmt = Format.kasprintf (fun msg -> Error (`Msg msg)) fmt

let reporter ppf =
let report src level ~over k msgf =
Expand Down Expand Up @@ -100,7 +101,7 @@ module Fake_resolver = struct
Unix_scheduler.inj (Ok [ str ])
| _ ->
Unix_scheduler.inj
(Rresult.R.error_msgf "domain %a does not exists"
(error_msgf "domain %a does not exists"
Fmt.(Dump.list string)
(Domain_name.to_strings domain_name))
end
Expand Down Expand Up @@ -159,7 +160,7 @@ let verify dns ic =
let server_keys, dkim_fields =
List.fold_left2
(fun a server_key ((_, _, _) as dkim_field) ->
let open Rresult.R in
let ( >>= ) = Result.bind in
match server_key >>= Dkim.post_process_server with
| Ok server_key -> (server_key, dkim_field) :: a
| Error (`Msg err) ->
Expand Down Expand Up @@ -249,7 +250,7 @@ let test_sign (trust, filename) =
close_out oc ;
let ic = open_in (filename ^ ".signed") in
let server = Dkim.server_of_dkim ~key:priv_of_seed dkim in
let domain_name = Rresult.R.get_ok (Dkim.domain_name dkim) in
let domain_name = Result.get_ok (Dkim.domain_name dkim) in
let rs = verify (Some (domain_name, server)) ic in
match rs with
| Ok rs ->
Expand Down

0 comments on commit 875ecc4

Please sign in to comment.