<commit_before>
(** Environment for configure variable
    @author <NAME>
  *)

module Msg    = BaseMessage;;
module MapVar = Map.Make(String);;
module SetVar = Set.Make(String);;

(** Variable type
  *)
type definition =
    {
      default: env -> string * env;
      hidden:  bool;
    }

(** Environment type
  *)
and env = 
    {
      defined:      definition MapVar.t;
      value:        string MapVar.t;
      print_hidden: bool;
    }
;;

(** Add a variable to environment. [hide] allow to store
  * a variable that will be hidden to user (not printed).
  *)
let var_define ?(hide=false) name dflt env =
  if not (MapVar.mem name env.defined) then
    {
      env with 
          defined = MapVar.add 
                      name 
                      {default = dflt; hidden = hide} 
                      env.defined;
    }
  else
    failwith 
      (Printf.sprintf
         "Variable %S is already defined"
         name)
;;

(** Set value of a variable 
  *)
let var_set name value env =
  if MapVar.mem name env.defined then
    {
      env with
          value = MapVar.add name value env.value;     
    }
  else
    failwith 
      (Printf.sprintf 
         "Cannot set variable %S because it is not defined" 
         name)
;;

(** Retrieve the value of a variable
  *)
let var_get ?(mandatory=false) name env =
  try 
    MapVar.find name env.value, env
  with Not_found ->
    (
      try
        let def = 
          MapVar.find name env.defined
        in
        let advertise = 
          not def.hidden 
        in
        let dflt, env = 
          if advertise then Msg.checking name;
          def.default env
        in
          if advertise then Msg.result dflt;
          dflt, var_set name dflt env
      with Not_found -><commit_msg>Display checks/result in the right order<commit_after>
(** Environment for configure variable
    @author <NAME>
  *)

module Msg    = BaseMessage;;
module MapVar = Map.Make(String);;
module SetVar = Set.Make(String);;

(** Variable type
  *)
type definition =
    {
      default: env -> string * env;
      hidden:  bool;
    }

(** Environment type
  *)
and env = 
    {
      defined:      definition MapVar.t;
      value:        string MapVar.t;
      print_hidden: bool;
    }
;;

(** Add a variable to environment. [hide] allow to store
  * a variable that will be hidden to user (not printed).
  *)
let var_define ?(hide=false) name dflt env =
  if not (MapVar.mem name env.defined) then
    {
      env with 
          defined = MapVar.add 
                      name 
                      {default = dflt; hidden = hide} 
                      env.defined;
    }
  else
    failwith 
      (Printf.sprintf
         "Variable %S is already defined"
         name)
;;

(** Set value of a variable 
  *)
let var_set name value env =
  if MapVar.mem name env.defined then
    {
      env with
          value = MapVar.add name value env.value;     
    }
  else
    failwith 
      (Printf.sprintf 
         "Cannot set variable %S because it is not defined" 
         name)
;;

(** Retrieve the value of a variable
  *)
let var_get ?(mandatory=false) name env =
  try 
    MapVar.find name env.value, env
  with Not_found ->
    (
      try
        let def = 
          MapVar.find name env.defined
        in
        let dflt, env = 
          def.default env
        in
          if not def.hidden then 
            (
              Msg.checking name;
              Msg.result dflt
            );
          dflt, var_set name dflt env
      with Not_found ->
{'fraction_non_alphanumeric': 0.08651399491094147, 'fraction_numerical': 0.0, 'mean_word_length': 1.6402985074626866, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5257827', 'n_tokens_mistral': 1091, 'n_tokens_neox': 1042, 'n_words': 352}