open StdLabels module Syntax = Syntax module Table = ImportDataTypes.Table module Path = ImportDataTypes.Path module T = Read_conf module Expression = ImportExpression.T let current_syntax = 1 let t_of_yojson : Yojson.Safe.t -> Syntax.t = fun json -> let keys = Yojson.Safe.Util.keys json in let version = match List.find_opt keys ~f:(String.equal "version") with | None -> Printf.printf "No version given. Your setup may break in the future.\n\ Please add « \"version\":%d » in your configuration.\n\n" current_syntax; `Int 1 | Some _ -> Yojson.Safe.Util.member "version" json in match version with | `Int 1 -> Of_json.t_of_yojson json | other -> Printf.eprintf "Unsuported version : %s\n" (Yojson.Safe.to_string other); exit 1 module TomlReader = Read_conf.Make (Helpers.Toml.Decode) let t_of_toml : Otoml.t -> (Syntax.t, string) result = fun toml -> let version = Otoml.find toml (Otoml.get_integer ~strict:false) [ "version" ] in match version with | 1 -> TomlReader.read toml | _ -> Printf.eprintf "Unsuported version : %d\n" version; exit 1 let dummy_conf = Syntax. { source = { file = ""; tab = 0; name = "" }; version = 1; externals = []; columns = []; filters = []; sort = []; uniq = []; } let get_table_for_name : Syntax.t -> string option -> Table.t = fun conf name -> match name with | None -> conf.source | Some name -> if String.equal name conf.source.name then conf.source else let ext = List.find conf.externals ~f:(fun (ext : Syntax.extern) -> String.equal name ext.target.name) in ext.target let root_table : Syntax.t -> Table.t = fun conf -> conf.source let get_dependancies_for_table : Syntax.t -> Table.t -> Syntax.extern list = fun conf source -> let is_root = source = conf.source in List.filter conf.externals ~f:(fun (ext : Syntax.extern) -> (* Enumerate the intern_key and check the source pointed by each column *) Expression.fold_values ext.intern_key ~init:false ~f:(fun acc expr -> if acc then acc else match expr.Syntax.Path.alias with | Some v -> String.equal v source.name | None -> is_root)) let print_path_expression t = ImportExpression.Repr.repr Path.repr t let print_extern t = let toml = Syntax.toml_of_extern t in Otoml.Printer.to_string toml let expression_from_string s = Read_conf.ExpressionParser.of_string Read_conf.ExpressionParser.path s