1use std::collections::hash_map::Entry;
7use std::collections::{BTreeMap, HashMap};
8
9use crate::errors::MspError;
10use crate::msp::{CommandAcceptedResult, CommandAckStatus, ErrorKind};
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14#[non_exhaustive]
15pub enum Submission {
16 New,
18 Retry,
20 Duplicate,
22 Conflict,
24}
25
26#[derive(Debug, Clone, PartialEq, Eq)]
28#[non_exhaustive]
29pub enum PendingSettlement<I> {
30 Acknowledged {
32 command_id: String,
34 input: I,
36 status: CommandAckStatus,
38 },
39 Refused {
41 command_id: String,
43 input: I,
45 reason: Option<String>,
47 },
48 Discarded {
50 command_id: String,
52 input: I,
54 },
55}
56
57#[derive(Debug, Clone, PartialEq, Eq)]
59pub struct PendingCommand<I> {
60 command_id: String,
61 input: I,
62 submission_index: u64,
63 retry_ready: bool,
64}
65
66impl<I> PendingCommand<I> {
67 #[must_use]
69 pub fn command_id(&self) -> &str {
70 &self.command_id
71 }
72
73 #[must_use]
75 pub const fn input(&self) -> &I {
76 &self.input
77 }
78
79 #[must_use]
82 pub const fn submission_index(&self) -> u64 {
83 self.submission_index
84 }
85
86 #[must_use]
89 pub const fn retry_ready(&self) -> bool {
90 self.retry_ready
91 }
92}
93
94#[derive(Debug)]
96pub struct PendingCommandSet<I> {
97 entries: HashMap<String, PendingCommand<I>>,
98 order: BTreeMap<u64, String>,
99 next_submission_index: u64,
100}
101
102impl<I> Default for PendingCommandSet<I> {
103 fn default() -> Self {
104 Self {
105 entries: HashMap::new(),
106 order: BTreeMap::new(),
107 next_submission_index: 0,
108 }
109 }
110}
111
112impl<I> PendingCommandSet<I> {
113 #[must_use]
115 pub fn new() -> Self {
116 Self::default()
117 }
118
119 #[must_use]
121 pub fn get(&self, command_id: &str) -> Option<&PendingCommand<I>> {
122 self.entries.get(command_id)
123 }
124
125 #[must_use]
127 pub fn len(&self) -> usize {
128 self.entries.len()
129 }
130
131 #[must_use]
133 pub fn is_empty(&self) -> bool {
134 self.entries.is_empty()
135 }
136
137 pub fn iter(&self) -> impl Iterator<Item = &PendingCommand<I>> {
139 self.order
140 .values()
141 .filter_map(|command_id| self.entries.get(command_id))
142 }
143
144 pub fn acknowledge(&mut self, ack: &CommandAcceptedResult) -> Option<PendingSettlement<I>> {
146 let entry = self.take(&ack.command_id)?;
147 Some(PendingSettlement::Acknowledged {
148 command_id: entry.command_id,
149 input: entry.input,
150 status: ack.status,
151 })
152 }
153
154 pub fn refuse(&mut self, error: &MspError) -> Option<PendingSettlement<I>> {
158 let data = error.data();
159 if data.kind != ErrorKind::CommandRejected {
160 return None;
161 }
162 let command_id = data.command_id.as_deref()?;
163 let entry = self.take(command_id)?;
164 Some(PendingSettlement::Refused {
165 command_id: entry.command_id,
166 input: entry.input,
167 reason: data.reason.clone(),
168 })
169 }
170
171 pub fn discard(&mut self, command_id: &str) -> Option<PendingSettlement<I>> {
173 let entry = self.take(command_id)?;
174 Some(PendingSettlement::Discarded {
175 command_id: entry.command_id,
176 input: entry.input,
177 })
178 }
179
180 pub fn retire_server_terminal(&mut self, command_id: &str) -> bool {
183 self.take(command_id).is_some()
184 }
185
186 pub fn transport_died(&mut self) {
189 for entry in self.entries.values_mut() {
190 entry.retry_ready = true;
191 }
192 }
193
194 fn take(&mut self, command_id: &str) -> Option<PendingCommand<I>> {
195 let entry = self.entries.remove(command_id)?;
196 self.order.remove(&entry.submission_index);
197 Some(entry)
198 }
199}
200
201impl<I: PartialEq> PendingCommandSet<I> {
202 pub fn submit(&mut self, command_id: impl Into<String>, input: I) -> Submission {
208 let command_id = command_id.into();
209 match self.entries.entry(command_id.clone()) {
210 Entry::Vacant(slot) => {
211 let submission_index = self.next_submission_index;
212 slot.insert(PendingCommand {
213 command_id: command_id.clone(),
214 input,
215 submission_index,
216 retry_ready: false,
217 });
218 self.next_submission_index += 1;
219 self.order.insert(submission_index, command_id);
220 Submission::New
221 }
222 Entry::Occupied(slot) if slot.get().input != input => Submission::Conflict,
223 Entry::Occupied(mut slot) if slot.get().retry_ready => {
224 slot.get_mut().retry_ready = false;
225 Submission::Retry
226 }
227 Entry::Occupied(_) => Submission::Duplicate,
228 }
229 }
230}