freya_native_core/
node_id.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use std::{
    ops::Deref,
    str::FromStr,
};

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Copy, PartialEq, Debug, Eq, PartialOrd, Ord, Hash, Default)]
pub struct NodeId(shipyard::EntityId);

impl Deref for NodeId {
    type Target = shipyard::EntityId;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl From<NodeId> for shipyard::EntityId {
    fn from(value: NodeId) -> Self {
        value.0
    }
}

impl From<shipyard::EntityId> for NodeId {
    fn from(value: shipyard::EntityId) -> Self {
        NodeId(value)
    }
}

impl FromStr for NodeId {
    type Err = std::fmt::Error;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let Some((index, gen)) = s.split_once('-') else {
            return Err(std::fmt::Error);
        };
        let index = index.parse().map_err(|_| std::fmt::Error)?;
        let gen = gen.parse().map_err(|_| std::fmt::Error)?;
        Ok(Self(shipyard::EntityId::new_from_index_and_gen(index, gen)))
    }
}

impl std::fmt::Display for NodeId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&format!("{}-{}", self.index(), self.gen()))
    }
}

impl torin::prelude::NodeKey for NodeId {}

impl ragnarok::NodeKey for NodeId {}