<commit_before>(* This is free and unencumbered software released into the public domain. *)

open Prelude

module Topic = struct
  type t = { path: string list; message_type: string; qos_policy: int }

  let separator_string = "/"
  let separator_char = (Char.of_string separator_string)

  let create path =
    if (List.exists (fun part -> String.contains part separator_char) path) then begin
      raise (Invalid_argument "topic path component must not contain the separator character")
    end;
    {path = path; message_type = ""; qos_policy = 0}

  let path topic = topic.path
  let message_type topic = topic.message_type
  let qos_policy topic = topic.qos_policy

  let of_string path = create [path] (* TODO: String.split path separator_char *)
  let to_string topic = String.concat separator_string topic.path
end

(* See: https://stomp.github.io/stomp-specification-1.2.html#Server_Frames *)
(* See: https://stomp.github.io/stomp-specification-1.2.html#Client_Frames *)
module Stomp_command = struct
  type t =
    (* Server commands *)
    | CONNECTED
    | MESSAGE
    | RECEIPT
    | ERROR
    (* Client commands *)
    | CONNECT
    | SEND
    | SUBSCRIBE
    | UNSUBSCRIBE
    | ACK
    | NACK
    | BEGIN
    | COMMIT
    | ABORT
    | DISCONNECT

  let of_string = function
    (* Server commands *)
    | "CONNECTED" -> CONNECTED
    | "MESSAGE" -> MESSAGE
    | "RECEIPT" -> RECEIPT
    | "ERROR" -> ERROR
    (* Client commands *)
    | "CONNECT" -> CONNECT
    | "SEND" -> SEND
    | "SUBSCRIBE" -> SUBSCRIBE
    | "UNSUBSCRIBE" -> UNSUBSCRIBE
    | "ACK" -> ACK
    | "NACK" -> NACK
    | "BEGIN" -> BEGIN
    | "COMMIT" -> COMMIT
    | "ABORT" -> ABORT
    | "DISCONNECT" -> DISCONNECT
    | input -> raise (Invalid_argument input)

  let to_string = function
    (* Server commands *)
    | CONNECTED -> "CONNECTED"
    | MESSAGE -> "MESSAGE"
    | RECEIPT -> "RECEIPT"
    | ERROR -> "ERROR"
    (* Client commands *)
    | CONNECT -> "CONNECT"
    | SEND -> "SEND"
    | SUBSCRIBE -> "SUBSCRIBE"
    | UNSUBSCRIBE -> "UNSUBSCRIBE"
    | ACK -> "ACK"
    | NACK -> "NACK"
    | BEGIN -> "BEGIN"
    | COMMIT -> "COMMIT"
    | ABORT -> "ABORT"
    | DISCONNECT -> "DISCONNECT"

  let length command =
    (String.length (to_string command))
end

(* See: https://stomp.github.io/stomp-specification-1.2.html#STOMP_Frames *)
module Stomp_header = struct
  type t = { key: string; value: string }

  let create key value =
    {key = key; value = value}

  let key header = header.key
  let value header = header.value

  let of_string input = assert false (* TODO *)

  let to_string header =
    Printf.sprintf "%s:%s" header.key header.value

  let length command =
    (String.length command.key) + 1 + (String.length command.value)
end

(* See: https://stomp.github.io/stomp-specification-1.2.html#STOMP_Frames *)
module Stomp_frame = struct
  type t = { command: Stomp_command.t; headers: Stomp_header.t list; body: string }

  let create command headers body =
    {command = command; headers = headers; body = body}

  let command frame = frame.command
  let headers frame = frame.headers
  let body frame = frame.body

  let size frame =
    (Stomp_command.length frame.command) + 1 +
    (List.fold_left (+) 0
      (List.map
        (fun header -> Stomp_header.length header + 1)
        frame.headers)) + 1 +
    (String.length frame.body) + 1

  let to_string frame =
    let buffer = Buffer.create (size frame) in
    Buffer.add_string buffer (Stomp_command.to_string frame.command);
    Buffer.add_char buffer '\n';
    List.iter
      (fun header ->
        Buffer.add_string buffer (Stomp_header.to_string header);
        Buffer.add_char buffer '\n')<commit_msg>Simplified record construction using argument punning.<commit_after>(* This is free and unencumbered software released into the public domain. *)

open Prelude

module Topic = struct
  type t = { path: string list; message_type: string; qos_policy: int }

  let separator_string = "/"
  let separator_char = (Char.of_string separator_string)

  let create path =
    if (List.exists (fun part -> String.contains part separator_char) path) then begin
      raise (Invalid_argument "topic path component must not contain the separator character")
    end;
    {path = path; message_type = ""; qos_policy = 0}

  let path topic = topic.path
  let message_type topic = topic.message_type
  let qos_policy topic = topic.qos_policy

  let of_string path = create [path] (* TODO: String.split path separator_char *)
  let to_string topic = String.concat separator_string topic.path
end

(* See: https://stomp.github.io/stomp-specification-1.2.html#Server_Frames *)
(* See: https://stomp.github.io/stomp-specification-1.2.html#Client_Frames *)
module Stomp_command = struct
  type t =
    (* Server commands *)
    | CONNECTED
    | MESSAGE
    | RECEIPT
    | ERROR
    (* Client commands *)
    | CONNECT
    | SEND
    | SUBSCRIBE
    | UNSUBSCRIBE
    | ACK
    | NACK
    | BEGIN
    | COMMIT
    | ABORT
    | DISCONNECT

  let of_string = function
    (* Server commands *)
    | "CONNECTED" -> CONNECTED
    | "MESSAGE" -> MESSAGE
    | "RECEIPT" -> RECEIPT
    | "ERROR" -> ERROR
    (* Client commands *)
    | "CONNECT" -> CONNECT
    | "SEND" -> SEND
    | "SUBSCRIBE" -> SUBSCRIBE
    | "UNSUBSCRIBE" -> UNSUBSCRIBE
    | "ACK" -> ACK
    | "NACK" -> NACK
    | "BEGIN" -> BEGIN
    | "COMMIT" -> COMMIT
    | "ABORT" -> ABORT
    | "DISCONNECT" -> DISCONNECT
    | input -> raise (Invalid_argument input)

  let to_string = function
    (* Server commands *)
    | CONNECTED -> "CONNECTED"
    | MESSAGE -> "MESSAGE"
    | RECEIPT -> "RECEIPT"
    | ERROR -> "ERROR"
    (* Client commands *)
    | CONNECT -> "CONNECT"
    | SEND -> "SEND"
    | SUBSCRIBE -> "SUBSCRIBE"
    | UNSUBSCRIBE -> "UNSUBSCRIBE"
    | ACK -> "ACK"
    | NACK -> "NACK"
    | BEGIN -> "BEGIN"
    | COMMIT -> "COMMIT"
    | ABORT -> "ABORT"
    | DISCONNECT -> "DISCONNECT"

  let length command =
    (String.length (to_string command))
end

(* See: https://stomp.github.io/stomp-specification-1.2.html#STOMP_Frames *)
module Stomp_header = struct
  type t = { key: string; value: string }

  let create key value = { key; value }

  let key header = header.key
  let value header = header.value

  let of_string input = assert false (* TODO *)

  let to_string header =
    Printf.sprintf "%s:%s" header.key header.value

  let length command =
    (String.length command.key) + 1 + (String.length command.value)
end

(* See: https://stomp.github.io/stomp-specification-1.2.html#STOMP_Frames *)
module Stomp_frame = struct
  type t = { command: Stomp_command.t; headers: Stomp_header.t list; body: string }

  let create command headers body = { command; headers; body }

  let command frame = frame.command
  let headers frame = frame.headers
  let body frame = frame.body

  let size frame =
    (Stomp_command.length frame.command) + 1 +
    (List.fold_left (+) 0
      (List.map
        (fun header -> Stomp_header.length header + 1)
        frame.headers)) + 1 +
    (String.length frame.body) + 1

  let to_string frame =
    let buffer = Buffer.create (size frame) in
    Buffer.add_string buffer (Stomp_command.to_string frame.command);
    Buffer.add_char buffer '\n';
    List.iter
      (fun header ->
        Buffer.add_string buffer (Stomp_header.to_string header);
        Buffer.add_char buffer '\n')
{'fraction_non_alphanumeric': 0.12101566720691519, 'fraction_numerical': 0.004051863857374392, 'mean_word_length': 2.795489492567914, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 67, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4908322', 'n_tokens_mistral': 2529, 'n_tokens_neox': 2460, 'n_words': 657}