ytil_noxi/
notify.rs

1//! Notification utilities for sending error and warning messages to Nvim.
2
3use core::fmt::Debug;
4
5pub use nvim_oxi::api::opts;
6pub use nvim_oxi::api::types;
7use nvim_oxi::api::types::LogLevel;
8
9use crate::dict;
10
11/// Types that can be converted to a notification message for Nvim.
12///
13/// Implementors provide a way to transform themselves into a string suitable for display
14/// in Nvim notifications.
15pub trait Notifiable: Debug {
16    fn to_msg(&self) -> impl AsRef<str>;
17}
18
19impl<T: Notifiable + ?Sized> Notifiable for &T {
20    fn to_msg(&self) -> impl AsRef<str> {
21        (*self).to_msg()
22    }
23}
24
25impl Notifiable for color_eyre::Report {
26    fn to_msg(&self) -> impl AsRef<str> {
27        self.to_string()
28    }
29}
30
31impl Notifiable for String {
32    fn to_msg(&self) -> impl AsRef<str> {
33        self
34    }
35}
36
37impl Notifiable for &str {
38    fn to_msg(&self) -> impl AsRef<str> {
39        self
40    }
41}
42
43/// Notifies the user of an error message in Nvim.
44pub fn error<N: Notifiable>(notifiable: N) {
45    if let Err(err) = nvim_oxi::api::notify(notifiable.to_msg().as_ref(), LogLevel::Error, &dict! {}) {
46        nvim_oxi::dbg!(format!("cannot notify error | msg={notifiable:?} error={err:#?}"));
47    }
48}
49
50/// Notifies the user of a warning message in Nvim.
51pub fn warn<N: Notifiable>(notifiable: N) {
52    if let Err(err) = nvim_oxi::api::notify(notifiable.to_msg().as_ref(), LogLevel::Warn, &dict! {}) {
53        nvim_oxi::dbg!(format!("cannot notify warning | msg={notifiable:?} error={err:#?}"));
54    }
55}