platform.rs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. use serde::{Deserialize, Serialize};
  2. use std::fmt::Display;
  3. use std::str::FromStr;
  4. #[derive(
  5. Copy,
  6. Clone,
  7. Hash,
  8. PartialEq,
  9. Eq,
  10. PartialOrd,
  11. Ord,
  12. Serialize,
  13. Deserialize,
  14. Debug,
  15. Default,
  16. clap::ValueEnum,
  17. )]
  18. #[non_exhaustive]
  19. pub(crate) enum Platform {
  20. /// Targeting the web platform using WASM
  21. #[clap(name = "web")]
  22. #[serde(rename = "web")]
  23. #[default]
  24. Web,
  25. /// Targeting macos desktop
  26. /// When running on macos, you can also use `--platform desktop` to build for the desktop
  27. #[cfg_attr(target_os = "macos", clap(alias = "desktop"))]
  28. #[clap(name = "macos")]
  29. #[serde(rename = "macos")]
  30. MacOS,
  31. /// Targeting windows desktop
  32. /// When running on windows, you can also use `--platform desktop` to build for the desktop
  33. #[cfg_attr(target_os = "windows", clap(alias = "desktop"))]
  34. #[clap(name = "windows")]
  35. #[serde(rename = "windows")]
  36. Windows,
  37. /// Targeting linux desktop
  38. /// When running on linux, you can also use `--platform desktop` to build for the desktop
  39. #[cfg_attr(target_os = "linux", clap(alias = "desktop"))]
  40. #[clap(name = "linux")]
  41. #[serde(rename = "linux")]
  42. Linux,
  43. /// Targeting the ios platform
  44. ///
  45. /// Can't work properly if you're not building from an Apple device.
  46. #[clap(name = "ios")]
  47. #[serde(rename = "ios")]
  48. Ios,
  49. /// Targeting the android platform
  50. #[clap(name = "android")]
  51. #[serde(rename = "android")]
  52. Android,
  53. /// Targeting the server platform using Axum and Dioxus-Fullstack
  54. ///
  55. /// This is implicitly passed if `fullstack` is enabled as a feature. Using this variant simply
  56. /// means you're only building the server variant without the `.wasm` to serve.
  57. #[clap(name = "server")]
  58. #[serde(rename = "server")]
  59. Server,
  60. /// Targeting the static generation platform using SSR and Dioxus-Fullstack
  61. #[clap(name = "liveview")]
  62. #[serde(rename = "liveview")]
  63. Liveview,
  64. }
  65. /// An error that occurs when a platform is not recognized
  66. pub(crate) struct UnknownPlatformError;
  67. impl std::fmt::Display for UnknownPlatformError {
  68. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  69. write!(f, "Unknown platform")
  70. }
  71. }
  72. impl FromStr for Platform {
  73. type Err = UnknownPlatformError;
  74. fn from_str(s: &str) -> Result<Self, Self::Err> {
  75. match s {
  76. "web" => Ok(Self::Web),
  77. "macos" => Ok(Self::MacOS),
  78. "windows" => Ok(Self::Windows),
  79. "linux" => Ok(Self::Linux),
  80. "liveview" => Ok(Self::Liveview),
  81. "server" => Ok(Self::Server),
  82. "ios" => Ok(Self::Ios),
  83. "android" => Ok(Self::Android),
  84. _ => Err(UnknownPlatformError),
  85. }
  86. }
  87. }
  88. impl Display for Platform {
  89. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  90. f.write_str(match self {
  91. Platform::Web => "web",
  92. Platform::MacOS => "macos",
  93. Platform::Windows => "windows",
  94. Platform::Linux => "linux",
  95. Platform::Ios => "ios",
  96. Platform::Android => "android",
  97. Platform::Server => "server",
  98. Platform::Liveview => "liveview",
  99. })
  100. }
  101. }
  102. impl Platform {
  103. /// Get the feature name for the platform in the dioxus crate
  104. pub(crate) fn feature_name(&self) -> &str {
  105. match self {
  106. Platform::Web => "web",
  107. Platform::MacOS => "desktop",
  108. Platform::Windows => "desktop",
  109. Platform::Linux => "desktop",
  110. Platform::Server => "server",
  111. Platform::Liveview => "liveview",
  112. Platform::Ios => "mobile",
  113. Platform::Android => "mobile",
  114. }
  115. }
  116. /// Get the name of the folder we need to generate for this platform
  117. ///
  118. /// Note that web and server share the same platform folder since we'll export the web folder as a bundle on its own
  119. pub(crate) fn build_folder_name(&self) -> &'static str {
  120. match self {
  121. Platform::Web => "web",
  122. Platform::Server => "web",
  123. Platform::Liveview => "liveview",
  124. Platform::Ios => "ios",
  125. Platform::Android => "android",
  126. Platform::Windows => "windows",
  127. Platform::Linux => "linux",
  128. Platform::MacOS => "macos",
  129. }
  130. }
  131. pub(crate) fn expected_name(&self) -> &'static str {
  132. match self {
  133. Platform::Web => "Web",
  134. Platform::MacOS => "Desktop MacOS",
  135. Platform::Windows => "Desktop Windows",
  136. Platform::Linux => "Desktop Linux",
  137. Platform::Ios => "Mobile iOS",
  138. Platform::Android => "Mobile Android",
  139. Platform::Server => "Server",
  140. Platform::Liveview => "Liveview",
  141. }
  142. }
  143. pub(crate) fn autodetect_from_cargo_feature(feature: &str) -> Option<Self> {
  144. match feature {
  145. "web" => Some(Platform::Web),
  146. "desktop" | "native" => {
  147. #[cfg(target_os = "macos")]
  148. {
  149. Some(Platform::MacOS)
  150. }
  151. #[cfg(target_os = "windows")]
  152. {
  153. Some(Platform::Windows)
  154. }
  155. #[cfg(target_os = "linux")]
  156. {
  157. Some(Platform::Linux)
  158. }
  159. // Possibly need a something for freebsd? Maybe default to Linux?
  160. #[cfg(not(any(target_os = "linux", target_os = "windows", target_os = "macos")))]
  161. {
  162. None
  163. }
  164. }
  165. "mobile" => None,
  166. "liveview" => Some(Platform::Liveview),
  167. "server" => Some(Platform::Server),
  168. _ => None,
  169. }
  170. }
  171. }