lib.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. //! Tauri utility helpers
  5. #![warn(missing_docs, rust_2018_idioms)]
  6. pub mod assets;
  7. pub mod config;
  8. pub mod html;
  9. pub mod platform;
  10. /// `tauri::App` package information.
  11. #[derive(Debug, Clone)]
  12. pub struct PackageInfo {
  13. /// App name
  14. pub name: String,
  15. /// App version
  16. pub version: String,
  17. /// The crate authors.
  18. pub authors: &'static str,
  19. /// The crate description.
  20. pub description: &'static str,
  21. }
  22. impl PackageInfo {
  23. /// Returns the application package name.
  24. /// On macOS and Windows it's the `name` field, and on Linux it's the `name` in `kebab-case`.
  25. pub fn package_name(&self) -> String {
  26. #[cfg(target_os = "linux")]
  27. {
  28. use heck::ToKebabCase;
  29. self.name.clone().to_kebab_case()
  30. }
  31. #[cfg(not(target_os = "linux"))]
  32. self.name.clone()
  33. }
  34. }
  35. /// Information about environment variables.
  36. #[derive(Debug, Clone)]
  37. #[non_exhaustive]
  38. pub struct Env {
  39. /// The APPIMAGE environment variable.
  40. #[cfg(target_os = "linux")]
  41. pub appimage: Option<std::ffi::OsString>,
  42. /// The APPDIR environment variable.
  43. #[cfg(target_os = "linux")]
  44. pub appdir: Option<std::ffi::OsString>,
  45. }
  46. #[allow(clippy::derivable_impls)]
  47. impl Default for Env {
  48. fn default() -> Self {
  49. #[cfg(target_os = "linux")]
  50. {
  51. let env = Self {
  52. #[cfg(target_os = "linux")]
  53. appimage: std::env::var_os("APPIMAGE"),
  54. #[cfg(target_os = "linux")]
  55. appdir: std::env::var_os("APPDIR"),
  56. };
  57. if env.appimage.is_some() || env.appdir.is_some() {
  58. // validate that we're actually running on an AppImage
  59. // an AppImage is mounted to `/tmp/.mount_${appPrefix}${hash}`
  60. // see https://github.com/AppImage/AppImageKit/blob/1681fd84dbe09c7d9b22e13cdb16ea601aa0ec47/src/runtime.c#L501
  61. // note that it is safe to use `std::env::current_exe` here since we just loaded an AppImage.
  62. if !std::env::current_exe()
  63. .map(|p| p.to_string_lossy().into_owned().starts_with("/tmp/.mount_"))
  64. .unwrap_or(true)
  65. {
  66. panic!("`APPDIR` or `APPIMAGE` environment variable found but this application was not detected as an AppImage; this might be a security issue.");
  67. }
  68. }
  69. env
  70. }
  71. #[cfg(not(target_os = "linux"))]
  72. {
  73. Self {}
  74. }
  75. }
  76. }
  77. /// The result type of `tauri-utils`.
  78. pub type Result<T> = std::result::Result<T, Error>;
  79. /// The error type of `tauri-utils`.
  80. #[derive(Debug, thiserror::Error)]
  81. #[non_exhaustive]
  82. pub enum Error {
  83. /// Target triple architecture error
  84. #[error("Unable to determine target-architecture")]
  85. Architecture,
  86. /// Target triple OS error
  87. #[error("Unable to determine target-os")]
  88. Os,
  89. /// Target triple environment error
  90. #[error("Unable to determine target-environment")]
  91. Environment,
  92. /// Tried to get resource on an unsupported platform
  93. #[error("Unsupported platform for reading resources")]
  94. UnsupportedPlatform,
  95. /// Get parent process error
  96. #[error("Could not get parent process")]
  97. ParentProcess,
  98. /// Get parent process PID error
  99. #[error("Could not get parent PID")]
  100. ParentPid,
  101. /// Get child process error
  102. #[error("Could not get child process")]
  103. ChildProcess,
  104. /// IO error
  105. #[error("{0}")]
  106. Io(#[from] std::io::Error),
  107. }