mixnet/core/sphinx/target.rs
1// Copyright 2022 Parity Technologies (UK) Ltd.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation
6// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7// and/or sell copies of the Software, and to permit persons to whom the
8// Software is furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19// DEALINGS IN THE SOFTWARE.
20
21//! Hop target type.
22
23use super::packet::{PeerId, RawMixnodeIndex, MAX_MIXNODE_INDEX};
24use std::fmt;
25
26/// The index of a mixnode in a session's mixnode list. The index is always <=
27/// [`MAX_MIXNODE_INDEX`].
28#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
29pub struct MixnodeIndex(RawMixnodeIndex);
30
31impl MixnodeIndex {
32 /// Get the raw index out. This will always be <= [`MAX_MIXNODE_INDEX`].
33 pub fn get(self) -> RawMixnodeIndex {
34 self.0
35 }
36}
37
38impl TryFrom<usize> for MixnodeIndex {
39 type Error = ();
40
41 fn try_from(index: usize) -> Result<Self, Self::Error> {
42 if index <= MAX_MIXNODE_INDEX as usize {
43 Ok(Self(index as RawMixnodeIndex))
44 } else {
45 Err(())
46 }
47 }
48}
49
50impl TryFrom<RawMixnodeIndex> for MixnodeIndex {
51 type Error = ();
52
53 fn try_from(index: RawMixnodeIndex) -> Result<Self, Self::Error> {
54 (index as usize).try_into()
55 }
56}
57
58impl fmt::Display for MixnodeIndex {
59 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
60 self.0.fmt(fmt)
61 }
62}
63
64#[derive(Debug, PartialEq, Eq)]
65pub enum Target {
66 MixnodeIndex(MixnodeIndex),
67 PeerId(PeerId),
68}