Skip to content

Commit

Permalink
Remove thiserror dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Aug 5, 2024
1 parent dce2d63 commit a7f1811
Show file tree
Hide file tree
Showing 12 changed files with 399 additions and 147 deletions.
23 changes: 0 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ rust-version = "1.70"
[workspace.dependencies]
libc = "0.2"
bitflags = "2.6"
thiserror = "1"
gir-format-check = "^0.1"
glib-sys = { path = "glib/sys", version = "0.21" }
gobject-sys = { path = "glib/gobject-sys", version = "0.21" }
Expand Down
1 change: 0 additions & 1 deletion cairo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ workspace = true
cairo-sys-rs.workspace = true
libc.workspace = true
bitflags.workspace = true
thiserror.workspace = true
freetype-rs = { version = "0.37", optional = true }

[dev-dependencies]
Expand Down
182 changes: 121 additions & 61 deletions cairo/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,157 +1,167 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use std::{fmt::Debug, io};

use crate::ffi;
use thiserror::Error;
use std::{fmt, fmt::Debug, io};

#[derive(Error, Debug, Clone, PartialEq, Copy, Eq)]
#[derive(Debug, Clone, PartialEq, Copy, Eq)]
#[non_exhaustive]
#[doc(alias = "cairo_status_t")]
pub enum Error {
#[error("No Memory")]
#[doc(alias = "STATUS_NO_MEMORY")]
NoMemory,
#[error("Invalid Restore")]
#[doc(alias = "STATUS_INVALID_RESTORE")]
InvalidRestore,
#[error("Invalid Pop Group")]
#[doc(alias = "STATUS_INVALID_POP_GROUP")]
InvalidPopGroup,
#[error("No Current Point")]
#[doc(alias = "STATUS_NO_CURRENT_POINT")]
NoCurrentPoint,
#[error("Invalid Matrix")]
#[doc(alias = "STATUS_INVALID_MATRIX")]
InvalidMatrix,
#[error("Invalid Status")]
#[doc(alias = "STATUS_INVALID_STATUS")]
InvalidStatus,
#[error("Null Pointer")]
#[doc(alias = "STATUS_NULL_POINTER")]
NullPointer,
#[error("Invalid String")]
#[doc(alias = "STATUS_INVALID_STRING")]
InvalidString,
#[error("Invalid Path Data")]
#[doc(alias = "STATUS_INVALID_PATH_DATA")]
InvalidPathData,
#[error("Cairo : Read Error")]
#[doc(alias = "STATUS_READ_ERROR")]
ReadError,
#[error("Write Error")]
#[doc(alias = "STATUS_WRITE_ERROR")]
WriteError,
#[error("Surface Finished")]
#[doc(alias = "STATUS_SURFACE_FINISHED")]
SurfaceFinished,
#[error("Surface Type Mismatch")]
#[doc(alias = "STATUS_SURFACE_TYPE_MISMATCH")]
SurfaceTypeMismatch,
#[error("Pattern Type Mismatch")]
#[doc(alias = "STATUS_PATTERN_TYPE_MISMATCH")]
PatternTypeMismatch,
#[error("Invalid Content")]
#[doc(alias = "STATUS_INVALID_CONTENT")]
InvalidContent,
#[error("Invalid Format")]
#[doc(alias = "STATUS_INVALID_FORMAT")]
InvalidFormat,
#[error("Invalid Visual")]
#[doc(alias = "STATUS_INVALID_VISUAL")]
InvalidVisual,
#[error("File Not Found")]
#[doc(alias = "STATUS_FILE_NOT_FOUND")]
FileNotFound,
#[error("Invalid Dash")]
#[doc(alias = "STATUS_INVALID_DASH")]
InvalidDash,
#[error("Invalid Dash Comment")]
#[doc(alias = "STATUS_INVALID_DSC_COMMENT")]
InvalidDscComment,
#[error("Invalid Index")]
#[doc(alias = "STATUS_INVALID_INDEX")]
InvalidIndex,
#[error("Clip Not Representable")]
#[doc(alias = "STATUS_CLIP_NOT_REPRESENTABLE")]
ClipNotRepresentable,
#[error("Temp File Error")]
#[doc(alias = "STATUS_TEMP_FILE_ERROR")]
TempFileError,
#[error("Invalid Stride")]
#[doc(alias = "STATUS_INVALID_STRIDE")]
InvalidStride,
#[error("Font Type Mismatch")]
#[doc(alias = "STATUS_FONT_TYPE_MISMATCH")]
FontTypeMismatch,
#[error("User Font Immutable")]
#[doc(alias = "STATUS_USER_FONT_IMMUTABLE")]
UserFontImmutable,
#[error("User Font Error")]
#[doc(alias = "STATUS_USER_FONT_ERROR")]
UserFontError,
#[error("Negative Count")]
#[doc(alias = "STATUS_NEGATIVE_COUNT")]
NegativeCount,
#[error("Invalid Clusters")]
#[doc(alias = "STATUS_INVALID_CLUSTERS")]
InvalidClusters,
#[error("Invalid Slant")]
#[doc(alias = "STATUS_INVALID_SLANT")]
InvalidSlant,
#[error("Invalid Weight")]
#[doc(alias = "STATUS_INVALID_WEIGHT")]
InvalidWeight,
#[error("Invalid Size")]
#[doc(alias = "STATUS_INVALID_SIZE")]
InvalidSize,
#[error("User Font Not Implemented")]
#[doc(alias = "STATUS_USER_FONT_NOT_IMPLEMENTED")]
UserFontNotImplemented,
#[error("Device Type Mismatch")]
#[doc(alias = "STATUS_DEVICE_TYPE_MISMATCH")]
DeviceTypeMismatch,
#[error("Device Error")]
#[doc(alias = "STATUS_DEVICE_ERROR")]
DeviceError,
#[error("Invalid Mesh Construction")]
#[doc(alias = "STATUS_INVALID_MESH_CONSTRUCTION")]
InvalidMeshConstruction,
#[error("Device Finished")]
#[doc(alias = "STATUS_DEVICE_FINISHED")]
DeviceFinished,
#[error("JBig2Global Missing")]
#[doc(alias = "STATUS_J_BIG2_GLOBAL_MISSING")]
JBig2GlobalMissing,
#[error("PNG Error")]
#[doc(alias = "STATUS_PNG_ERROR")]
PngError,
#[error("Freetype Error")]
#[doc(alias = "STATUS_FREETYPE_ERROR")]
FreetypeError,
#[error("Win32Gdi Error")]
#[doc(alias = "STATUS_WIN32_GDI_ERROR")]
Win32GdiError,
#[cfg(feature = "v1_16")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
#[error("Tag Error")]
#[doc(alias = "STATUS_TAG_ERROR")]
TagError,
#[cfg(feature = "v1_18")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
#[error("Dwrite Error")]
#[doc(alias = "STATUS_DWRITE_ERROR")]
DwriteError,
#[error("LastStatus")]
#[doc(alias = "STATUS_LAST_STATUS")]
LastStatus,
#[error("Unknown {0}")]
#[doc(hidden)]
__Unknown(i32),
}

