lib.rs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #![doc = include_str!("../README.md")]
  2. #![doc(html_logo_url = "https://avatars.githubusercontent.com/u/79236386")]
  3. #![doc(html_favicon_url = "https://avatars.githubusercontent.com/u/79236386")]
  4. mod config;
  5. pub use config::*;
  6. mod bundle;
  7. pub use bundle::*;
  8. mod cargo;
  9. pub use cargo::*;
  10. #[doc(hidden)]
  11. pub mod __private {
  12. use crate::CrateConfig;
  13. pub const CONFIG_ENV: &str = "DIOXUS_CONFIG";
  14. pub fn save_config(config: &CrateConfig) -> CrateConfigDropGuard {
  15. std::env::set_var(CONFIG_ENV, serde_json::to_string(config).unwrap());
  16. CrateConfigDropGuard
  17. }
  18. /// A guard that removes the config from the environment when dropped.
  19. pub struct CrateConfigDropGuard;
  20. impl Drop for CrateConfigDropGuard {
  21. fn drop(&mut self) {
  22. std::env::remove_var(CONFIG_ENV);
  23. }
  24. }
  25. }
  26. /// An error that occurs when the dioxus CLI was not used to build the application.
  27. #[derive(Debug)]
  28. pub struct DioxusCLINotUsed;
  29. impl std::fmt::Display for DioxusCLINotUsed {
  30. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  31. f.write_str("dioxus CLI was not used to build the application")
  32. }
  33. }
  34. impl std::error::Error for DioxusCLINotUsed {}
  35. #[cfg(feature = "read-config")]
  36. /// The current crate's configuration.
  37. pub static CURRENT_CONFIG: once_cell::sync::Lazy<
  38. Result<crate::config::CrateConfig, DioxusCLINotUsed>,
  39. > = once_cell::sync::Lazy::new(|| {
  40. CURRENT_CONFIG_JSON
  41. .and_then(|config| serde_json::from_str(config).ok())
  42. .ok_or_else(|| {
  43. tracing::error!("A library is trying to access the crate's configuration, but the dioxus CLI was not used to build the application.");
  44. DioxusCLINotUsed
  45. })
  46. });
  47. #[cfg(feature = "read-config")]
  48. /// The current crate's configuration.
  49. pub const CURRENT_CONFIG_JSON: Option<&str> = std::option_env!("DIOXUS_CONFIG");