Source

hack-and-slash / action.ml

Full commit
open Core.Std
open Direction.Export

module Export = struct
  type _action =
    | Move of Direction.t
    | Display
  with sexp, bin_io
end

type t = Export._action =
  | Move of Direction.t
  | Display
with sexp, bin_io

let to_line = function
  | Move d ->
    begin match d with
    | Up    -> "u"
    | Down  -> "d"
    | Left  -> "l"
    | Right -> "r"
    end
  | Display -> "display"

let of_line l =
  match String.strip l |> String.lowercase with
  | "u" -> Ok (Move Up)
  | "d" -> Ok (Move Down)
  | "l" -> Ok (Move Left)
  | "r" -> Ok (Move Right)
  | "display" -> Ok (Display)
  | s -> error "Unknown action" s
           <:sexp_of<string>>