xue/encode/errors.rs
1//! The pipeline's user-actionable error type.
2//!
3//! Mirrors `xuebuild/errors.py`: everything a user can fix (a bad GRIB file, a
4//! missing record, an axis that is not on the model's cadence) is an
5//! `EncodeError`, which the CLI turns into `error: …` and exit code 2.
6//! Anything else is a bug and panics or bubbles up as `anyhow::Error`.
7
8use std::fmt;
9
10/// One message, whatever stage raised it: nothing matches on the kind, and
11/// the CLI prints every one the same way. The constructors name the stage
12/// at the call site.
13#[derive(Debug)]
14pub struct EncodeError(pub String);
15
16impl EncodeError {
17 /// A GRIB/NetCDF file or a conversion step was invalid.
18 pub fn conversion(message: impl Into<String>) -> Self {
19 Self(message.into())
20 }
21
22 /// The public manifest violates the versioned contract.
23 pub fn manifest(message: impl Into<String>) -> Self {
24 Self(message.into())
25 }
26
27 /// A Xue bundle or store violates the versioned contract.
28 pub fn bundle(message: impl Into<String>) -> Self {
29 Self(message.into())
30 }
31}
32
33impl fmt::Display for EncodeError {
34 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
35 formatter.write_str(&self.0)
36 }
37}
38
39impl std::error::Error for EncodeError {}
40
41pub type Result<T> = std::result::Result<T, EncodeError>;