Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Content_type.Type.of_string and Header.add_unless_exists #81

Merged
merged 1 commit into from
Aug 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/content_type.ml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,21 @@ module Type = struct
| `Multipart -> "multipart"
| `Ietf_token token | `X_token token -> token

let of_string str =
match String.lowercase_ascii str with
| "text" -> Ok `Text
| "image" -> Ok `Image
| "audio" -> Ok `Audio
| "video" -> Ok `Video
| "application" -> Ok `Application
| "message" -> Ok `Message
| "multipart" -> Ok `Multipart
| str -> (
match (ietf str, extension str) with
| Ok ietf, _ -> Ok ietf
| _, Ok extension -> Ok extension
| _ -> Rresult.R.error_msgf "Invalid type: %S" str)

let compare a b =
String.(
compare (lowercase_ascii (to_string a)) (lowercase_ascii (to_string b)))
Expand Down
1 change: 1 addition & 0 deletions lib/content_type.mli
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ module Type : sig
val is_multipart : t -> bool
val is_message : t -> bool
val to_string : t -> string
val of_string : string -> (t, [> Rresult.R.msg ]) result
end

module Subtype : sig
Expand Down
4 changes: 4 additions & 0 deletions lib/header.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ let add : type a. Field_name.t -> a Field.t * a -> t -> t =
let field = Field.Field (field_name, w, v) in
Location.inj ~location:Location.none field :: t

let add_unless_exists : type a. Field_name.t -> a Field.t * a -> t -> t =
fun field_name (w, v) t ->
if exists field_name t then t else add field_name (w, v) t

let replace : type a. Field_name.t -> a Field.t * a -> t -> t =
fun field_name (w, v) t ->
let rec replace acc = function
Expand Down
5 changes: 5 additions & 0 deletions lib/header.mli
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ val add : Field_name.t -> 'a Field.t * 'a -> t -> t
(** [add field_name (w, v) t] adds a new field-name with value v. [add]
does not replace [field_name] if it already exists into [t]. *)

val add_unless_exists : Field_name.t -> 'a Field.t * 'a -> t -> t
(** [add_unless_exists field_name (w, v) t] is a collection of header fields that
is the same as [t] if [t] already includes [field_name], and otherwise is
equivalent to [add field_name (w, v) t]. *)

val replace : Field_name.t -> 'a Field.t * 'a -> t -> t
(** [replace field_name (w, v) t] replaces existing field-name [field_name] in [t]
by the new value [v]. If [field_name] does not exist, it adds it. *)
Expand Down