1 // Take a look at the license at the top of the repository in the LICENSE file.
2 
3 use std::fmt::Debug;
4 use std::io;
5 use thiserror::Error;
6 
7 #[derive(Error, Debug, Clone, PartialEq, Copy, Eq)]
8 #[non_exhaustive]
9 #[doc(alias = "cairo_status_t")]
10 pub enum Error {
11     #[error("No Memory")]
12     #[doc(alias = "STATUS_NO_MEMORY")]
13     NoMemory,
14     #[error("Invalid Restore")]
15     #[doc(alias = "STATUS_INVALID_RESTORE")]
16     InvalidRestore,
17     #[error("Invalid Pop Group")]
18     #[doc(alias = "STATUS_INVALID_POP_GROUP")]
19     InvalidPopGroup,
20     #[error("No Current Point")]
21     #[doc(alias = "STATUS_NO_CURRENT_POINT")]
22     NoCurrentPoint,
23     #[error("Invalid Matrix")]
24     #[doc(alias = "STATUS_INVALID_MATRIX")]
25     InvalidMatrix,
26     #[error("Invalid Status")]
27     #[doc(alias = "STATUS_INVALID_STATUS")]
28     InvalidStatus,
29     #[error("Null Pointer")]
30     #[doc(alias = "STATUS_NULL_POINTER")]
31     NullPointer,
32     #[error("Invalid String")]
33     #[doc(alias = "STATUS_INVALID_STRING")]
34     InvalidString,
35     #[error("Invalid Path Data")]
36     #[doc(alias = "STATUS_INVALID_PATH_DATA")]
37     InvalidPathData,
38     #[error("Cairo : Read Error")]
39     #[doc(alias = "STATUS_READ_ERROR")]
40     ReadError,
41     #[error("Write Error")]
42     #[doc(alias = "STATUS_WRITE_ERROR")]
43     WriteError,
44     #[error("Surface Finished")]
45     #[doc(alias = "STATUS_SURFACE_FINISHED")]
46     SurfaceFinished,
47     #[error("Surface Type Mismatch")]
48     #[doc(alias = "STATUS_SURFACE_TYPE_MISMATCH")]
49     SurfaceTypeMismatch,
50     #[error("Pattern Type Mismatch")]
51     #[doc(alias = "STATUS_PATTERN_TYPE_MISMATCH")]
52     PatternTypeMismatch,
53     #[error("Invalid Content")]
54     #[doc(alias = "STATUS_INVALID_CONTENT")]
55     InvalidContent,
56     #[error("Invalid Format")]
57     #[doc(alias = "STATUS_INVALID_FORMAT")]
58     InvalidFormat,
59     #[error("Invalid Visual")]
60     #[doc(alias = "STATUS_INVALID_VISUAL")]
61     InvalidVisual,
62     #[error("File Not Found")]
63     #[doc(alias = "STATUS_FILE_NOT_FOUND")]
64     FileNotFound,
65     #[error("Invalid Dash")]
66     #[doc(alias = "STATUS_INVALID_DASH")]
67     InvalidDash,
68     #[error("Invalid Dash Comment")]
69     #[doc(alias = "STATUS_INVALID_DSC_COMMENT")]
70     InvalidDscComment,
71     #[error("Invalid Index")]
72     #[doc(alias = "STATUS_INVALID_INDEX")]
73     InvalidIndex,
74     #[error("Clip Not Representable")]
75     #[doc(alias = "STATUS_CLIP_NOT_REPRESENTABLE")]
76     ClipNotRepresentable,
77     #[error("Temp File Error")]
78     #[doc(alias = "STATUS_TEMP_FILE_ERROR")]
79     TempFileError,
80     #[error("Invalid Stride")]
81     #[doc(alias = "STATUS_INVALID_STRIDE")]
82     InvalidStride,
83     #[error("Font Type Mismatch")]
84     #[doc(alias = "STATUS_FONT_TYPE_MISMATCH")]
85     FontTypeMismatch,
86     #[error("User Font Immutable")]
87     #[doc(alias = "STATUS_USER_FONT_IMMUTABLE")]
88     UserFontImmutable,
89     #[error("User Font Error")]
90     #[doc(alias = "STATUS_USER_FONT_ERROR")]
91     UserFontError,
92     #[error("Negative Count")]
93     #[doc(alias = "STATUS_NEGATIVE_COUNT")]
94     NegativeCount,
95     #[error("Invalid Clusters")]
96     #[doc(alias = "STATUS_INVALID_CLUSTERS")]
97     InvalidClusters,
98     #[error("Invalid Slant")]
99     #[doc(alias = "STATUS_INVALID_SLANT")]
100     InvalidSlant,
101     #[error("Invalid Weight")]
102     #[doc(alias = "STATUS_INVALID_WEIGHT")]
103     InvalidWeight,
104     #[error("Invalid Size")]
105     #[doc(alias = "STATUS_INVALID_SIZE")]
106     InvalidSize,
107     #[error("User Font Not Implemented")]
108     #[doc(alias = "STATUS_USER_FONT_NOT_IMPLEMENTED")]
109     UserFontNotImplemented,
110     #[error("Device Type Mismatch")]
111     #[doc(alias = "STATUS_DEVICE_TYPE_MISMATCH")]
112     DeviceTypeMismatch,
113     #[error("Device Error")]
114     #[doc(alias = "STATUS_DEVICE_ERROR")]
115     DeviceError,
116     #[error("Invalid Mesh Construction")]
117     #[doc(alias = "STATUS_INVALID_MESH_CONSTRUCTION")]
118     InvalidMeshConstruction,
119     #[error("Device Finished")]
120     #[doc(alias = "STATUS_DEVICE_FINISHED")]
121     DeviceFinished,
122     #[error("JBig2Global Missing")]
123     #[doc(alias = "STATUS_J_BIG2_GLOBAL_MISSING")]
124     JBig2GlobalMissing,
125     #[error("PNG Error")]
126     #[doc(alias = "STATUS_PNG_ERROR")]
127     PngError,
128     #[error("Freetype Error")]
129     #[doc(alias = "STATUS_FREETYPE_ERROR")]
130     FreetypeError,
131     #[error("Win32Gdi Error")]
132     #[doc(alias = "STATUS_WIN32_GDI_ERROR")]
133     Win32GdiError,
134     #[error("LastStatus")]
135     #[doc(alias = "STATUS_LAST_STATUS")]
136     LastStatus,
137     #[error("Unknown {0}")]
138     #[doc(hidden)]
139     __Unknown(i32),
140 }
141 #[doc(hidden)]
142 impl From<Error> for ffi::cairo_status_t {
from(err: Error) -> ffi::cairo_status_t143     fn from(err: Error) -> ffi::cairo_status_t {
144         match err {
145             Error::NoMemory => ffi::STATUS_NO_MEMORY,
146             Error::InvalidRestore => ffi::STATUS_INVALID_RESTORE,
147             Error::InvalidPopGroup => ffi::STATUS_INVALID_POP_GROUP,
148             Error::NoCurrentPoint => ffi::STATUS_NO_CURRENT_POINT,
149             Error::InvalidMatrix => ffi::STATUS_INVALID_MATRIX,
150             Error::InvalidStatus => ffi::STATUS_INVALID_STATUS,
151             Error::NullPointer => ffi::STATUS_NULL_POINTER,
152             Error::InvalidString => ffi::STATUS_INVALID_STRING,
153             Error::InvalidPathData => ffi::STATUS_INVALID_PATH_DATA,
154             Error::ReadError => ffi::STATUS_READ_ERROR,
155             Error::WriteError => ffi::STATUS_WRITE_ERROR,
156             Error::SurfaceFinished => ffi::STATUS_SURFACE_FINISHED,
157             Error::SurfaceTypeMismatch => ffi::STATUS_SURFACE_TYPE_MISMATCH,
158             Error::PatternTypeMismatch => ffi::STATUS_PATTERN_TYPE_MISMATCH,
159             Error::InvalidContent => ffi::STATUS_INVALID_CONTENT,
160             Error::InvalidFormat => ffi::STATUS_INVALID_FORMAT,
161             Error::InvalidVisual => ffi::STATUS_INVALID_VISUAL,
162             Error::FileNotFound => ffi::STATUS_FILE_NOT_FOUND,
163             Error::InvalidDash => ffi::STATUS_INVALID_DASH,
164             Error::InvalidDscComment => ffi::STATUS_INVALID_DSC_COMMENT,
165             Error::InvalidIndex => ffi::STATUS_INVALID_INDEX,
166             Error::ClipNotRepresentable => ffi::STATUS_CLIP_NOT_REPRESENTABLE,
167             Error::TempFileError => ffi::STATUS_TEMP_FILE_ERROR,
168             Error::InvalidStride => ffi::STATUS_INVALID_STRIDE,
169             Error::FontTypeMismatch => ffi::STATUS_FONT_TYPE_MISMATCH,
170             Error::UserFontImmutable => ffi::STATUS_USER_FONT_IMMUTABLE,
171             Error::UserFontError => ffi::STATUS_USER_FONT_ERROR,
172             Error::NegativeCount => ffi::STATUS_NEGATIVE_COUNT,
173             Error::InvalidClusters => ffi::STATUS_INVALID_CLUSTERS,
174             Error::InvalidSlant => ffi::STATUS_INVALID_SLANT,
175             Error::InvalidWeight => ffi::STATUS_INVALID_WEIGHT,
176             Error::InvalidSize => ffi::STATUS_INVALID_SIZE,
177             Error::UserFontNotImplemented => ffi::STATUS_USER_FONT_NOT_IMPLEMENTED,
178             Error::DeviceTypeMismatch => ffi::STATUS_DEVICE_TYPE_MISMATCH,
179             Error::DeviceError => ffi::STATUS_DEVICE_ERROR,
180             Error::InvalidMeshConstruction => ffi::STATUS_INVALID_MESH_CONSTRUCTION,
181             Error::DeviceFinished => ffi::STATUS_DEVICE_FINISHED,
182             Error::JBig2GlobalMissing => ffi::STATUS_J_BIG2_GLOBAL_MISSING,
183             Error::PngError => ffi::STATUS_PNG_ERROR,
184             Error::FreetypeError => ffi::STATUS_FREETYPE_ERROR,
185             Error::Win32GdiError => ffi::STATUS_WIN32_GDI_ERROR,
186             Error::LastStatus => ffi::STATUS_LAST_STATUS,
187             Error::__Unknown(value) => value,
188         }
189     }
190 }
191 
192 #[doc(hidden)]
193 impl From<ffi::cairo_status_t> for Error {
from(value: ffi::cairo_status_t) -> Self194     fn from(value: ffi::cairo_status_t) -> Self {
195         match value {
196             ffi::STATUS_NO_MEMORY => Self::NoMemory,
197             ffi::STATUS_INVALID_RESTORE => Self::InvalidRestore,
198             ffi::STATUS_INVALID_POP_GROUP => Self::InvalidPopGroup,
199             ffi::STATUS_NO_CURRENT_POINT => Self::NoCurrentPoint,
200             ffi::STATUS_INVALID_MATRIX => Self::InvalidMatrix,
201             ffi::STATUS_INVALID_STATUS => Self::InvalidStatus,
202             ffi::STATUS_NULL_POINTER => Self::NullPointer,
203             ffi::STATUS_INVALID_STRING => Self::InvalidString,
204             ffi::STATUS_INVALID_PATH_DATA => Self::InvalidPathData,
205             ffi::STATUS_READ_ERROR => Self::ReadError,
206             ffi::STATUS_WRITE_ERROR => Self::WriteError,
207             ffi::STATUS_SURFACE_FINISHED => Self::SurfaceFinished,
208             ffi::STATUS_SURFACE_TYPE_MISMATCH => Self::SurfaceTypeMismatch,
209             ffi::STATUS_PATTERN_TYPE_MISMATCH => Self::PatternTypeMismatch,
210             ffi::STATUS_INVALID_CONTENT => Self::InvalidContent,
211             ffi::STATUS_INVALID_FORMAT => Self::InvalidFormat,
212             ffi::STATUS_INVALID_VISUAL => Self::InvalidVisual,
213             ffi::STATUS_FILE_NOT_FOUND => Self::FileNotFound,
214             ffi::STATUS_INVALID_DASH => Self::InvalidDash,
215             ffi::STATUS_INVALID_DSC_COMMENT => Self::InvalidDscComment,
216             ffi::STATUS_INVALID_INDEX => Self::InvalidIndex,
217             ffi::STATUS_CLIP_NOT_REPRESENTABLE => Self::ClipNotRepresentable,
218             ffi::STATUS_TEMP_FILE_ERROR => Self::TempFileError,
219             ffi::STATUS_INVALID_STRIDE => Self::InvalidStride,
220             ffi::STATUS_FONT_TYPE_MISMATCH => Self::FontTypeMismatch,
221             ffi::STATUS_USER_FONT_IMMUTABLE => Self::UserFontImmutable,
222             ffi::STATUS_USER_FONT_ERROR => Self::UserFontError,
223             ffi::STATUS_NEGATIVE_COUNT => Self::NegativeCount,
224             ffi::STATUS_INVALID_CLUSTERS => Self::InvalidClusters,
225             ffi::STATUS_INVALID_SLANT => Self::InvalidSlant,
226             ffi::STATUS_INVALID_WEIGHT => Self::InvalidWeight,
227             ffi::STATUS_INVALID_SIZE => Self::InvalidSize,
228             ffi::STATUS_USER_FONT_NOT_IMPLEMENTED => Self::UserFontNotImplemented,
229             ffi::STATUS_DEVICE_TYPE_MISMATCH => Self::DeviceTypeMismatch,
230             ffi::STATUS_DEVICE_ERROR => Self::DeviceError,
231             ffi::STATUS_INVALID_MESH_CONSTRUCTION => Self::InvalidMeshConstruction,
232             ffi::STATUS_DEVICE_FINISHED => Self::DeviceFinished,
233             ffi::STATUS_J_BIG2_GLOBAL_MISSING => Self::JBig2GlobalMissing,
234             ffi::STATUS_PNG_ERROR => Self::PngError,
235             ffi::STATUS_FREETYPE_ERROR => Self::FreetypeError,
236             ffi::STATUS_WIN32_GDI_ERROR => Self::Win32GdiError,
237             ffi::STATUS_LAST_STATUS => Self::LastStatus,
238             value => Self::__Unknown(value),
239         }
240     }
241 }
242 
243 #[derive(Error, Debug)]
244 pub enum IoError {
245     #[error("Cairo error: {0}")]
246     Cairo(#[from] Error),
247     #[error("IO error: {0}")]
248     Io(#[from] io::Error),
249 }
250 
251 #[derive(Error, Debug)]
252 pub enum BorrowError {
253     #[error("Failed to borrow with Cairo error: {0}")]
254     Cairo(#[from] crate::Error),
255     #[error("Can't get exclusive access")]
256     NonExclusive,
257 }
258 
259 pub type Result<T> = std::result::Result<T, Error>;
260