impl std::error::Error for Error {}

impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::NoMemory => fmt.write_str("No Memory"),
Error::InvalidRestore => fmt.write_str("Invalid Restore"),
Error::InvalidPopGroup => fmt.write_str("Invalid Pop Group"),
Error::NoCurrentPoint => fmt.write_str("No Current Point"),
Error::InvalidMatrix => fmt.write_str("Invalid Matrix"),
Error::InvalidStatus => fmt.write_str("Invalid Status"),
Error::NullPointer => fmt.write_str("Null Pointer"),
Error::InvalidString => fmt.write_str("Invalid String"),
Error::InvalidPathData => fmt.write_str("Invalid Path Data"),
Error::ReadError => fmt.write_str("Cairo : Read Error"),
Error::WriteError => fmt.write_str("Write Error"),
Error::SurfaceFinished => fmt.write_str("Surface Finished"),
Error::SurfaceTypeMismatch => fmt.write_str("Surface Type Mismatch"),
Error::PatternTypeMismatch => fmt.write_str("Pattern Type Mismatch"),
Error::InvalidContent => fmt.write_str("Invalid Content"),
Error::InvalidFormat => fmt.write_str("Invalid Format"),
Error::InvalidVisual => fmt.write_str("Invalid Visual"),
Error::FileNotFound => fmt.write_str("File Not Found"),
Error::InvalidDash => fmt.write_str("Invalid Dash"),
Error::InvalidDscComment => fmt.write_str("Invalid Dash Comment"),
Error::InvalidIndex => fmt.write_str("Invalid Index"),
Error::ClipNotRepresentable => fmt.write_str("Clip Not Representable"),
Error::TempFileError => fmt.write_str("Temp File Error"),
Error::InvalidStride => fmt.write_str("Invalid Stride"),
Error::FontTypeMismatch => fmt.write_str("Font Type Mismatch"),
Error::UserFontImmutable => fmt.write_str("User Font Immutable"),
Error::UserFontError => fmt.write_str("User Font Error"),
Error::NegativeCount => fmt.write_str("Negative Count"),
Error::InvalidClusters => fmt.write_str("Invalid Clusters"),
Error::InvalidSlant => fmt.write_str("Invalid Slant"),
Error::InvalidWeight => fmt.write_str("Invalid Weight"),
Error::InvalidSize => fmt.write_str("Invalid Size"),
Error::UserFontNotImplemented => fmt.write_str("User Font Not Implemented"),
Error::DeviceTypeMismatch => fmt.write_str("Device Type Mismatch"),
Error::DeviceError => fmt.write_str("Device Error"),
Error::InvalidMeshConstruction => fmt.write_str("Invalid Mesh Construction"),
Error::DeviceFinished => fmt.write_str("Device Finished"),
Error::JBig2GlobalMissing => fmt.write_str("JBig2Global Missing"),
Error::PngError => fmt.write_str("PNG Error"),
Error::FreetypeError => fmt.write_str("Freetype Error"),
Error::Win32GdiError => fmt.write_str("Win32Gdi Error"),
#[cfg(feature = "v1_16")]
Error::TagError => fmt.write_str("Tag error"),
#[cfg(feature = "v1_18")]
Error::DwriteError => fmt.write_str("Dwrite error"),
Error::LastStatus => fmt.write_str("LastStatus"),
Error::__Unknown(value) => write!(fmt, "Unknown {value}"),
}
}
}

