Skip to content

Instantly share code, notes, and snippets.

@leostera
Forked from p1xelHer0/config.ml
Last active February 10, 2020 22:39
Show Gist options
  • Save leostera/1146f9dbc996dc7ecbc7cdd4bc04924a to your computer and use it in GitHub Desktop.
Save leostera/1146f9dbc996dc7ecbc7cdd4bc04924a to your computer and use it in GitHub Desktop.
open Rresult
open Sexplib0.Sexp
let parse root_path =
let read_config path =
let root_path = path |> Fpath.to_dir_path |> Fpath.normalize in
let odot_file = Fpath.append root_path Model.config_file in
Bos.OS.File.read odot_file
in
let read_link l =
let sep_l =
let sep_l' (models, invalids) l' =
match l' with
| List [ Atom s; Atom t ] ->
( Model.{ source = Fpath.v s; target = Fpath.v t } :: models,
invalids )
| _ -> (models, l' :: invalids)
in
List.fold_left sep_l' ([], []) l
in
match sep_l with
| (oks, []) -> Ok oks
| (_, errs) -> Error (`Invalid_Symlink errs)
in
let read_rules = function
| List ss -> read_link ss
| Atom s -> Error (`Invalid_Config s)
in
let to_result path contents =
contents |> Parsexp.Single.parse_string >>| read_rules
|> R.error_to_msg
~pp_error:(Parsexp.Parse_error.report ~filename:(Fpath.to_string path))
in
let path = Fpath.v root_path in
path |> read_config >>= to_result path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment