You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

build.rs 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. use quote::quote;
  2. use std::fs::File;
  3. use std::io::{Read, Write};
  4. use strfmt;
  5. use translation_provider::generate_translation;
  6. generate_translation! {
  7. id,
  8. display,
  9. january,
  10. february,
  11. march,
  12. april,
  13. may,
  14. june,
  15. july,
  16. august,
  17. september,
  18. october,
  19. november,
  20. december,
  21. restart_required_info,
  22. budget_category_header,
  23. budget_amount_header,
  24. budget_balance_header,
  25. budget_category_name_placeholder,
  26. spending_name_header,
  27. spending_budget_category_header,
  28. spending_amount_header,
  29. spending_day_header,
  30. spending_name_placeholder,
  31. spending_category_name_placeholder,
  32. decimal_separator,
  33. thousands_separator,
  34. format_money(sign: &str, whole: &str, cents: &str),
  35. whole_balance(sign: &str, whole: &str, cents: &str),
  36. currency_symbol,
  37. }
  38. fn main() {
  39. let mut get_fn = quote! {};
  40. let mut get_language_list = quote! {};
  41. for entry in std::fs::read_dir("translations").expect("Could not open translation directory") {
  42. let entry = entry.unwrap();
  43. let path = entry.path();
  44. // the joy of OsString -> String
  45. let mut file = File::open(&path).unwrap();
  46. let mut content = String::new();
  47. file.read_to_string(&mut content)
  48. .expect("Could not read translation file");
  49. // make sure the file is correct - but this step could technically be skipped
  50. let translation: TranslationProvider =
  51. serde_json::from_str(&content).expect("Could not deserialize file");
  52. // those are the user defined ones, the ones generated
  53. // this should be automated somehow..!
  54. let id = translation.id();
  55. let display = translation.display();
  56. // gradually build our getter, using the filename
  57. // note: build some Vec<String> language index would also be interesting i.e. to show the
  58. // user when selecting a language in a ComboBox
  59. get_fn = quote! {
  60. #get_fn
  61. #id => #content,
  62. };
  63. get_language_list = quote! {
  64. #get_language_list
  65. (#id.to_string(), #display.to_string()),
  66. };
  67. }
  68. get_fn = quote! {
  69. pub fn get_provider(locale_id: &str) -> Option<TranslationProvider> {
  70. serde_json::from_str(match locale_id {
  71. #get_fn
  72. _ => return None,
  73. }).expect("TranslationProvider construction failed - the build is corrupted!")
  74. }
  75. };
  76. get_language_list = quote! {
  77. pub fn get_language_list() -> Vec<(String, String)> {
  78. vec![
  79. #get_language_list
  80. ]
  81. }
  82. };
  83. let final_generated = quote! {
  84. impl TranslationProvider {
  85. #get_fn
  86. #get_language_list
  87. }
  88. };
  89. let out_dir = std::env::var("OUT_DIR").unwrap();
  90. let dest_path = std::path::Path::new(&out_dir).join("translation_provider_generated.rs");
  91. let mut f = File::create(&dest_path).unwrap();
  92. f.write(TranslationProvider::generated_code().as_bytes())
  93. .unwrap();
  94. f.write(final_generated.to_string().as_bytes()).unwrap();
  95. }