#[doc(hidden)]
impl From<Error> for ffi::cairo_status_t {
fn from(err: Error) -> ffi::cairo_status_t {
fn from(err: Error) -> Self {
match err {
Error::NoMemory => ffi::STATUS_NO_MEMORY,
Error::InvalidRestore => ffi::STATUS_INVALID_RESTORE,
Expand Down Expand Up @@ -250,29 +260,79 @@ impl From<ffi::cairo_status_t> for Error {
ffi::STATUS_FREETYPE_ERROR => Self::FreetypeError,
ffi::STATUS_WIN32_GDI_ERROR => Self::Win32GdiError,
#[cfg(feature = "v1_16")]
ffi::STATUS_TAG_ERROR => Self::TagError,
ffi::STATUS_TAG_ERROR => Error::TagError,
#[cfg(feature = "v1_18")]
ffi::STATUS_DWRITE_ERROR => Self::DwriteError,
ffi::STATUS_DWRITE_ERROR => Error::DwriteError,
ffi::STATUS_LAST_STATUS => Self::LastStatus,
value => Self::__Unknown(value),
}
}
}

#[derive(Error, Debug)]
#[derive(Debug)]
pub enum IoError {
#[error("Cairo error: {0}")]
Cairo(#[from] Error),
#[error("IO error: {0}")]
Io(#[from] io::Error),
Cairo(Error),
Io(io::Error),
}

impl std::error::Error for IoError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
IoError::Cairo(err) => Some(err),
IoError::Io(err) => Some(err),
}
}
}

impl fmt::Display for IoError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> ::core::fmt::Result {
match self {
IoError::Cairo(err) => write!(fmt, "Cairo error: {err}"),
IoError::Io(err) => write!(fmt, "IO error: {err}"),
}
}
}

#[derive(Error, Debug)]
impl std::convert::From<Error> for IoError {
fn from(source: Error) -> Self {
IoError::Cairo(source)
}
}

impl std::convert::From<io::Error> for IoError {
fn from(source: io::Error) -> Self {
IoError::Io(source)
}
}

#[derive(Debug)]
pub enum BorrowError {
#[error("Failed to borrow with Cairo error: {0}")]
Cairo(#[from] crate::Error),
#[error("Can't get exclusive access")]
Cairo(crate::Error),
NonExclusive,
}

impl std::error::Error for BorrowError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
BorrowError::Cairo(err) => Some(err),
BorrowError::NonExclusive => None,
}
}
}

impl fmt::Display for BorrowError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
BorrowError::Cairo(err) => write!(fmt, "Failed to borrow with Cairo error: {err}"),
BorrowError::NonExclusive => fmt.write_str("Can't get exclusive access"),
}
}
}

impl std::convert::From<crate::Error> for BorrowError {
fn from(err: crate::Error) -> Self {
BorrowError::Cairo(err)
}
}

pub type Result<T> = std::result::Result<T, Error>;
1 change: 0 additions & 1 deletion gio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ futures-io = "0.3"
futures-util = { version = "0.3", default-features = false }
gio-sys.workspace = true
glib.workspace = true
thiserror.workspace = true
pin-project-lite = "0.2"
smallvec = "1"

Expand Down
Loading

0 comments on commit a7f1811

Please sign in to comment.