1use std::io;
2use std::path::PathBuf;
3
4use thiserror::Error;
5
6pub type Result<T> = std::result::Result<T, OhosAppError>;
7
8#[derive(Debug, Error)]
9pub enum OhosAppError {
10 #[error("{message}")]
11 Message { message: String },
12 #[error("failed to parse package metadata in [{manifest_path}]: {source}")]
13 ConfigParse {
14 manifest_path: PathBuf,
15 source: serde_json::Error,
16 },
17 #[error(
18 "missing required configuration [{field}]; provide it via {cli_flag}, env {env_names}, or [package.metadata.ohos-app.<profile>] in [{manifest_path}]"
19 )]
20 MissingRequiredConfig {
21 field: &'static str,
22 cli_flag: &'static str,
23 env_names: &'static str,
24 manifest_path: PathBuf,
25 },
26 #[error("failed to read file [{path}]: {source}")]
27 Io { path: PathBuf, source: io::Error },
28 #[error("failed to read Cargo metadata: {0}")]
29 CargoMetadata(#[from] cargo_metadata::Error),
30 #[error("Rust project [{manifest_path}] must define a library target")]
31 MissingLibraryTarget { manifest_path: PathBuf },
32 #[error("unsupported OHOS target triple [{target}]")]
33 UnsupportedTarget { target: String },
34 #[error("OpenHarmony SDK root does not exist: [{path}]")]
35 MissingSdkRoot { path: PathBuf },
36 #[error("OpenHarmony SDK version directory does not exist: [{path}]")]
37 MissingSdkVersion { path: PathBuf },
38 #[error("failed to discover an SDK version under [{root}]")]
39 NoSdkVersionsFound { root: PathBuf },
40 #[error("required file is missing: [{path}]")]
41 MissingFile { path: PathBuf },
42 #[error("failed to spawn command [{program}] in [{cwd}]: {source}")]
43 CommandSpawn {
44 program: String,
45 cwd: PathBuf,
46 source: io::Error,
47 },
48 #[error("command failed [{program}] in [{cwd}] with exit code {code:?}")]
49 CommandFailed {
50 program: String,
51 cwd: PathBuf,
52 code: Option<i32>,
53 },
54 #[error("no .app artifact was found under [{search_root}] after packaging")]
55 PackageArtifactNotFound { search_root: PathBuf },
56}
57
58pub type HarmonyAppError = OhosAppError;
59
60impl OhosAppError {
61 pub fn message(message: impl Into<String>) -> Self {
62 Self::Message {
63 message: message.into(),
64 }
65 }
66
67 pub fn io(path: impl Into<PathBuf>, source: io::Error) -> Self {
68 Self::Io {
69 path: path.into(),
70 source,
71 }
72 }
73}
74
75impl From<io::Error> for OhosAppError {
76 fn from(source: io::Error) -> Self {
77 Self::Message {
78 message: format!("I/O error: {source}"),
79 }
80 }
81}