Something went wrong. Try again.
Git format in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233open Core
let name_char = function | 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | '-' -> true | _ -> false
let alpha = function 'a' .. 'z' | 'A' .. 'Z' -> true | _ -> false
let check_name ~variable s = if s = "" || (not (String.for_all name_char s)) || (variable && not (alpha s.[0])) then Error.encode ("invalid configuration name: " ^ s)
let escape_value s = if String.contains s '\000' then Error.encode "NUL in configuration value"; let whitespace = function | ' ' | '\t' | '\n' | '\r' | '\011' | '\012' -> true | _ -> false in let quoted = String.length s > 0 && (whitespace s.[0] || whitespace s.[String.length s - 1] || String.contains s '#' || String.contains s ';') in let b = Buffer.create (String.length s + 2) in if quoted then Buffer.add_char b '"'; String.iter (function | '\\' -> Buffer.add_string b "\\\\" | '"' -> Buffer.add_string b "\\\"" | '\n' -> Buffer.add_string b "\\n" | '\t' -> Buffer.add_string b "\\t" | c -> Buffer.add_char b c) s; if quoted then Buffer.add_char b '"'; Buffer.contents b
let header name subsection = check_name ~variable:false name; match subsection with | None -> "[" ^ String.lowercase_ascii name ^ "]" | Some sub -> if String.contains sub '\n' || String.contains sub '\000' then Error.encode "newline or NUL in subsection"; let b = Buffer.create (String.length sub + 2) in String.iter (fun c -> if c = '\\' || c = '"' then Buffer.add_char b '\\'; Buffer.add_char b c) sub; "[" ^ String.lowercase_ascii name ^ " \"" ^ Buffer.contents b ^ "\"]"
let rec scalar : type a. a codec -> a -> value = fun c v -> match c with | String_codec -> String (v, Loc.Meta.none) | Bool -> String ((if v then "true" else "false"), Loc.Meta.none) | Int -> String (string_of_int v, Loc.Meta.none) | Map (_, enc, c) -> scalar c (enc v) | Value -> v | Section_codec _ | Sections _ | File_codec _ -> Error.encode "a section variable needs a scalar codec"
let raw ~preserve m = if not preserve then None else match Loc.Meta.text m with | None -> None | Some text -> let loc = Loc.Meta.loc m in let first = Loc.first_byte loc and last = Loc.last_byte loc in if Loc.compare_byte_pos first 0 < 0 || Loc.compare_byte_pos last (String.length text) >= 0 || Loc.compare_byte_pos last first < 0 then None else Some (String.sub text first (last - first + 1))
let write_ast ~indent ~preserve buffer value = let padding = match indent with | None -> "\t" | Some n when n >= 0 -> String.make n ' ' | Some _ -> Error.encode "indent must be nonnegative" in let add s = Buffer.add_string buffer s in let trivia m = if preserve then add (Loc.Meta.ws_before m) in let variable (m : member) = trivia m.meta; match raw ~preserve m.meta with | Some bytes -> add bytes | None -> check_name ~variable:true (fst m.name); add padding; add (String.lowercase_ascii (fst m.name)); (match m.value with | Unset _ -> () | String (s, _) -> add " = "; add (escape_value s) | _ -> Error.encode "a variable must be a string or valueless"); add "\n" in let section = function | Section (s, m) -> trivia m; (match raw ~preserve m with | Some bytes -> add bytes | None when fst s.name = "" && Option.is_none s.subsection -> () | None -> add (header (fst s.name) (Option.map fst s.subsection)); add "\n"); List.iter variable s.members | _ -> Error.encode "a configuration file contains sections only" in match value with | File (sections, m) -> ( match raw ~preserve m with | Some bytes -> add bytes | None -> List.iter section sections; if preserve then add (Loc.Meta.ws_after m)) | _ -> Error.encode "a configuration file needs a file node"
let rec members : type o f. (o, f) fields -> o -> member list = fun fields v -> match fields with | Make _ -> [] | Member (before, key, c, enc, _, _) -> let before = members before v in let member = { name = (key, Loc.Meta.none); value = scalar c (enc v); meta = Loc.Meta.none; } in before @ [ member ]
let section_value name sub fields v = Section ( { name = (name, Loc.Meta.none); subsection = Option.map (fun s -> (s, Loc.Meta.none)) sub; members = members fields v; }, Loc.Meta.none )
let rec to_value : type a. a codec -> a -> value = fun c v -> match c with | Value -> v | Map (_, enc, c) -> to_value c (enc v) | String_codec | Bool | Int -> scalar c v | Section_codec (name, sub, fields) -> File ([ section_value name sub fields v ], Loc.Meta.none) | Sections (name, fields) -> File ( List.map (fun (sub, v) -> section_value name sub fields v) v, Loc.Meta.none ) | File_codec fields -> File (file_fields fields v, Loc.Meta.none)
and file_fields : type o f. (o, f) fields -> o -> value list = fun fields v -> match fields with | Make _ -> [] | Member (before, _, c, enc, _, _) -> ( let before = file_fields before v in match to_value c (enc v) with | File (sections, _) -> before @ sections | _ -> Error.encode "file members must describe sections")
let rec write_variables : type o f. add:(string -> unit) -> padding:string -> (o, f) fields -> o -> unit = fun ~add ~padding fields v -> match fields with | Make _ -> () | Member (before, key, c, enc, _, _) -> write_variables ~add ~padding before v; check_name ~variable:true key; let s = match scalar c (enc v) with | String (s, _) -> s | Unset _ -> "" | _ -> Error.encode "variable encoder produced a non-scalar" in add padding; add key; add " = "; add (escape_value s); add "\n"
let rec write : type a. indent:int option -> preserve:bool -> Buffer.t -> a codec -> a -> unit = fun ~indent ~preserve buffer c v -> let add s = Buffer.add_string buffer s in let section name sub fields v = let padding = match indent with None -> "\t" | Some n -> String.make n ' ' in add (header name sub); add "\n"; write_variables ~add ~padding fields v in match c with | Value -> write_ast ~indent ~preserve buffer v | Map (_, enc, c) -> write ~indent ~preserve buffer c (enc v) | Section_codec (name, sub, fields) -> section name sub fields v | Sections (name, fields) -> List.iter (fun (sub, v) -> section name sub fields v) v | File_codec fields -> write_file ~indent ~preserve buffer fields v | String_codec | Bool | Int -> Error.encode "a file encoder needs sections"
and write_file : type o f. indent:int option -> preserve:bool -> Buffer.t -> (o, f) fields -> o -> unit = fun ~indent ~preserve buffer fields v -> match fields with | Make _ -> () | Member (before, _, c, enc, _, _) -> write_file ~indent ~preserve buffer before v; write ~indent ~preserve buffer c (enc v)
let to_string ?indent ?(preserve = false) c v = if Option.fold ~none:false ~some:(fun n -> n < 0) indent then Error.encode "indent must be nonnegative"; let buffer = Buffer.create 256 in write ~indent ~preserve buffer c v; Buffer.contents buffer