(*----------------------------------------------------------------------------
    Copyright (c) 2017 Inhabited Type LLC.

    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.

    3. Neither the name of the author nor the names of his contributors
       may be used to endorse or promote products derived from this software
       without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
    OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
    ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
  ----------------------------------------------------------------------------*)


(** Response Status Codes

   The status-code element is a three-digit integer code giving the result of
   the attempt to understand and satisfy the request.

   See {{:https://tools.ietf.org/html/rfc7231#section-6} RFC7231§6} for more
   details. *)

type informational = [
  | `Continue
  | `Switching_protocols
  ]
(** The 1xx (Informational) class of status code indicates an interim
    response for communicating connection status or request progress
    prior to completing the requested action and sending a final
    response.

    See {{:https://tools.ietf.org/html/rfc7231#section-6.2} RFC7231§6.2}
    for more details. *)

type successful = [
  | `OK
  | `Created
  | `Accepted
  | `Non_authoritative_information
  | `No_content
  | `Reset_content
  | `Partial_content
  ]
(** The 2xx (Successful) class of status code indicates that the client's
    request was successfully received, understood, and accepted.

    See {{:https://tools.ietf.org/html/rfc7231#section-6.3} RFC7231§6.3}
    for more details. *)

type redirection = [
  | `Multiple_choices
  | `Moved_permanently
  | `Found
  | `See_other
  | `Not_modified
  | `Use_proxy
  | `Temporary_redirect
  ]
(** The 3xx (Redirection) class of status code indicates that further
    action needs to be taken by the user agent in order to fulfill the
    request.

    See {{:https://tools.ietf.org/html/rfc7231#section-6.4} RFC7231§6.4} for
    more details. *)

type client_error = [
  | `Bad_request
  | `Unauthorized
  | `Payment_required
  | `Forbidden
  | `Not_found
  | `Method_not_allowed
  | `Not_acceptable
  | `Proxy_authentication_required
  | `Request_timeout
  | `Conflict
  | `Gone
  | `Length_required
  | `Precondition_failed
  | `Payload_too_large
  | `Uri_too_long
  | `Unsupported_media_type
  | `Range_not_satisfiable
  | `Expectation_failed
  | `Upgrade_required
  | `I_m_a_teapot
  | `Enhance_your_calm
  | `Precondition_required
  | `Too_many_requests
  | `Request_header_fields_too_large
  ]
(** The 4xx (Client Error) class of status code indicates that the client
    seems to have erred.

    See {{:https://tools.ietf.org/html/rfc7231#section-6.5} RFC7231§6.5} for
    more details. *)

type server_error = [
  | `Internal_server_error
  | `Not_implemented
  | `Bad_gateway
  | `Service_unavailable
  | `Gateway_timeout
  | `Http_version_not_supported
  | `Network_authentication_required
  ]
(** The 5xx (Server Error) class of status code indicates that the server is
    aware that it has erred or is incapable of performing the requested
    method.

    See {{:https://tools.ietf.org/html/rfc7231#section-6.6} RFC7231§6.6} for
    more details. *)

type standard = [
  | informational
  | successful
  | redirection
  | client_error
  | server_error
  ]
(** The status codes defined in the HTTP 1.1 RFCs *)

type t = [
  | standard
  | `Code of int ]
(** The standard codes along with support for custom codes. *)

val default_reason_phrase : standard -> string
(** [default_reason_phrase standard] is the example reason phrase provided
    by RFC7231 for the [standard] status code. The RFC allows servers to use
    reason phrases besides these in responses. *)

val to_code : t -> int
(** [to_code t] is the integer representation of [t]. *)

val of_code : int -> t
(** [of_code i] is the [t] representation of [i]. [of_code] raises [Failure]
    if [i] is not a positive three-digit number. *)

val unsafe_of_code : int -> t
(** [unsafe_of_code i] is equivalent to [of_code i], except it accepts any
    positive code, regardless of the number of digits it has. On negative
    codes, it will still raise [Failure]. *)

val is_informational : t -> bool
(** [is_informational t] is true iff [t] belongs to the Informational class
    of status codes. *)

val is_successful : t -> bool
(** [is_successful t] is true iff [t] belongs to the Successful class of
    status codes. *)

val is_redirection : t -> bool
(** [is_redirection t] is true iff [t] belongs to the Redirection class of
    status codes. *)

val is_client_error : t -> bool
(** [is_client_error t] is true iff [t] belongs to the Client Error class of
    status codes. *)

val is_server_error : t -> bool
(** [is_server_error t] is true iff [t] belongs to the Server Error class of
    status codes. *)

val is_error : t -> bool
(** [is_error t] is true iff [t] belongs to the Client Error or Server Error
    class of status codes. *)

val to_string : t -> string
val of_string : string -> t

val pp_hum : Format.formatter -> t -> unit [@@ocaml.toplevel_printer]


