dioxus_leaflet/types/
leaflet_resources.rs

1use serde::{Deserialize, Serialize};
2
3/// Leaflet resource configuration
4#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
5pub enum LeafletResources {
6    /// Use CDN with specified version
7    Cdn {
8        version: String,
9        base_url: Option<String>, // Allow custom CDN base URL
10    },
11    /// Use local files
12    Local {
13        css_path: String,
14        js_path: String,
15    },
16}
17
18impl LeafletResources {
19    /// Creates a CDN configuration with the specified version
20    pub fn cdn(version: impl Into<String>) -> Self {
21        Self::Cdn {
22            version: version.into(),
23            base_url: None,
24        }
25    }
26
27    /// Creates a CDN configuration with custom base URL
28    pub fn cdn_with_base_url(version: impl Into<String>, base_url: impl Into<String>) -> Self {
29        Self::Cdn {
30            version: version.into(),
31            base_url: Some(base_url.into()),
32        }
33    }
34
35    /// Creates a local files configuration
36    pub fn local(css_path: impl Into<String>, js_path: impl Into<String>) -> Self {
37        Self::Local {
38            css_path: css_path.into(),
39            js_path: js_path.into(),
40        }
41    }
42
43    /// Returns the CSS URL/path
44    pub fn css_url(&self) -> String {
45        match self {
46            Self::Cdn { version, base_url } => {
47                let base = base_url.as_deref().unwrap_or("https://unpkg.com");
48                format!("{}/leaflet@{}/dist/leaflet.css", base, version)
49            }
50            Self::Local { css_path, .. } => css_path.clone(),
51        }
52    }
53
54    /// Returns the JS URL/path
55    pub fn js_url(&self) -> String {
56        match self {
57            Self::Cdn { version, base_url } => {
58                let base = base_url.as_deref().unwrap_or("https://unpkg.com");
59                format!("{}/leaflet@{}/dist/leaflet.js", base, version)
60            }
61            Self::Local { js_path, .. } => js_path.clone(),
62        }
63    }
64
65    /// Returns the integrity hash for CSS (if using CDN with known versions)
66    pub fn css_integrity(&self) -> Option<String> {
67        match self {
68            Self::Cdn { version, base_url } if base_url.is_none() => {
69                // Only provide integrity for unpkg.com with known versions
70                match version.as_str() {
71                    "1.9.4" => Some("sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=".to_string()),
72                    "1.9.3" => Some("sha256-kLaT2GOSpHechhsozzB+flnD+zUyjE2LlfWPgU04xyI=".to_string()),
73                    "1.9.2" => Some("sha256-sA+zWATbFveLLNqWO2gtiw3HL/lh1giY/Inf1BJ0z14=".to_string()),
74                    _ => None,
75                }
76            }
77            _ => None,
78        }
79    }
80
81    /// Returns the integrity hash for JS (if using CDN with known versions)
82    pub fn js_integrity(&self) -> Option<String> {
83        match self {
84            Self::Cdn { version, base_url } if base_url.is_none() => {
85                // Only provide integrity for unpkg.com with known versions
86                match version.as_str() {
87                    "1.9.4" => Some("sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=".to_string()),
88                    "1.9.3" => Some("sha256-WBkoXOwTeyKclOHuWtc+i2uENFpDZ9YPdf5Hf+D7ewM=".to_string()),
89                    "1.9.2" => Some("sha256-o9N4PsYA2zOcVD5OHEHviWpTGQ4Q1jEzU7oJiE+zRCE=".to_string()),
90                    _ => None,
91                }
92            }
93            _ => None,
94        }
95    }
96}
97
98impl Default for LeafletResources {
99    fn default() -> Self {
100        Self::cdn("1.9.4")
101    }
102}