From 195a1e0ee80712d6cd32585afb5ffb56a4983fb0 Mon Sep 17 00:00:00 2001 From: ennucore Date: Fri, 27 Mar 2020 22:05:30 +0300 Subject: [PATCH] Tunnel.tunnel_to --- src/tunnel.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/tunnel.rs b/src/tunnel.rs index 94494d3..2b9f247 100644 --- a/src/tunnel.rs +++ b/src/tunnel.rs @@ -1,7 +1,9 @@ use crate::crypto::PublicKey; use alloc::vec::Vec; +use alloc::boxed::Box; use serde::{Serialize, Deserialize}; +use core::option::Option; #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] pub struct Tunnel { @@ -9,8 +11,29 @@ pub struct Tunnel { } impl Tunnel { + pub fn from_vec(nodes: Vec) -> Self { + Self { nodes } + } + pub fn tunnel_to(&self, me: &PublicKey, to: &PublicKey) -> Option { - None + let my_index = self.nodes + .iter() + .enumerate() + .find(|&node| node.1 == me).unwrap().0; + match self.nodes + .iter() + .enumerate() + .find(|&node| node.1 == to) { + Some(en) => { + match my_index < en.0 { + true => { Some(Tunnel::from_vec(Vec::::from(self.nodes.clone()[en.0..].to_vec()))) } + false => { Some(Tunnel::from_vec(Vec::::from(self.nodes.clone()[..=en.0].to_vec()))) } + } + } + None => { + None + } + } } pub fn next(&self, to: &PublicKey) -> PublicKey {