Skip to content

Instantly share code, notes, and snippets.

@p1xelHer0
Last active February 10, 2020 22:36
Show Gist options
  • Save p1xelHer0/ed00d2c0e64804420fa306ab850b57b7 to your computer and use it in GitHub Desktop.
Save p1xelHer0/ed00d2c0e64804420fa306ab850b57b7 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
if List.length (snd sep_l) > 0 then Error (`Invalid_Symlink (snd sep_l))
else Ok (fst sep_l)
in
let read_rules = function
| List ss -> Ok (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