path.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. use std::path::PathBuf;
  2. use serde_repr::{Serialize_repr, Deserialize_repr};
  3. #[derive(Serialize_repr, Deserialize_repr, Clone, Debug)]
  4. #[repr(u16)]
  5. pub enum BaseDirectory {
  6. Audio = 1,
  7. Cache,
  8. Config,
  9. Data,
  10. LocalData,
  11. Desktop,
  12. Document,
  13. Download,
  14. Executable,
  15. Font,
  16. Home,
  17. Picture,
  18. Public,
  19. Runtime,
  20. Template,
  21. Video,
  22. Resource,
  23. App,
  24. }
  25. pub fn resolve_path(path: String, dir: Option<BaseDirectory>) -> crate::Result<String> {
  26. if let Some(base_dir) = dir {
  27. let base_dir_path = match base_dir {
  28. BaseDirectory::Audio => audio_dir(),
  29. BaseDirectory::Cache => cache_dir(),
  30. BaseDirectory::Config => config_dir(),
  31. BaseDirectory::Data => data_dir(),
  32. BaseDirectory::LocalData => local_data_dir(),
  33. BaseDirectory::Desktop => desktop_dir(),
  34. BaseDirectory::Document => document_dir(),
  35. BaseDirectory::Download => download_dir(),
  36. BaseDirectory::Executable => executable_dir(),
  37. BaseDirectory::Font => font_dir(),
  38. BaseDirectory::Home => home_dir(),
  39. BaseDirectory::Picture => picture_dir(),
  40. BaseDirectory::Public => public_dir(),
  41. BaseDirectory::Runtime => runtime_dir(),
  42. BaseDirectory::Template => template_dir(),
  43. BaseDirectory::Video => video_dir(),
  44. BaseDirectory::Resource => resource_dir(),
  45. BaseDirectory::App => app_dir(),
  46. };
  47. if let Some(mut base_dir_path_value) = base_dir_path {
  48. base_dir_path_value.push(path);
  49. Ok(base_dir_path_value.to_string_lossy().to_string())
  50. } else {
  51. Err(crate::Error::from(crate::ErrorKind::Path("unable to determine base dir path".to_string())))
  52. }
  53. } else {
  54. Ok(path)
  55. }
  56. }
  57. // Returns the path to the user's audio directory.
  58. pub fn audio_dir() -> Option<PathBuf> {
  59. dirs::audio_dir()
  60. }
  61. // Returns the path to the user's cache directory.
  62. pub fn cache_dir() -> Option<PathBuf> {
  63. dirs::cache_dir()
  64. }
  65. // Returns the path to the user's config directory.
  66. pub fn config_dir() -> Option<PathBuf> {
  67. dirs::config_dir()
  68. }
  69. // Returns the path to the user's data directory.
  70. pub fn data_dir() -> Option<PathBuf> {
  71. dirs::data_dir()
  72. }
  73. // Returns the path to the user's local data directory.
  74. pub fn local_data_dir() -> Option<PathBuf> {
  75. dirs::data_local_dir()
  76. }
  77. // Returns the path to the user's desktop directory.
  78. pub fn desktop_dir() -> Option<PathBuf> {
  79. dirs::desktop_dir()
  80. }
  81. // Returns the path to the user's document directory.
  82. pub fn document_dir() -> Option<PathBuf> {
  83. dirs::document_dir()
  84. }
  85. // Returns the path to the user's download directory.
  86. pub fn download_dir() -> Option<PathBuf> {
  87. dirs::download_dir()
  88. }
  89. // Returns the path to the user's executable directory.
  90. pub fn executable_dir() -> Option<PathBuf> {
  91. dirs::executable_dir()
  92. }
  93. // Returns the path to the user's font directory.
  94. pub fn font_dir() -> Option<PathBuf> {
  95. dirs::font_dir()
  96. }
  97. // Returns the path to the user's home directory.
  98. pub fn home_dir() -> Option<PathBuf> {
  99. dirs::home_dir()
  100. }
  101. // Returns the path to the user's picture directory.
  102. pub fn picture_dir() -> Option<PathBuf> {
  103. dirs::picture_dir()
  104. }
  105. // Returns the path to the user's public directory.
  106. pub fn public_dir() -> Option<PathBuf> {
  107. dirs::public_dir()
  108. }
  109. // Returns the path to the user's runtime directory.
  110. pub fn runtime_dir() -> Option<PathBuf> {
  111. dirs::runtime_dir()
  112. }
  113. // Returns the path to the user's template directory.
  114. pub fn template_dir() -> Option<PathBuf> {
  115. dirs::template_dir()
  116. }
  117. // Returns the path to the user's video dir
  118. pub fn video_dir() -> Option<PathBuf> {
  119. dirs::video_dir()
  120. }
  121. pub fn resource_dir() -> Option<PathBuf> {
  122. crate::platform::resource_dir().ok()
  123. }
  124. fn app_name() -> crate::Result<String> {
  125. let exe = std::env::current_exe()?;
  126. let app_name = exe
  127. .file_name().expect("failed to get exe filename")
  128. .to_string_lossy();
  129. Ok(app_name.to_string())
  130. }
  131. pub fn app_dir() -> Option<PathBuf> {
  132. dirs::config_dir()
  133. .and_then(|mut dir| {
  134. if let Ok(app_name) = app_name() {
  135. dir.push(app_name);
  136. Some(dir)
  137. } else {
  138. None
  139. }
  140. })
  141. }