file_system.rs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. use tauri_ui::WebView;
  2. use crate::dir;
  3. use crate::execute_promise;
  4. use crate::file;
  5. use std::fs::File;
  6. use std::io::Write;
  7. pub fn list<T: 'static>(
  8. webview: &mut WebView<'_, T>,
  9. path: String,
  10. callback: String,
  11. error: String,
  12. ) {
  13. execute_promise(
  14. webview,
  15. move || {
  16. dir::walk_dir(path.to_string())
  17. .and_then(|f| serde_json::to_string(&f).map_err(|err| err.to_string()))
  18. },
  19. callback,
  20. error,
  21. );
  22. }
  23. pub fn list_dirs<T: 'static>(
  24. webview: &mut WebView<'_, T>,
  25. path: String,
  26. callback: String,
  27. error: String,
  28. ) {
  29. execute_promise(
  30. webview,
  31. move || {
  32. dir::list_dir_contents(&path)
  33. .and_then(|f| serde_json::to_string(&f).map_err(|err| err.to_string()))
  34. },
  35. callback,
  36. error,
  37. );
  38. }
  39. pub fn write_file<T: 'static>(
  40. webview: &mut WebView<'_, T>,
  41. file: String,
  42. contents: String,
  43. callback: String,
  44. error: String,
  45. ) {
  46. execute_promise(
  47. webview,
  48. move || {
  49. File::create(file)
  50. .map_err(|err| err.to_string())
  51. .and_then(|mut f| {
  52. f.write_all(contents.as_bytes())
  53. .map_err(|err| err.to_string())
  54. .map(|_| "".to_string())
  55. })
  56. },
  57. callback,
  58. error,
  59. );
  60. }
  61. pub fn read_text_file<T: 'static>(
  62. webview: &mut WebView<'_, T>,
  63. path: String,
  64. callback: String,
  65. error: String,
  66. ) {
  67. execute_promise(
  68. webview,
  69. move || {
  70. file::read_string(path).and_then(|f| {
  71. serde_json::to_string(&f)
  72. .map_err(|err| err.to_string())
  73. .map(|s| s.to_string())
  74. })
  75. },
  76. callback,
  77. error,
  78. );
  79. }
  80. pub fn read_binary_file<T: 'static>(
  81. webview: &mut WebView<'_, T>,
  82. path: String,
  83. callback: String,
  84. error: String,
  85. ) {
  86. execute_promise(
  87. webview,
  88. move || {
  89. file::read_binary(path).and_then(|f| {
  90. serde_json::to_string(&f)
  91. .map_err(|err| err.to_string())
  92. .map(|s| s.to_string())
  93. })
  94. },
  95. callback,
  96. error,
  97. );
  98. }