settings_toml.d 3.7 KB

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