Skip to main content

taxy/server/
port_list.rs

1use crate::proxy::PortContext;
2use taxy_api::{id::ShortId, port::PortEntry};
3
4#[derive(Default)]
5pub struct PortList {
6    contexts: Vec<PortContext>,
7}
8
9impl PortList {
10    pub fn entries(&self) -> impl Iterator<Item = &PortEntry> {
11        self.contexts.iter().map(|c| c.entry())
12    }
13
14    pub fn as_slice(&self) -> &[PortContext] {
15        &self.contexts
16    }
17
18    pub fn as_mut_slice(&mut self) -> &mut [PortContext] {
19        &mut self.contexts
20    }
21
22    pub fn get(&self, id: ShortId) -> Option<&PortContext> {
23        self.contexts.iter().find(|p| p.entry().id == id)
24    }
25
26    pub fn update(&mut self, ctx: PortContext) -> bool {
27        if let Some(index) = self
28            .contexts
29            .iter()
30            .position(|p| p.entry().id == ctx.entry().id)
31        {
32            if self.contexts[index].entry != ctx.entry {
33                self.contexts[index].apply(ctx);
34                true
35            } else {
36                false
37            }
38        } else {
39            self.contexts.push(ctx);
40            true
41        }
42    }
43
44    pub fn delete(&mut self, id: ShortId) -> bool {
45        if let Some(index) = self.contexts.iter().position(|p| p.entry().id == id) {
46            self.contexts.remove(index).reset();
47            true
48        } else {
49            false
50        }
51    }
52
53    pub fn reset(&mut self, id: ShortId) -> bool {
54        if let Some(index) = self.contexts.iter().position(|p| p.entry().id == id) {
55            self.contexts[index].reset();
56            true
57        } else {
58            false
59        }
60    }
61}