polars_utils/
error.rs

1use std::borrow::Cow;
2use std::fmt::{Display, Formatter};
3
4use crate::config::verbose;
5use crate::format_pl_smallstr;
6
7type ErrString = Cow<'static, str>;
8
9#[derive(Debug)]
10pub enum PolarsUtilsError {
11    ComputeError(ErrString),
12}
13
14impl Display for PolarsUtilsError {
15    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
16        match self {
17            PolarsUtilsError::ComputeError(s) => {
18                let s = s.as_ref();
19                write!(f, "{s}")
20            },
21        }
22    }
23}
24
25pub type Result<T> = std::result::Result<T, PolarsUtilsError>;
26
27/// Utility whose Display impl truncates the string unless POLARS_VERBOSE is set.
28pub struct TruncateErrorDetail<'a>(pub &'a str);
29
30impl std::fmt::Display for TruncateErrorDetail<'_> {
31    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
32        let maybe_truncated = if verbose() {
33            self.0
34        } else {
35            // Clamp the output on non-verbose
36            &self.0[..self.0.len().min(4096)]
37        };
38
39        f.write_str(maybe_truncated)?;
40
41        if maybe_truncated.len() != self.0.len() {
42            let n_more = self.0.len() - maybe_truncated.len();
43            f.write_str(" ...(set POLARS_VERBOSE=1 to see full response (")?;
44            f.write_str(&format_pl_smallstr!("{}", n_more))?;
45            f.write_str(" more characters))")?;
46        };
47
48        Ok(())
49    }
50}