1use quick_error::quick_error;
2use std::borrow::Cow;
3use std::path::PathBuf;
4use std::process::ExitStatus;
5use std::{env, fmt, io, num, time};
6
7quick_error! {
8 #[derive(Debug)]
9 #[non_exhaustive]
10 pub enum CargoDebError {
11 Io(err: io::Error) {
12 from()
13 display("I/O error: {}", err)
14 source(err)
15 }
16 TomlParsing(err: cargo_toml::Error, path: PathBuf) {
17 display("Unable to parse {}", path.display())
18 source(err)
19 }
20 IoFile(msg: &'static str, err: io::Error, file: PathBuf) {
21 display("{msg}: {}{}{}",
22 file.display(),
23 file.is_relative().then(|| env::current_dir().ok()).flatten().map(|cwd| format!("\nnote: The current dir is '{}'", cwd.display())).unwrap_or_default(),
24 file.ancestors().find(|p| p.exists() && p.parent().is_some()).map(|p| format!("\nnote: '{}' exists", p.display())).unwrap_or_default(),
25 )
26 source(err)
27 }
28 CommandFailed(err: io::Error, cmd: Cow<'static, str>) {
29 display("Command `{cmd}` failed to launch\nnote: The current $PATH is {}", env::var("PATH").as_deref().unwrap_or("unset or invalid"))
30 source(err)
31 }
32 CommandError(msg: &'static str, arg: String, reason: Vec<u8>) {
33 display("{msg} ({arg}): {}", String::from_utf8_lossy(reason).trim_start_matches("error: "))
34 }
35 Str(msg: &'static str) {
36 display("{msg}")
37 from()
38 }
39 NumParse(msg: &'static str, err: num::ParseIntError) {
40 display("{msg}")
41 source(err)
42 }
43 InvalidVersion(msg: &'static str, ver: String) {
44 display("Version '{ver}' is invalid: {msg}")
45 }
46 InstallFailed(status: ExitStatus) {
47 display("Installation failed, because `dpkg -i` returned error {status}")
48 }
49 BuildFailed {
50 display("Build failed")
51 }
52 DebHelperReplaceFailed(name: PathBuf) {
53 display("Unable to replace #DEBHELPER# token in maintainer script '{}'", name.display())
54 }
55 StripFailed(name: PathBuf, reason: String) {
56 display("Unable to strip binary '{}': {reason}", name.display())
57 }
58 SystemTime(err: time::SystemTimeError) {
59 from()
60 display("Unable to get system time")
61 source(err)
62 }
63 ParseTOML(err: toml::de::Error) {
64 from()
65 display("Unable to parse Cargo.toml")
66 source(err)
67 }
68 ParseJSON(err: serde_json::Error) {
69 from()
70 display("Unable to parse `cargo metadata` output")
71 source(err)
72 }
73 PackageNotFound(path: String, reason: Vec<u8>) {
74 display("Path '{path}' does not belong to a package: {}", String::from_utf8_lossy(reason))
75 }
76 BinariesNotFound(crate_name: String) {
77 display("No binaries or cdylibs found. The package '{crate_name}' is empty. Please specify some assets to package in Cargo.toml")
78 }
79 PackageNotFoundInWorkspace(name: String, available: String) {
80 display("The workspace doesn't have a package named {name}.\nnote: Available packages are: {available}")
81 }
82 NoRootFoundInWorkspace(available: String) {
83 display("This is a workspace with multiple packages, and there is no single package at the root.\nPlease specify the package with `-p` or set one in the workspace's `default-members = []`.\nnote: Available packages are: {available}")
84 }
85 VariantNotFound(variant: String) {
86 display("[package.metadata.deb.variants.{}] not found in Cargo.toml", variant)
87 }
88 GlobPatternError(err: glob::PatternError) {
89 from()
90 display("Unable to parse glob pattern")
91 source(err)
92 }
93 AssetFileNotFound(source_path: PathBuf, target_path: PathBuf, is_glob: bool, is_built: bool) {
94 display("{} {}: {}\nNeeded for {}",
95 if *is_glob { "Glob pattern" } else { "Static file asset" },
96 if *is_built { "has not been built" } else { "path did not match any existing files" },
97 source_path.display(),
98 target_path.display(),
99 )
100 }
101 AssetGlobError(err: glob::GlobError) {
102 from()
103 display("Unable to iterate asset glob result")
104 source(err)
105 }
106 Context(msg: String, err: Box<CargoDebError>) {
107 display("{msg}")
108 source(err)
109 }
110 #[cfg(feature = "lzma")]
111 LzmaCompressionError(err: xz2::stream::Error) {
112 display("Lzma compression error: {:?}", err)
113 }
114 }
115}
116
117impl CargoDebError {
118 pub(crate) fn context(self, msg: impl fmt::Display) -> Self {
119 Self::Context(msg.to_string(), Box::new(self))
120 }
121}
122
123impl From<fmt::Error> for CargoDebError {
124 fn from(_: fmt::Error) -> Self {
125 Self::Str("fmt")
126 }
127}
128
129pub type CDResult<T> = Result<T, CargoDebError>;