Skip to main content

effindom_native_packaging/
error.rs

1use std::fmt;
2use std::path::PathBuf;
3
4pub type Result<T> = std::result::Result<T, Error>;
5
6#[derive(Debug)]
7pub enum Error {
8    ReadFile {
9        path: PathBuf,
10        source: std::io::Error,
11    },
12    InvalidIconPath(PathBuf),
13    DecodeIcon {
14        path: PathBuf,
15        message: String,
16    },
17    InvalidIconDimensions {
18        path: PathBuf,
19        width: u32,
20        height: u32,
21    },
22    PngIconTooSmall {
23        path: PathBuf,
24        width: u32,
25        height: u32,
26        minimum: u32,
27    },
28    InvisibleIcon(PathBuf),
29    InvalidIconRasterSize(u32),
30    DuplicateIconRasterSize(u32),
31    MissingIconRaster(u32),
32    InvalidIconRasterData {
33        size: u32,
34        expected: usize,
35        actual: usize,
36    },
37    InvalidIconName(String),
38    EncodeIcon {
39        format: &'static str,
40        message: String,
41    },
42    InvalidApplicationMetadata {
43        field: &'static str,
44        message: String,
45    },
46    InvalidBundlePath {
47        path: PathBuf,
48        message: &'static str,
49    },
50    MissingBundleInput(PathBuf),
51    UnsupportedBundleInput(PathBuf),
52    DuplicateBundlePath(PathBuf),
53    SourceOutputOverlap {
54        source: PathBuf,
55        destination: PathBuf,
56    },
57    BundleOutputExists(PathBuf),
58    BundleIo {
59        operation: &'static str,
60        path: PathBuf,
61        source: std::io::Error,
62    },
63    PublishBundle {
64        from: PathBuf,
65        to: PathBuf,
66        source: std::io::Error,
67    },
68    RollbackBundle {
69        backup: PathBuf,
70        destination: PathBuf,
71        source: std::io::Error,
72    },
73    SerializePackageRecord(serde_json::Error),
74    ParsePackageRecord {
75        path: PathBuf,
76        source: serde_json::Error,
77    },
78    UnsupportedPackageRecordSchema(u32),
79    PackageRecordArtifactMissing(PathBuf),
80    PackageRecordLengthMismatch {
81        path: PathBuf,
82        expected: u64,
83        actual: u64,
84    },
85    PackageRecordChecksumMismatch {
86        path: PathBuf,
87        expected: String,
88        actual: String,
89    },
90    InvalidArchiveName(PathBuf),
91    ArchiveOutputExists(PathBuf),
92    ArchiveIo {
93        operation: &'static str,
94        path: PathBuf,
95        source: std::io::Error,
96    },
97    ArchiveEncoding {
98        format: &'static str,
99        message: String,
100    },
101    ArchiveDecoding {
102        format: &'static str,
103        message: String,
104    },
105    ArchiveChecksumMismatch {
106        path: PathBuf,
107        expected: String,
108        actual: String,
109    },
110    ArchivePackageRecordMismatch(PathBuf),
111    UnsupportedArchiveEntry(PathBuf),
112    InvalidUniversalMacOsSlice {
113        path: PathBuf,
114        message: String,
115    },
116    UniversalMacOsMismatch {
117        field: String,
118        arm64: String,
119        x64: String,
120    },
121    UniversalMacOsTool {
122        operation: &'static str,
123        path: PathBuf,
124        message: String,
125    },
126    DistributionTargetMismatch {
127        format: &'static str,
128        actual: String,
129    },
130    DistributionTool {
131        tool: &'static str,
132        operation: &'static str,
133        path: PathBuf,
134        message: String,
135    },
136    ParseNativeRuntimeManifest(serde_json::Error),
137    SerializeNativeRuntimeManifest(serde_json::Error),
138    InvalidNativeRuntimeManifest(String),
139    ParseFuiConfig(serde_json::Error),
140    InvalidFuiConfig(String),
141}
142
143impl fmt::Display for Error {
144    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
145        match self {
146            Self::ReadFile { path, source } => write!(formatter, "failed to read {}: {source}", path.display()),
147            Self::InvalidIconPath(path) => write!(formatter, "application icon {} must be an SVG or PNG source file", path.display()),
148            Self::DecodeIcon { path, message } => write!(formatter, "failed to decode icon {}: {message}", path.display()),
149            Self::InvalidIconDimensions { path, width, height } => write!(formatter, "icon {} must be square and non-empty, but is {width}x{height}", path.display()),
150            Self::PngIconTooSmall { path, width, height, minimum } => write!(formatter, "PNG icon {} is {width}x{height}; provide a square PNG of at least {minimum}x{minimum} or use SVG", path.display()),
151            Self::InvisibleIcon(path) => write!(formatter, "icon {} is fully transparent and would be invisible", path.display()),
152            Self::InvalidIconRasterSize(size) => write!(formatter, "icon raster size must be greater than zero, got {size}"),
153            Self::DuplicateIconRasterSize(size) => write!(formatter, "icon raster size {size} was requested more than once"),
154            Self::MissingIconRaster(size) => write!(formatter, "icon raster set is missing required {size}x{size} pixels"),
155            Self::InvalidIconRasterData { size, expected, actual } => write!(formatter, "icon raster {size}x{size} has {actual} RGBA bytes; expected {expected}"),
156            Self::InvalidIconName(name) => write!(formatter, "icon name {name:?} must use only ASCII letters, digits, dots, hyphens, or underscores"),
157            Self::EncodeIcon { format, message } => write!(formatter, "failed to encode {format} icon: {message}"),
158            Self::InvalidApplicationMetadata { field, message } => write!(formatter, "invalid application metadata {field}: {message}"),
159            Self::InvalidBundlePath { path, message } => write!(formatter, "invalid bundle path {}: {message}", path.display()),
160            Self::MissingBundleInput(path) => write!(formatter, "bundle input {} does not exist", path.display()),
161            Self::UnsupportedBundleInput(path) => write!(formatter, "bundle input {} must be a regular file and must not be a symbolic link", path.display()),
162            Self::DuplicateBundlePath(path) => write!(formatter, "bundle destination {} is assigned more than once", path.display()),
163            Self::SourceOutputOverlap { source, destination } => write!(formatter, "bundle input {} overlaps output {}; choose an output outside every input path", source.display(), destination.display()),
164            Self::BundleOutputExists(path) => write!(formatter, "bundle output {} already exists; enable overwrite explicitly to replace it", path.display()),
165            Self::BundleIo { operation, path, source } => write!(formatter, "failed to {operation} {}: {source}", path.display()),
166            Self::PublishBundle { from, to, source } => write!(formatter, "failed to publish staged bundle {} to {}: {source}", from.display(), to.display()),
167            Self::RollbackBundle { backup, destination, source } => write!(formatter, "failed to restore previous bundle {} to {} after publication failed: {source}", backup.display(), destination.display()),
168            Self::SerializePackageRecord(source) => write!(formatter, "failed to serialize native package record: {source}"),
169            Self::ParsePackageRecord { path, source } => write!(formatter, "failed to parse native package record {}: {source}", path.display()),
170            Self::UnsupportedPackageRecordSchema(version) => write!(formatter, "native package record schema {version} is unsupported; rebuild the package with this cargo-fui version"),
171            Self::PackageRecordArtifactMissing(path) => write!(formatter, "packaged artifact {} is missing; rebuild the package from verified runtime inputs", path.display()),
172            Self::PackageRecordLengthMismatch { path, expected, actual } => write!(formatter, "packaged artifact {} has {actual} bytes but the package record requires {expected}; rebuild the package", path.display()),
173            Self::PackageRecordChecksumMismatch { path, expected, actual } => write!(formatter, "packaged artifact {} has SHA-256 {actual} but the package record requires {expected}; rebuild the package from verified inputs", path.display()),
174            Self::InvalidArchiveName(path) => write!(formatter, "release archive name {} must be one UTF-8 file name with the extension required by its format", path.display()),
175            Self::ArchiveOutputExists(path) => write!(formatter, "release archive output {} already exists; enable overwrite explicitly to replace it", path.display()),
176            Self::ArchiveIo { operation, path, source } => write!(formatter, "failed to {operation} release archive artifact {}: {source}", path.display()),
177            Self::ArchiveEncoding { format, message } => write!(formatter, "failed to encode deterministic {format} release archive: {message}"),
178            Self::ArchiveDecoding { format, message } => write!(formatter, "failed to decode {format} release archive: {message}"),
179            Self::ArchiveChecksumMismatch { path, expected, actual } => write!(formatter, "release archive {} has SHA-256 {actual} but its checksum sidecar requires {expected}; download or rebuild the complete artifact set", path.display()),
180            Self::ArchivePackageRecordMismatch(path) => write!(formatter, "release archive package record {} does not match the record embedded in the extracted bundle", path.display()),
181            Self::UnsupportedArchiveEntry(path) => write!(formatter, "release archive entry {} has an unsupported type or unsafe link target", path.display()),
182            Self::InvalidUniversalMacOsSlice { path, message } => write!(formatter, "invalid universal macOS slice {}: {message}", path.display()),
183            Self::UniversalMacOsMismatch { field, arm64, x64 } => write!(formatter, "universal macOS slice mismatch for {field}: ARM64 has {arm64:?}, x64 has {x64:?}"),
184            Self::UniversalMacOsTool { operation, path, message } => write!(formatter, "failed to {operation} macOS Mach-O {}: {message}", path.display()),
185            Self::DistributionTargetMismatch { format, actual } => write!(formatter, "cannot create {format} from package target {actual}"),
186            Self::DistributionTool { tool, operation, path, message } => write!(formatter, "{tool} failed to {operation} {}: {message}", path.display()),
187            Self::ParseNativeRuntimeManifest(source) => write!(formatter, "failed to parse native runtime manifest: {source}"),
188            Self::SerializeNativeRuntimeManifest(source) => write!(formatter, "failed to serialize native runtime manifest: {source}"),
189            Self::InvalidNativeRuntimeManifest(message) => write!(formatter, "invalid native runtime manifest: {message}"),
190            Self::ParseFuiConfig(source) => write!(formatter, "failed to parse fui-config.json: {source}"),
191            Self::InvalidFuiConfig(message) => write!(formatter, "invalid fui-config.json: {message}"),
192        }
193    }
194}
195
196impl std::error::Error for Error {
197    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
198        match self {
199            Self::ReadFile { source, .. } => Some(source),
200            Self::BundleIo { source, .. }
201            | Self::ArchiveIo { source, .. }
202            | Self::PublishBundle { source, .. }
203            | Self::RollbackBundle { source, .. } => Some(source),
204            Self::SerializePackageRecord(source) => Some(source),
205            Self::ParsePackageRecord { source, .. } => Some(source),
206            Self::ParseNativeRuntimeManifest(source)
207            | Self::SerializeNativeRuntimeManifest(source)
208            | Self::ParseFuiConfig(source) => Some(source),
209            _ => None,
210        }
211    }
212}