use {parking_lot::Mutex, reovim_kernel::api::v1::Service};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PendingLevel {
Info,
Success,
Warning,
Error,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PendingOp {
Push {
level: PendingLevel,
title: String,
},
ProgressBegin {
token: String,
title: String,
message: String,
percentage: u8,
},
ProgressReport {
token: String,
message: Option<String>,
percentage: Option<u8>,
},
ProgressEnd {
token: String,
message: Option<String>,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PendingEntry {
pub source: Option<String>,
pub op: PendingOp,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PendingNotification {
pub level: PendingLevel,
pub title: String,
}
#[derive(Debug)]
pub struct PendingNotificationQueue {
queue: Mutex<Vec<PendingEntry>>,
}
impl PendingNotificationQueue {
#[must_use]
pub const fn new() -> Self {
Self {
queue: Mutex::new(Vec::new()),
}
}
pub fn push(&self, level: PendingLevel, title: impl Into<String>) {
self.queue.lock().push(PendingEntry {
source: None,
op: PendingOp::Push {
level,
title: title.into(),
},
});
}
pub fn push_op(&self, source: Option<String>, op: PendingOp) {
self.queue.lock().push(PendingEntry { source, op });
}
pub fn drain(&self) -> Vec<PendingEntry> {
let mut queue = self.queue.lock();
std::mem::take(&mut *queue)
}
#[must_use]
pub fn len(&self) -> usize {
self.queue.lock().len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.queue.lock().is_empty()
}
}
impl Default for PendingNotificationQueue {
fn default() -> Self {
Self::new()
}
}
impl Service for PendingNotificationQueue {}
#[cfg(test)]
#[path = "notification_queue_tests.rs"]
mod tests;