ad_tensors_rs/
error.rs

1use thiserror::Error;
2
3/// Crate-wide error type for API skeleton operations.
4///
5/// # Examples
6///
7/// ```rust
8/// use ad_tensors_rs::{Error, Result};
9///
10/// fn maybe_fail(flag: bool) -> Result<()> {
11///     if flag {
12///         Ok(())
13///     } else {
14///         Err(Error::InvalidAdTensor {
15///             message: "demo".into(),
16///         })
17///     }
18/// }
19///
20/// assert!(maybe_fail(true).is_ok());
21/// assert!(maybe_fail(false).is_err());
22/// ```
23#[derive(Debug, Error)]
24pub enum Error {
25    /// The runtime used by builder `.run()` was not configured.
26    #[error("default runtime is not configured; call `set_default_runtime(...)` first")]
27    RuntimeNotConfigured,
28
29    /// A required thread-local global context is missing.
30    #[error("missing global context for type `{type_name}`")]
31    MissingGlobalContext { type_name: &'static str },
32
33    /// Stored context could not be downcast to the expected type.
34    #[error("global context type mismatch: expected `{expected}`")]
35    ContextTypeMismatch { expected: &'static str },
36
37    /// Wrapper for backend/linalg/einsum errors from tenferro crates.
38    #[error(transparent)]
39    Backend(#[from] tenferro_device::Error),
40
41    /// Wrapper for AD-rule level errors from `chainrules-core`.
42    #[error(transparent)]
43    Autodiff(#[from] chainrules_core::AutodiffError),
44
45    /// AD tensor operands are structurally invalid for the requested operation.
46    #[error("invalid AD tensor operands: {message}")]
47    InvalidAdTensor { message: String },
48
49    /// Reverse-mode operands belong to different tapes.
50    #[error("reverse-mode operands must share one tape: expected {expected}, found {found}")]
51    MixedReverseTape { expected: u64, found: u64 },
52
53    /// Operation is not available for the currently selected runtime.
54    #[error("operation `{op}` is not supported on runtime `{runtime}`")]
55    UnsupportedRuntimeOp {
56        op: &'static str,
57        runtime: &'static str,
58    },
59
60    /// AD operation is not available for the requested mode.
61    #[error("AD operation `{op}` is not supported for the provided inputs")]
62    UnsupportedAdOp { op: &'static str },
63}
64
65/// Convenience result alias.
66///
67/// # Examples
68///
69/// ```rust
70/// use ad_tensors_rs::{Error, Result};
71///
72/// let ok: Result<i32> = Ok(1);
73/// let err: Result<i32> = Err(Error::InvalidAdTensor {
74///     message: "sample".into(),
75/// });
76///
77/// assert_eq!(ok.unwrap(), 1);
78/// assert!(err.is_err());
79/// ```
80pub type Result<T> = std::result::Result<T, Error>;