rinf_cli/tool/
error.rs

1use std::error::Error;
2use std::fmt::{Display, Formatter};
3use std::path::PathBuf;
4
5/// Error type for Rinf configuration loading.
6#[derive(Debug)]
7pub enum SetupError {
8  // Below are automatically converted variants.
9  Io(std::io::Error),
10  Yaml(serde_yml::Error),
11  Clipboard(arboard::Error),
12  WatchingFile(notify::Error),
13  // Below are manually constructed variants.
14  ReflectionModule,
15  PubConfig(String),
16  BadFilePath(PathBuf),
17  NotFlutterApp,
18  TemplateApplied,
19  DuplicatedSignal(String),
20  CodeSyntax(String),
21  SubprocessError,
22}
23
24impl Error for SetupError {
25  fn source(&self) -> Option<&(dyn Error + 'static)> {
26    match self {
27      Self::Io(e) => Some(e),
28      Self::Yaml(e) => Some(e),
29      Self::Clipboard(e) => Some(e),
30      Self::WatchingFile(e) => Some(e),
31      _ => None,
32    }
33  }
34}
35
36impl Display for SetupError {
37  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
38    match self {
39      Self::Io(e) => {
40        write!(f, "Failed to operate on file: {}", e)
41      }
42      Self::Yaml(e) => {
43        write!(f, "Failed to parse YAML: {}", e)
44      }
45      Self::Clipboard(e) => {
46        write!(f, "Failed to use clipboard: {}", e)
47      }
48      Self::WatchingFile(e) => {
49        write!(f, "Failed to watch files: {}", e)
50      }
51      Self::ReflectionModule => {
52        write!(f, "Failed to write reflection modules")
53      }
54      Self::PubConfig(s) => {
55        write!(f, "Invalid `pubspec.yaml` config: {}", s)
56      }
57      Self::BadFilePath(p) => {
58        write!(f, "Invalid file path: `{}`", p.display())
59      }
60      Self::NotFlutterApp => {
61        write!(f, "This is not a Flutter app project")
62      }
63      Self::TemplateApplied => {
64        write!(f, "Rust template has already been applied")
65      }
66      Self::DuplicatedSignal(n) => {
67        write!(f, "Duplicated signals named `{}` were found", n)
68      }
69      Self::CodeSyntax(n) => {
70        write!(f, "Invalid syntax in file `{}`", n)
71      }
72      Self::SubprocessError => {
73        write!(f, "A subprocess did not exit successfully")
74      }
75    }
76  }
77}
78
79impl From<std::io::Error> for SetupError {
80  fn from(err: std::io::Error) -> Self {
81    Self::Io(err)
82  }
83}
84
85impl From<serde_yml::Error> for SetupError {
86  fn from(err: serde_yml::Error) -> Self {
87    Self::Yaml(err)
88  }
89}
90
91impl From<arboard::Error> for SetupError {
92  fn from(err: arboard::Error) -> Self {
93    Self::Clipboard(err)
94  }
95}
96
97impl From<notify::Error> for SetupError {
98  fn from(err: notify::Error) -> Self {
99    Self::WatchingFile(err)
100  }
101}