105 lines
2.7 KiB
Rust
105 lines
2.7 KiB
Rust
//! The pending-approval table: whoever takes an entry out answers it, and everyone else finds it
|
|
//! gone. In memory only.
|
|
|
|
use crate::policy::{Ask, Decision};
|
|
use proto::{DecisionRecord, DenyReason, PendingApproval, Timestamp};
|
|
use std::collections::BTreeMap;
|
|
use std::sync::mpsc::{Receiver, Sender, channel};
|
|
use std::sync::{Mutex, MutexGuard};
|
|
|
|
/// What the waiting thread is told. Boxed: clippy's `large_enum_variant` rejects it unboxed.
|
|
#[derive(Debug)]
|
|
pub enum Verdict {
|
|
Run(Box<Decision>),
|
|
Denied(DenyReason),
|
|
}
|
|
|
|
/// An owner's answer to a pending call.
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub enum Answer {
|
|
/// `by` is "bxctl" or a Mattermost user id; `post` the Mattermost post that answered.
|
|
Approved {
|
|
by: Option<String>,
|
|
post: Option<String>,
|
|
},
|
|
Refused {
|
|
by: Option<String>,
|
|
post: Option<String>,
|
|
reason: Option<String>,
|
|
},
|
|
Expired,
|
|
}
|
|
|
|
/// What an approval leaves: the verdict (run or deny) and the record written.
|
|
#[derive(Debug)]
|
|
pub struct Answered {
|
|
pub verdict: Verdict,
|
|
pub outcome: DecisionRecord,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct Entry {
|
|
pub info: PendingApproval,
|
|
pub ask: Ask,
|
|
pub reply: Sender<Verdict>,
|
|
}
|
|
|
|
#[derive(Debug, Default)]
|
|
pub struct Table {
|
|
entries: Mutex<BTreeMap<u64, Entry>>,
|
|
}
|
|
|
|
impl Table {
|
|
pub fn new() -> Table {
|
|
Table {
|
|
entries: Mutex::new(BTreeMap::new()),
|
|
}
|
|
}
|
|
|
|
fn lock(&self) -> MutexGuard<'_, BTreeMap<u64, Entry>> {
|
|
self.entries
|
|
.lock()
|
|
.unwrap_or_else(|poisoned| poisoned.into_inner())
|
|
}
|
|
|
|
pub fn insert(&self, info: PendingApproval, ask: Ask) -> Receiver<Verdict> {
|
|
let (sender, receiver) = channel();
|
|
self.lock().insert(
|
|
info.approval,
|
|
Entry {
|
|
info,
|
|
ask,
|
|
reply: sender,
|
|
},
|
|
);
|
|
receiver
|
|
}
|
|
|
|
/// Is approval `id` still pending? Only a hint: it may be taken the moment after.
|
|
pub fn contains(&self, id: u64) -> bool {
|
|
self.lock().contains_key(&id)
|
|
}
|
|
|
|
pub fn take(&self, id: u64) -> Option<Entry> {
|
|
self.lock().remove(&id)
|
|
}
|
|
|
|
pub fn take_expired(&self, now: Timestamp) -> Vec<Entry> {
|
|
// One lock: taking it once per id would let an approve slip in between.
|
|
let mut entries = self.lock();
|
|
let ids: Vec<u64> = entries
|
|
.values()
|
|
.filter(|entry| now >= entry.info.expires)
|
|
.map(|entry| entry.info.approval)
|
|
.collect();
|
|
ids.iter().filter_map(|id| entries.remove(id)).collect()
|
|
}
|
|
|
|
pub fn list(&self) -> Vec<PendingApproval> {
|
|
self.lock()
|
|
.values()
|
|
.map(|entry| entry.info.clone())
|
|
.collect()
|
|
}
|
|
}
|