refloxide_core/error.rs
1//! Error type and result alias for the refloxide kernel.
2//!
3//! All fallible operations in the public Rust API return [`Result<T>`].
4//! [`RefloxideError`] carries enough context for callers to recover or to
5//! surface a typed Python exception in the bindings crate.
6
7use thiserror::Error;
8
9fn format_energy_index(energy_index: &Option<usize>) -> String {
10 match energy_index {
11 Some(index) => format!(", energy-index {index}"),
12 None => String::new(),
13 }
14}
15
16/// Errors returned by the refloxide kernel.
17#[derive(Debug, Error, Clone)]
18pub enum RefloxideError {
19 /// Layer rows and tensor rows do not agree on the number of slabs.
20 #[error("layer count mismatch: layers has {layers} rows, tensor has {tensor} rows")]
21 LayerCountMismatch {
22 /// Number of rows seen in the layers buffer.
23 layers: usize,
24 /// Number of slabs seen in the tensor buffer.
25 tensor: usize,
26 },
27
28 /// The stack does not carry both a fronting and a backing row.
29 #[error("stack requires at least 2 layers (fronting + backing), got {0}")]
30 InsufficientLayers(usize),
31
32 /// Photon energy is not strictly positive.
33 #[error("invalid photon energy {0} eV (must be positive)")]
34 InvalidEnergy(f64),
35
36 /// Dynamic matrix could not be inverted at a particular layer and q-point.
37 #[error("dynamic matrix is singular at layer {layer}, q-index {q_index}{}", format_energy_index(.energy_index))]
38 SingularDynamicMatrix {
39 /// Index of the offending slab.
40 layer: usize,
41 /// Index in the q-vector at which the inversion failed.
42 q_index: usize,
43 /// Batch energy index when applicable.
44 energy_index: Option<usize>,
45 },
46
47 /// An input array did not have the expected shape at the FFI boundary.
48 #[error("invalid input shape: {0}")]
49 InvalidShape(String),
50
51 /// A peak parameter is outside its valid domain: non-finite; Gaussian `width <= 0`;
52 /// Lorentzian `gamma <= 0`; Voigt `sigma <= 0` or `gamma < 0`; pseudo-Voigt
53 /// `fwhm <= 0` or `eta` outside `[0, 1]`.
54 #[error("invalid peak parameter: {0}")]
55 InvalidParameter(String),
56
57 /// The GPU backend could not acquire a device or failed during dispatch.
58 #[error("gpu backend error: {0}")]
59 Gpu(String),
60}
61
62/// Convenience alias used throughout the crate.
63pub type Result<T> = std::result::Result<T, RefloxideError>;