Created
May 2, 2014 19:30
-
-
Save AeroNotix/11484040 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instead of exception, I'd use
Result.t
Take a look at
Str
andRe
modules, though I'd prefer to useocamllex