Last active
February 10, 2020 22:36
-
-
Save p1xelHer0/ed00d2c0e64804420fa306ab850b57b7 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 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