dioxus_leaflet/types/
id.rs

1use std::{
2    hash::Hash,
3    fmt,
4    rc::Rc,
5};
6use serde::Serialize;
7
8#[derive(Debug, Clone, PartialEq, Eq, Hash)]
9pub enum Id {
10    Map { id: usize },
11    Marker { parent: Rc<Id>, id: usize },
12    Polygon { parent: Rc<Id>, id: usize },
13    Popup { parent: Rc<Id>, id: usize },
14}
15
16impl Id {
17    pub fn map(id: usize) -> Self {
18        Id::Map { id }
19    }
20
21    pub fn marker(parent: &Rc<Id>, id: usize) -> Id {
22        Id::Marker { parent: parent.clone(), id }
23    }
24
25    pub fn polygon(parent: &Rc<Id>, id: usize) -> Id {
26        Id::Polygon { parent: parent.clone(), id }
27    }
28
29    pub fn popup(parent: &Rc<Id>, id: usize) -> Id {
30        Id::Popup { parent: parent.clone(), id }
31    }
32
33    pub fn id(&self) -> usize {
34        match self {
35            Id::Map { id } 
36                | Id::Marker { id, .. } 
37                | Id::Polygon { id, .. } 
38                | Id::Popup { id, .. } 
39                => *id,
40        }
41    }
42
43    pub fn parent(&self) -> Option<&Id> {
44        match self {
45            Id::Map { .. } => None,
46            Id::Marker { parent, .. }
47                | Id::Polygon { parent, .. } 
48                | Id::Popup { parent, .. }
49                => Some(parent.as_ref()),
50        }
51    }
52}
53
54impl Into<f64> for Id {
55    fn into(self) -> f64 {
56        match self {
57            Id::Map { id } 
58                | Id::Marker { id, .. } 
59                | Id::Polygon { id, .. } 
60                | Id::Popup { id, .. } 
61                => id as f64,
62        }
63    }
64}
65
66impl Serialize for Id {
67    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
68        match self {
69            Id::Map { id } 
70                | Id::Marker { id, .. } 
71                | Id::Polygon { id, .. } 
72                | Id::Popup { id, .. } 
73                => id.serialize(serializer),
74        }
75    }
76}
77
78impl fmt::Display for Id {
79    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80        match self {
81            Id::Map { id } => write!(f, "map-{id}"),
82            Id::Marker { id, .. } => write!(f, "marker-{id}"),
83            Id::Polygon { id, .. } => write!(f, "polygon-{id}"),
84            Id::Popup { id, .. } => write!(f, "popup-{id}"),
85        }
86    }
87}