Hcs.Codec

hcs · API reference

Request/response body codecs.

Use this module when a handler or client wants explicit encode/decode behavior for strings, JSON-like payloads, or application-specific body formats.

Type-directed request and response body codecs.

HCS itself does not choose a JSON, CBOR, MessagePack, or domain codec. This module defines a small codec signature and a functor that turns any codec implementation into helpers for request/response bodies.

Example:

module Json_codec : Hcs.Codec.CODEC = struct
  type 'a encoder = 'a -> string
  type 'a decoder = string -> ('a, string) result

  let content_type = "application/json"
  let encode enc value = Ok (Cstruct.of_string (enc value))
  let decode dec buf = dec (Cstruct.to_string buf)
  let encode_stream _ _ = None
  let decode_stream _ _ = None
end

module Json = Hcs.Codec.With_codec (Json_codec)
module type CODEC = sig ... end
type codec_error = 
  | Encode_error of string
  | Decode_error of string
  | Unsupported_body_type (* Normalized codec error. *)
val codec_error_to_string : codec_error -> string

Human-readable error message.

module With_codec (C : CODEC) : sig ... end
module Identity_codec : 
  CODEC
    with type 'a encoder = 'a -> Cstruct.t
     and type 'a decoder = Cstruct.t -> ('a, string) result

Pass-through codec for raw binary data.

module String_codec : 
  CODEC
    with type 'a encoder = 'a -> string
     and type 'a decoder = string -> ('a, string) result

UTF-8/text codec.