Skip to main content

crossbuild_core/
error.rs

1//! Error types for `cargo-crossbuild`.
2
3use std::io;
4use std::path::PathBuf;
5use which;
6
7/// Errors produced by `cargo-crossbuild`.
8#[derive(Debug, thiserror::Error)]
9pub enum CrossBuildError {
10    #[error("invalid target triple `{target}`: {reason}")]
11    InvalidTarget { target: String, reason: String },
12
13    #[error("could not find Cargo.toml starting from {}", searched_from.display())]
14    ManifestNotFound { searched_from: PathBuf },
15
16    #[error("manifest path must point to Cargo.toml, got {}", path.display())]
17    ManifestNotCargoToml { path: PathBuf },
18
19    #[error("unable to execute cargo program `{program}`")]
20    CargoUnavailable { program: String },
21
22    #[error("host `{host}` does not support target `{target}` in the current configuration")]
23    HostUnsupported { host: String, target: String },
24
25    #[error("provider `{provider}` failed: {reason}")]
26    ProviderFailed { provider: String, reason: String },
27
28    #[error("no suitable {provider_type} provider found for target `{target}`")]
29    ProviderNotFound { provider_type: String, target: String },
30
31    #[error("build command `{command}` exited with status {exit_code:?}")]
32    BuildFailed { command: String, exit_code: Option<i32> },
33
34    #[error("{message}")]
35    Configuration { message: String },
36
37    #[error("I/O error at {}: {source}", path.as_ref().map(|p| format!("{}", p.display())).unwrap_or_else(|| String::new()))]
38    Io { path: Option<PathBuf>, source: io::Error },
39
40    #[error("required tool not found: {tool}")]
41    ToolNotFound { tool: String },
42
43    #[error("sysroot not needed for native build")]
44    SysrootNotNeeded,
45
46    #[error("sysroot not found for target `{target}`")]
47    SysrootNotFound { target: String },
48
49    #[error("checksum verification failed for {url}: expected {expected}, got {actual}")]
50    ChecksumMismatch { url: String, expected: String, actual: String },
51
52    #[error("download failed for {url}: {reason}")]
53    DownloadFailed { url: String, reason: String },
54
55    #[error("lockfile corrupted: {reason}")]
56    LockfileCorrupted { reason: String },
57
58    #[error("cache operation failed: {reason}")]
59    CacheError { reason: String },
60
61    #[error("which error: {source}")]
62    WhichError { source: which::Error },
63}
64
65impl CrossBuildError {
66    pub fn configuration(message: impl Into<String>) -> Self {
67        Self::Configuration {
68            message: message.into(),
69        }
70    }
71}
72
73impl From<io::Error> for CrossBuildError {
74    fn from(source: io::Error) -> Self {
75        CrossBuildError::Io { path: None, source }
76    }
77}
78
79impl From<which::Error> for CrossBuildError {
80    fn from(source: which::Error) -> Self {
81        CrossBuildError::WhichError { source }
82    }
83}
84
85impl From<super::model::HostDetectError> for CrossBuildError {
86    fn from(e: super::model::HostDetectError) -> Self {
87        CrossBuildError::configuration(e.to_string())
88    }
89}