Skip to main content

feagi_npu_runtime/traits/
error.rs

1// Copyright 2025 Neuraville Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Error types for runtime operations
5
6use core::fmt;
7
8#[cfg(feature = "std")]
9extern crate std;
10
11#[cfg(feature = "std")]
12use std::string::String;
13
14/// Runtime errors
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub enum RuntimeError {
17    /// Capacity exceeded
18    CapacityExceeded {
19        /// Requested capacity
20        requested: usize,
21        /// Available capacity
22        available: usize,
23    },
24
25    /// Out of memory
26    OutOfMemory {
27        /// Requested bytes
28        requested_bytes: usize,
29    },
30
31    /// Invalid parameters provided
32    #[cfg(feature = "std")]
33    InvalidParameters(String),
34
35    /// Invalid operation (only available with std feature)
36    #[cfg(feature = "std")]
37    InvalidOperation(String),
38
39    /// Platform not supported (only available with std feature)
40    #[cfg(feature = "std")]
41    PlatformNotSupported(String),
42
43    /// Storage error (only available with std feature)
44    #[cfg(feature = "std")]
45    StorageError(String),
46
47    /// Generic error (for no_std environments)
48    #[cfg(not(feature = "std"))]
49    GenericError,
50}
51
52impl fmt::Display for RuntimeError {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        match self {
55            RuntimeError::CapacityExceeded {
56                requested,
57                available,
58            } => {
59                write!(
60                    f,
61                    "Capacity exceeded: requested {}, available {}",
62                    requested, available
63                )
64            }
65            RuntimeError::OutOfMemory { requested_bytes } => {
66                write!(f, "Out of memory: requested {} bytes", requested_bytes)
67            }
68            #[cfg(feature = "std")]
69            RuntimeError::InvalidParameters(msg) => {
70                write!(f, "Invalid parameters: {}", msg)
71            }
72            #[cfg(feature = "std")]
73            RuntimeError::InvalidOperation(msg) => {
74                write!(f, "Invalid operation: {}", msg)
75            }
76            #[cfg(feature = "std")]
77            RuntimeError::PlatformNotSupported(platform) => {
78                write!(f, "Platform not supported: {}", platform)
79            }
80            #[cfg(feature = "std")]
81            RuntimeError::StorageError(msg) => {
82                write!(f, "Storage error: {}", msg)
83            }
84            #[cfg(not(feature = "std"))]
85            RuntimeError::GenericError => {
86                write!(f, "Runtime error")
87            }
88        }
89    }
90}
91
92#[cfg(feature = "std")]
93impl std::error::Error for RuntimeError {}
94
95/// Result type for runtime operations
96pub type Result<T> = core::result::Result<T, RuntimeError>;