Skip to content

Commit fc5ac0c

Browse files
committed
http: Use or_error for method_of_string
1 parent 7d568e7 commit fc5ac0c

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

http/src/meth.ml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ include T
2121
include Comparable.Make (T)
2222

2323
let of_string = function
24-
| "GET" -> Some `GET
25-
| "HEAD" -> Some `HEAD
26-
| "POST" -> Some `POST
27-
| "PUT" -> Some `PUT
28-
| "DELETE" -> Some `DELETE
29-
| "CONNECT" -> Some `CONNECT
30-
| "OPTIONS" -> Some `OPTIONS
31-
| "TRACE" -> Some `TRACE
32-
| "PATCH" -> Some `PATCH
33-
| _ -> None
24+
| "GET" -> Ok `GET
25+
| "HEAD" -> Ok `HEAD
26+
| "POST" -> Ok `POST
27+
| "PUT" -> Ok `PUT
28+
| "DELETE" -> Ok `DELETE
29+
| "CONNECT" -> Ok `CONNECT
30+
| "OPTIONS" -> Ok `OPTIONS
31+
| "TRACE" -> Ok `TRACE
32+
| "PATCH" -> Ok `PATCH
33+
| meth -> Or_error.error "Invalid HTTP method" meth sexp_of_string
3434
;;
3535

3636
let to_string = function

http/src/meth.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type t =
1717
]
1818
[@@deriving sexp, compare, hash, enumerate, quickcheck]
1919

20-
val of_string : string -> t option
20+
val of_string : string -> t Or_error.t
2121
val to_string : t -> string
2222

2323
(** [is_safe t] returns true if the semantics for a HTTP method are essentially read-only,

http/test/test_method.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ let%test_unit "Http Methods can be coverted to strings and back" =
66
let b =
77
a
88
|> List.map ~f:Meth.to_string
9-
|> List.map ~f:(fun v -> Option.value_exn (Meth.of_string v))
9+
|> List.map ~f:(fun v -> Or_error.ok_exn (Meth.of_string v))
1010
in
1111
[%test_result: Meth.t list] ~expect:a b
1212
;;
1313

1414
let%test_unit "Meth.of_string (Meth.to_string m) is never none" =
1515
Quickcheck.test ~sexp_of:[%sexp_of: Meth.t] Meth.quickcheck_generator ~f:(fun meth ->
16-
[%test_result: Meth.t option]
17-
~expect:(Some meth)
16+
[%test_result: Meth.t Or_error.t]
17+
~expect:(Ok meth)
1818
(Meth.of_string (Meth.to_string meth)))
1919
;;

0 commit comments

Comments
 (0)