hyper_scripter/error_display/
mod.rs

1use crate::error::{Error, Error::*, FormatCode, SysPath};
2use std::fmt::{Display, Formatter, Result};
3use std::path::PathBuf;
4
5fn fmt_multi_path(f: &mut Formatter, msg: &str, mutli_path: &[PathBuf]) -> Result {
6    write!(f, "{}", msg)?;
7    if !mutli_path.is_empty() {
8        write!(f, ":")?;
9    }
10    let mut it = mutli_path.iter();
11    if let Some(p) = it.next() {
12        writeln!(f)?;
13        write!(f, "{}", p.to_string_lossy())?;
14    }
15    for p in it {
16        write!(f, "{}", p.to_string_lossy())?;
17    }
18    Ok(())
19}
20
21impl Display for Error {
22    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
23        // LOCALE
24        match self {
25            DontFuzz | Caution => return Ok(()),
26            Empty => write!(f, "No existing script!")?,
27            NoPreviousArgs => write!(f, "No previous argument!")?,
28            SysPathNotFound(SysPath::Config) => write!(
29                f,
30                "Can not find you're config path. Usually it should be `$HOME/.config`",
31            )?,
32            EmptyCreate => write!(f, "Not creating new script due to file not changing.")?,
33            SysPathNotFound(SysPath::Home) => write!(f, "Can not find you're home path.")?,
34            PermissionDenied(v) => fmt_multi_path(f, "Permission denied", v)?,
35            PathNotFound(v) => fmt_multi_path(f, "Path not found", v)?,
36            PathExist(path) => write!(f, "Path already exist: {:?}", path)?,
37            ScriptExist(name) => write!(f, "Script already exist: {}", name)?,
38            ScriptIsFiltered(name) => write!(f, "Script filtered out: {}", name)?,
39            ScriptNotFound(name) => write!(f, "Script not found: {}", name)?,
40            UnknownType(t) => write!(f, "Unknown type: {}", t)?,
41            Format(code, s) => {
42                write!(f, "Format error for ")?;
43                use FormatCode::*;
44                match code {
45                    RangeQuery => write!(f, "range query")?,
46                    Config => write!(f, "config file")?,
47                    ScriptName => write!(f, "script name")?,
48                    Regex => write!(f, "regular expression")?,
49                    ScriptQuery => write!(f, "script query")?,
50                    ScriptType => write!(f, "script type")?,
51                    Tag => write!(f, "tag")?,
52                    PromptLevel => write!(f, "prompt level")?,
53                    EnvPair => write!(f, "env pair (e.g. VAR=1)")?,
54                    Template => write!(f, "template")?,
55                    NonEmptyArray => {
56                        write!(f, "non-empty array")?;
57                        return Ok(());
58                    }
59                }
60                write!(f, " '{}'", s)?;
61            }
62            ScriptError(code) => write!(f, "Script exited unexpectedly with {}", code)?,
63            PreRunError(code) => write!(f, "Pre-run script exited unexpectedly with {}", code)?,
64            EditorError(code, cmd) => {
65                let cmd = cmd.join(" ");
66                write!(f, "Editor `{}` exited unexpectedly with {}", cmd, code)?
67            }
68            NoAlias(alias) => write!(f, "No such alias: {}", alias)?,
69            RedundantOpt(opt) => write!(f, "Redundant option: {:?}", opt)?,
70            _ => {
71                log::warn!("未被正確打印的錯誤:{:?}", self);
72                write!(f, "{:?}", self)?;
73            }
74        }
75        writeln!(f)
76    }
77}