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
#[derive(Debug, Clone)] pub struct Session{ is_self_a_router: bool, subscriptions: Vec<String>, } impl Session{ pub fn new() -> Self{ Self{ is_self_a_router: false, subscriptions: Vec::new()} } pub fn set_a_router(&mut self, is_a_router: bool) -> Option<()>{ match is_a_router != self.is_self_a_router { true => { self.is_self_a_router = is_a_router; Some(()) } false => { None } } } pub fn is_a_router(&self) -> bool{ self.is_self_a_router } pub fn clear_subscriptions(&mut self) -> &Self{ self.subscriptions = Vec::new(); self } pub fn sub(&mut self, key: String) -> bool{ if self.subscriptions.contains(&key){ return false; } self.subscriptions.push(key); self.subscriptions.sort(); return true; } pub fn unsub(&mut self, key: String) -> bool{ match self.subscriptions.binary_search(&key){ Ok(position) => { self.subscriptions.remove(position); true } Err(_) => { false } } } pub fn is_sub(&self, key: &String) -> bool{ self.subscriptions.contains(&key) } pub fn get_subscriptions(&self) -> Vec<String>{ self.subscriptions.clone() } }