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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use custom_derive::custom_derive;
use indexed_vec::Idx;
use newtype_derive::{
    newtype_as_item, newtype_fmt, newtype_wrap_bin_op, newtype_wrap_bin_op_assign, NewtypeAdd,
    NewtypeAddAssign, NewtypeDebug, NewtypeDisplay,
};

custom_derive! {
    /// Represent a transition identifier in the network
    #[derive(
         Ord, PartialOrd, Clone, Copy, Eq, PartialEq, Hash,
        NewtypeDebug, NewtypeDisplay, NewtypeAddAssign(usize), Default, NewtypeAdd(usize)
    )]
    pub struct TransitionId(usize);
}

impl Idx for TransitionId {
    fn new(v: usize) -> Self {
        Self::from(v)
    }

    fn index(self) -> usize {
        self.0
    }
}

custom_derive! {
    /// Represent a place identifier in the network
    #[derive(
        Ord, PartialOrd, Clone, Copy, Eq, PartialEq, Hash,
        NewtypeDebug, NewtypeDisplay, NewtypeAddAssign(usize), Default, NewtypeAdd(usize)
    )]
    pub struct PlaceId(usize);
}

impl Idx for PlaceId {
    fn new(v: usize) -> Self {
        Self::from(v)
    }

    fn index(self) -> usize {
        self.0
    }
}

impl ::std::convert::From<usize> for PlaceId {
    fn from(v: usize) -> Self {
        PlaceId(v)
    }
}

impl ::std::convert::From<usize> for TransitionId {
    fn from(v: usize) -> Self {
        TransitionId(v)
    }
}

/// Represent an id in the network
#[derive(Eq, PartialEq, Hash, Debug, Clone, Copy)]
pub enum NodeId {
    /// Place
    Place(PlaceId),
    /// Transition
    Transition(TransitionId),
}

impl From<TransitionId> for NodeId {
    fn from(tr: TransitionId) -> Self {
        Self::Transition(tr)
    }
}

impl From<PlaceId> for NodeId {
    fn from(pl: PlaceId) -> Self {
        Self::Place(pl)
    }
}

impl NodeId {
    /// Try to convert NodeId to PlaceId
    pub fn as_place(&self) -> Option<PlaceId> {
        match self {
            NodeId::Place(pl) => Some(*pl),
            NodeId::Transition(_) => None,
        }
    }

    /// Try to convert NodeId to TransitionId
    pub fn as_transition(&self) -> Option<TransitionId> {
        match self {
            NodeId::Place(_) => None,
            NodeId::Transition(tr) => Some(*tr),
        }
    }
}