Skip to main content

standout_input/
error.rs

1use std::io;
2
3#[derive(Debug, thiserror::Error)]
4pub enum InputError {
5    #[error("No editor found. Set VISUAL or EDITOR environment variable.")]
6    NoEditor,
7
8    #[error("Editor cancelled without saving.")]
9    EditorCancelled,
10
11    #[error("Editor failed: {0}")]
12    EditorFailed(#[source] io::Error),
13
14    #[error("Failed to read stdin: {0}")]
15    StdinFailed(#[source] io::Error),
16
17    #[error("Failed to read {path}: {source}")]
18    FileFailed {
19        path: String,
20        #[source]
21        source: io::Error,
22    },
23
24    #[error("Failed to read clipboard: {0}")]
25    ClipboardFailed(String),
26
27    #[error("Prompt cancelled by user.")]
28    PromptCancelled,
29
30    #[error("Prompt failed: {0}")]
31    PromptFailed(String),
32
33    #[error("Validation failed: {0}")]
34    ValidationFailed(String),
35
36    #[error("No input provided and no default available.")]
37    NoInput,
38
39    #[error("Required argument '{0}' not provided.")]
40    MissingArgument(String),
41
42    #[error("Failed to parse argument '{name}': {reason}")]
43    ParseError { name: String, reason: String },
44}
45
46impl InputError {
47    pub fn validation(msg: impl Into<String>) -> Self {
48        Self::ValidationFailed(msg.into())
49    }
50
51    pub fn file(path: impl Into<String>, source: io::Error) -> Self {
52        Self::FileFailed {
53            path: path.into(),
54            source,
55        }
56    }
57
58    pub fn parse(name: impl Into<String>, reason: impl Into<String>) -> Self {
59        Self::ParseError {
60            name: name.into(),
61            reason: reason.into(),
62        }
63    }
64}