Debian Codegraph @

helix-event/src/status.rs

raw symbols SCIP
1 //! A queue of async messages/errors that will be shown in the editor
2
3 use std::borrow::Cow;
4 use std::time::Duration;
5
6 use crate::{runtime_local, send_blocking};
7 use once_cell::sync::OnceCell;
8 use tokio::sync::mpsc::{Receiver, Sender};
9
10 /// Describes the severity level of a [`StatusMessage`].
11 #[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord)]
12 pub enum Severity {
13 Hint,
14 Info,
15 Warning,
16 Error,
17 }
18
19 pub struct StatusMessage {
20 pub severity: Severity,
21 pub message: Cow<'static, str>,
22 }
23
24 impl From<anyhow::Error> for StatusMessage {
25 fn from(err: anyhow::Error) -> Self {
26 StatusMessage {
27 severity: Severity::Error,
28 message: err.to_string().into(),
29 }
30 }
31 }
32
33 impl From<&'static str> for StatusMessage {
34 fn from(msg: &'static str) -> Self {
35 StatusMessage {
36 severity: Severity::Info,
37 message: msg.into(),
38 }
39 }
40 }
41
42 runtime_local! {
43 static MESSAGES: OnceCell<Sender<StatusMessage>> = OnceCell::new();
44 }
45
46 pub async fn report(msg: impl Into<StatusMessage>) {
47 // if the error channel overflows just ignore it
48 let _ = MESSAGES
49 .wait()
50 .send_timeout(msg.into(), Duration::from_millis(10))
51 .await;
52 }
53
54 pub fn report_blocking(msg: impl Into<StatusMessage>) {
55 let messages = MESSAGES.wait();
56 send_blocking(messages, msg.into())
57 }
58
59 /// Must be called once during editor startup exactly once
60 /// before any of the messages in this module can be used
61 ///
62 /// # Panics
63 /// If called multiple times
64 pub fn setup() -> Receiver<StatusMessage> {
65 let (tx, rx) = tokio::sync::mpsc::channel(128);
66 let _ = MESSAGES.set(tx);
67 rx
68 }