zsh_module/
log.rs

1//! Zsh native log functions. This module contains high level interfaces to the zsh log functions.
2
3use std::{
4    borrow::Cow,
5    ffi::{c_char, CStr, CString},
6};
7
8use zsh_sys as zsys;
9
10use crate::ToCString;
11
12/// Prints out a warning message from the command `cmd`. See [`crate::warn_named!`]
13pub fn warn_named(cmd: impl ToCString, msg: impl ToCString) {
14    let cmd_c = cmd.into_cstr();
15    let msg_c = msg.into_cstr();
16    unsafe { zsys::zwarnnam(cmd_c.as_ptr(), msg_c.as_ptr()) }
17}
18
19/// Prints out a warning message. See [`crate::warn!`]
20pub fn warn(msg: impl ToCString) {
21    let msg_c = msg.into_cstr();
22    unsafe { zsys::zwarn(msg_c.as_ptr()) }
23}
24
25/// Prints out an error message. See [`crate::error!`]
26pub fn error(msg: impl ToCString) {
27    let msg_c = msg.into_cstr();
28    unsafe { zsys::zerr(msg_c.as_ptr()) }
29}
30
31/// Prints out an error message from the command `cmd`. See [`crate::error_named!`]
32pub fn error_named(cmd: impl ToCString, msg: impl ToCString) {
33    let cmd = cmd.into_cstr();
34    let msg = msg.into_cstr();
35    unsafe { zsys::zerrnam(cmd.as_ptr(), msg.as_ptr()) }
36}
37
38#[macro_export]
39/// Prints out a warning message with a command name, like [`println!`]
40/// # Example
41/// ```no_run
42/// fn my_cd(action: &mut (), name: &str, args: &[&str]) -> zsh_module::MaybeError {
43///     if args.len() > 1 {
44///         zsh_module::warn_named!(name, "too much arguments!");
45///     }
46///     todo!()
47/// }
48///
49/// ```
50macro_rules! warn_named {
51    ($cmd:expr, $msg:expr $(,$val:expr)*) => {
52       $crate::log::warn_named($cmd, format!($msg, $($val),*))
53    };
54}
55
56#[macro_export]
57/// Prints out an error message with a command name, like [`println!`]
58/// # Example
59/// ```no_run
60/// fn my_cd(action: &mut (), name: &str, args: &[&str]) -> zsh_module::MaybeError {
61///     if args.len() > 1 {
62///         zsh_module::error_named!(name, "too much arguments!");
63///         return Err(todo!())
64///     }
65///    // code
66///    todo!()
67/// }
68///
69/// ```
70macro_rules! error_named {
71    ($cmd:expr, $msg:expr $(,$val:expr)*) => {
72       $crate::log::error_named($cmd, format!($msg, $($val),*))
73    };
74}
75
76/// Prints out a warning message, like [`println!`]
77/// # Example
78/// ```no_run
79/// let number = 10;
80/// if number != 42 {
81///     zsh_module::warn!("Wrong number, expected 42, got {}", number);
82/// }
83///
84/// ```
85#[macro_export]
86macro_rules! warn {
87    ($msg:expr $(,$val:expr)*) => {
88       $crate::log::warn(format!($msg, $($val),*))
89    };
90}
91
92/// Prints out an error message, like [`println!`]
93/// # Example
94/// ```no_run
95/// let number = 10;
96/// if number != 42 {
97///     zsh_module::error!("Wrong number, expected 42, got {}", number);
98///     return /* error */
99/// }
100///
101/// ```
102#[macro_export]
103macro_rules! error {
104    ($msg:expr $(,$val:expr)*) => {
105       $crate::log::error(format!($msg, $($val),*))
106    };
107}