Skip to main content

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