Skip to main content

shape_viz_core/
error.rs

1//! Error types for FChart Core
2
3use thiserror::Error;
4
5/// Main error type for FChart operations
6#[derive(Error, Debug)]
7pub enum ChartError {
8    /// GPU/Rendering related errors
9    #[error("GPU error: {0}")]
10    Gpu(#[from] wgpu::RequestDeviceError),
11
12    #[error("GPU surface error: {0}")]
13    Surface(#[from] wgpu::SurfaceError),
14
15    #[error("GPU device lost")]
16    DeviceLost,
17
18    #[error("GPU out of memory")]
19    OutOfMemory,
20
21    /// Data related errors
22    #[error("Invalid data: {0}")]
23    InvalidData(String),
24
25    #[error("Empty dataset")]
26    EmptyData,
27
28    #[error("Data range error: {message}")]
29    DataRange { message: String },
30
31    /// Rendering errors
32    #[error("Shader compilation failed: {0}")]
33    ShaderCompilation(String),
34
35    #[error("Texture creation failed: {0}")]
36    TextureCreation(String),
37
38    #[error("Buffer creation failed: {0}")]
39    BufferCreation(String),
40
41    /// Configuration errors
42    #[error("Invalid configuration: {0}")]
43    InvalidConfig(String),
44
45    #[error("Unsupported feature: {0}")]
46    UnsupportedFeature(String),
47
48    /// Layer system errors
49    #[error("Layer error: {0}")]
50    Layer(String),
51
52    #[error("Layer not found: {0}")]
53    LayerNotFound(String),
54
55    /// Text rendering errors
56    #[error("Text rendering error: {0}")]
57    TextRendering(String),
58
59    #[error("Font loading error: {0}")]
60    FontLoading(String),
61
62    /// I/O and format errors
63    #[error("I/O error: {0}")]
64    Io(#[from] std::io::Error),
65
66    #[error("Serialization error: {0}")]
67    Serialization(#[from] serde_json::Error),
68
69    /// Generic errors
70    #[error("Internal error: {0}")]
71    Internal(String),
72
73    #[error("Not implemented: {0}")]
74    NotImplemented(String),
75}
76
77/// Convenience type alias for Results with ChartError
78pub type Result<T> = std::result::Result<T, ChartError>;
79
80impl ChartError {
81    /// Create a new data range error
82    pub fn data_range(message: impl Into<String>) -> Self {
83        Self::DataRange {
84            message: message.into(),
85        }
86    }
87
88    /// Create a new invalid configuration error
89    pub fn invalid_config(message: impl Into<String>) -> Self {
90        Self::InvalidConfig(message.into())
91    }
92
93    /// Create a new layer error
94    pub fn layer(message: impl Into<String>) -> Self {
95        Self::Layer(message.into())
96    }
97
98    /// Create a new internal error
99    pub fn internal(message: impl Into<String>) -> Self {
100        Self::Internal(message.into())
101    }
102}
103
104// GPU-specific error conversions
105impl From<wgpu::BufferAsyncError> for ChartError {
106    fn from(_: wgpu::BufferAsyncError) -> Self {
107        Self::internal("GPU buffer operation failed")
108    }
109}
110
111// Text rendering error conversion is handled conditionally at compile time
112// #[cfg(feature = "text-rendering")]
113// impl From<cosmic_text::Error> for ChartError {
114//     fn from(err: cosmic_text::Error) -> Self {
115//         Self::TextRendering(err.to_string())
116//     }
117// }