Browse Source

Tunnel.tunnel_to

master
ennucore 5 years ago
parent
commit
195a1e0ee8
  1. 23
      src/tunnel.rs

23
src/tunnel.rs

@ -1,7 +1,9 @@
use crate::crypto::PublicKey; use crate::crypto::PublicKey;
use alloc::vec::Vec; use alloc::vec::Vec;
use alloc::boxed::Box;
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use core::option::Option;
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct Tunnel { pub struct Tunnel {
@ -9,9 +11,30 @@ pub struct Tunnel {
} }
impl Tunnel { impl Tunnel {
pub fn from_vec(nodes: Vec<PublicKey>) -> Self {
Self { nodes }
}
pub fn tunnel_to(&self, me: &PublicKey, to: &PublicKey) -> Option<Tunnel> { pub fn tunnel_to(&self, me: &PublicKey, to: &PublicKey) -> Option<Tunnel> {
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::<PublicKey>::from(self.nodes.clone()[en.0..].to_vec()))) }
false => { Some(Tunnel::from_vec(Vec::<PublicKey>::from(self.nodes.clone()[..=en.0].to_vec()))) }
}
}
None => {
None None
} }
}
}
pub fn next(&self, to: &PublicKey) -> PublicKey { pub fn next(&self, to: &PublicKey) -> PublicKey {
PublicKey { key: [0u8; 32] } PublicKey { key: [0u8; 32] }

Loading…
Cancel
Save