Skip to content

Instantly share code, notes, and snippets.

@AeroNotix
Created May 2, 2014 19:30
Show Gist options
  • Save AeroNotix/11484040 to your computer and use it in GitHub Desktop.
Save AeroNotix/11484040 to your computer and use it in GitHub Desktop.
open Core.Std
module T = Core.Time
module Message = struct
exception Invalid_message_format of string
type msg = {
channel : string
; nick : string
; message : string
; date : float
}
type info = {
channel : string
; message : string
; date : float
}
type t =
| Msg of msg
| Info of info
let is_info_marker = function
| "<--" -> true
| "-->" -> true
| _ -> false
let of_date_string s =
(T.of_string s) |> T.to_float
let of_string ~raw ~channel =
let msg = String.split raw '\t' in
match msg with
| [] -> assert false
| [date; nick; message] when is_info_marker nick ->
let date = of_date_string date in
Info {channel;message;date}
| [date; nick; message] ->
let date = of_date_string date in
Msg {channel;nick;message;date}
| _ -> raise (Invalid_message_format raw)
end
@xandkar
Copy link

xandkar commented May 2, 2014

  1. I'd indent the records and drop-down the 1st brace:
type info =
  { channel : string
  ; message : string
  ; date    : float
  }
  1. Instead of exception, I'd use Result.t

  2. Take a look at Str and Re modules, though I'd prefer to use ocamllex

@AeroNotix
Copy link
Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment