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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use super::bundlepack::{BundlePack, Constraint};
use std::collections::HashMap;
use std::fmt::Debug;
pub trait BundleStore: Debug {
fn push(&mut self, bp: &BundlePack);
fn remove(&mut self, bid: String) -> Option<BundlePack>;
fn count(&self) -> u64;
fn all_ids(&self) -> Vec<String>;
fn has_item(&self, bp: &BundlePack) -> bool;
fn pending(&self) -> Vec<&BundlePack>;
fn ready(&self) -> Vec<&BundlePack>;
fn forwarding(&self) -> Vec<&BundlePack>;
fn bundles(&self) -> Vec<&BundlePack>;
fn bundles_status(&self) -> Vec<String> {
self.bundles().iter().map(|bp| bp.to_string()).collect()
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct SimpleBundleStore {
bundles: HashMap<String, BundlePack>,
}
impl BundleStore for SimpleBundleStore {
fn push(&mut self, bp: &BundlePack) {
let entry = self.bundles.entry(bp.id().to_string()).or_insert_with(|| bp.clone());
*entry = bp.clone();
}
fn remove(&mut self, bid: String) -> Option<BundlePack> {
self.bundles.remove(&bid)
}
fn count(&self) -> u64 {
self.bundles.len() as u64
}
fn all_ids(&self) -> Vec<String> {
self.bundles.keys().cloned().collect()
}
fn has_item(&self, bp: &BundlePack) -> bool {
self.bundles.contains_key(&bp.id().to_string())
}
fn pending(&self) -> Vec<&BundlePack> {
self.bundles
.values()
.filter(|&e| {
!e.has_constraint(Constraint::ReassemblyPending)
&& e.has_constraint(Constraint::Contraindicated)
})
.collect::<Vec<&BundlePack>>()
}
fn ready(&self) -> Vec<&BundlePack> {
self.bundles
.values()
.filter(|&e| {
!e.has_constraint(Constraint::ReassemblyPending)
&& !e.has_constraint(Constraint::Contraindicated)
})
.collect::<Vec<&BundlePack>>()
}
fn forwarding(&self) -> Vec<&BundlePack> {
self.bundles
.values()
.filter(|&e| e.has_constraint(Constraint::ForwardPending))
.collect::<Vec<&BundlePack>>()
}
fn bundles(&self) -> Vec<&BundlePack> {
self.bundles.values().collect()
}
}
impl Default for SimpleBundleStore {
fn default() -> Self {
SimpleBundleStore::new()
}
}
impl SimpleBundleStore {
pub fn new() -> SimpleBundleStore {
SimpleBundleStore {
bundles: HashMap::new(),
}
}
}