settings_toml.d 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. public{
  2. import std.stdio;
  3. import std.file : read;
  4. import toml;
  5. }
  6. TOMLDocument toml_s;
  7. // settings keys
  8. string s_toml_http = "http";
  9. string s_toml_http_host = "host";
  10. string s_toml_http_port = "port";
  11. /*
  12. string s_toml_db = "database";
  13. string s_toml_db_host = "host";
  14. string s_toml_db_port = "port";
  15. string s_toml_db_name = "dbname";
  16. string s_toml_db_user = "user";
  17. string s_toml_db_pass = "pass";
  18. string s_toml_db_conn_timeout = "connect_timeout";
  19. string s_toml_db_conn_num = "connections_number";
  20. */
  21. void read_settings_toml(){ // read settings, settings validation
  22. toml_s = parseTOML(cast(string)read("settings.toml"));
  23. if( are_valid_config_values(toml_s) ){}else{ return; } // settings validation
  24. }
  25. bool are_valid_config_values(ref TOMLDocument toml_s){ // settings validation
  26. string invalid_settings = "invalid settings: ";
  27. string grumpy = " :(";
  28. string invalid_group = " group ";
  29. string invalid_key = " key" ~ grumpy;
  30. bool invalid_toml_group(string group){
  31. writeln(invalid_settings ~ group ~ invalid_group ~ grumpy); return false;
  32. }
  33. bool invalid_toml_value(string group, string key){
  34. writeln(invalid_settings ~ group ~ invalid_group ~ key ~ invalid_key ~ grumpy); return false;
  35. }
  36. if((s_toml_http in toml_s) != null){
  37. auto toml_http = toml_s[s_toml_http];
  38. if((s_toml_http_host in toml_http) != null){
  39. if(toml_http[s_toml_http_host].type == TOMLType.STRING){}else{ return invalid_toml_value(s_toml_http, s_toml_http_host); }
  40. }else{ return invalid_toml_value(s_toml_http, s_toml_http_host); }
  41. if((s_toml_http_port in toml_http) != null){
  42. if(toml_http[s_toml_http_port].type == TOMLType.INTEGER){}else{ return invalid_toml_value(s_toml_http, s_toml_http_port); }
  43. }else{ return invalid_toml_value(s_toml_http, s_toml_http_port); }
  44. }else{ return invalid_toml_group(s_toml_http); }
  45. /*
  46. if((s_toml_db in toml_s) != null){
  47. auto toml_db = toml_s[s_toml_db];
  48. if((s_toml_db_host in toml_db) != null){
  49. if(toml_db[s_toml_db_host].type == TOMLType.STRING){}else{ return invalid_toml_value(s_toml_db, s_toml_db_host); }
  50. }else{ return invalid_toml_value(s_toml_db, s_toml_db_host); }
  51. if((s_toml_db_port in toml_db) != null){
  52. if(toml_db[s_toml_db_port].type == TOMLType.STRING){}else{ return invalid_toml_value(s_toml_db, s_toml_db_port); }
  53. }else{ return invalid_toml_value(s_toml_db, s_toml_db_port); }
  54. if((s_toml_db_name in toml_db) != null){
  55. if(toml_db[s_toml_db_name].type == TOMLType.STRING){}else{ return invalid_toml_value(s_toml_db, s_toml_db_name); }
  56. }else{ return invalid_toml_value(s_toml_db, s_toml_db_name); }
  57. if((s_toml_db_user in toml_db) != null){
  58. if(toml_db[s_toml_db_user].type == TOMLType.STRING){}else{ return invalid_toml_value(s_toml_db, s_toml_db_user); }
  59. }else{ return invalid_toml_value(s_toml_db, s_toml_db_user); }
  60. if((s_toml_db_pass in toml_db) != null){
  61. if(toml_db[s_toml_db_pass].type == TOMLType.STRING){}else{ return invalid_toml_value(s_toml_db, s_toml_db_pass); }
  62. }else{ return invalid_toml_value(s_toml_db, s_toml_db_pass); }
  63. if((s_toml_db_conn_timeout in toml_db) != null){
  64. if(toml_db[s_toml_db_conn_timeout].type == TOMLType.STRING){}else{ return invalid_toml_value(s_toml_db, s_toml_db_conn_timeout); }
  65. }else{ return invalid_toml_value(s_toml_db, s_toml_db_conn_timeout); }
  66. if((s_toml_db_conn_num in toml_db) != null){
  67. if(toml_db[s_toml_db_conn_num].type == TOMLType.INTEGER){}else{ return invalid_toml_value(s_toml_db, s_toml_db_conn_num); }
  68. }else{ return invalid_toml_value(s_toml_db, s_toml_db_conn_num); }
  69. }else{ return invalid_toml_group(s_toml_db); }
  70. */
  71. return true;
  72. }