Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement public Error types using thiserror #892

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions nannou/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ rusttype = { version = "0.8", features = ["gpu_cache"] }
serde = "1"
serde_derive = "1"
serde_json = "1"
thiserror = "1"
toml = "0.5"
walkdir = "2"
web-sys = { version = "0.3.55", optional = true }
Expand Down
4 changes: 3 additions & 1 deletion nannou/src/draw/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::collections::HashMap;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::ops::{Deref, DerefMut};
use thiserror::Error;
use wgpu::util::{BufferInitDescriptor, DeviceExt};

/// Draw API primitives that may be rendered via the **Renderer** type.
Expand Down Expand Up @@ -134,7 +135,8 @@ pub struct Scissor {
height: u32,
}

#[derive(Debug)]
#[derive(Debug, Error)]
#[error("error rendering drawing")]
pub struct DrawError;

#[repr(C)]
Expand Down
40 changes: 6 additions & 34 deletions nannou/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,25 @@

use serde;
use serde_json;
use std::error::Error;
use std::io::{Read, Write};
use std::path::Path;
use std::{fmt, fs, io};
use std::{fs, io};
use thiserror::Error;
use toml;

/// Errors that might occur when saving a file.
#[derive(Debug)]
#[derive(Debug, Error)]
pub enum FileError<E> {
Io(io::Error),
#[error(transparent)]
Io(#[from] io::Error),
#[error(transparent)]
Format(E),
}

pub type JsonFileError = FileError<serde_json::Error>;
pub type TomlFileSaveError = FileError<toml::ser::Error>;
pub type TomlFileLoadError = FileError<toml::de::Error>;

impl<E> From<io::Error> for FileError<E> {
fn from(err: io::Error) -> Self {
FileError::Io(err)
}
}

impl From<serde_json::Error> for JsonFileError {
fn from(err: serde_json::Error) -> Self {
FileError::Format(err)
Expand All @@ -44,30 +40,6 @@ impl From<toml::de::Error> for TomlFileLoadError {
}
}

impl<E> Error for FileError<E>
where
E: Error,
{
fn cause(&self) -> Option<&dyn Error> {
match *self {
FileError::Io(ref err) => Some(err),
FileError::Format(ref err) => Some(err),
}
}
}

impl<E> fmt::Display for FileError<E>
where
E: Error,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
FileError::Io(ref err) => fmt::Display::fmt(err, f),
FileError::Format(ref err) => fmt::Display::fmt(err, f),
}
}
}

/// Safely save a file with the given contents, replacing the original if necessary.
///
/// Works as follows:
Expand Down
33 changes: 6 additions & 27 deletions nannou/src/text/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::text::{Font, FontCollection};
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::path::{Path, PathBuf};
use thiserror::Error;

/// A type-safe wrapper around the `FontId`.
///
Expand Down Expand Up @@ -34,11 +35,13 @@ pub struct Ids<'a> {
}

/// Returned when loading new fonts from file or bytes.
#[derive(Debug)]
#[derive(Debug, Error)]
pub enum Error {
/// Some error occurred while loading a `FontCollection` from a file.
Io(std::io::Error),
#[error(transparent)]
Io(#[from] std::io::Error),
/// No `Font`s could be yielded from the `FontCollection`.
#[error("No `Font` found in the loaded `FontCollection`.")]
NoFont,
}

Expand Down Expand Up @@ -201,28 +204,4 @@ impl<'a> Iterator for Ids<'a> {
fn next(&mut self) -> Option<Self::Item> {
self.keys.next().map(|&id| id)
}
}

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

impl std::error::Error for Error {
fn cause(&self) -> Option<&dyn std::error::Error> {
match *self {
Error::Io(ref e) => Some(e),
Error::NoFont => None,
}
}
}

impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
match *self {
Error::Io(ref e) => std::fmt::Display::fmt(e, f),
Error::NoFont => write!(f, "No `Font` found in the loaded `FontCollection`."),
}
}
}
}
24 changes: 5 additions & 19 deletions nannou/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;
use std::{env, fmt};
use thiserror::Error;
use winit::dpi::{LogicalSize, PhysicalSize};

pub use winit::window::Fullscreen;
Expand Down Expand Up @@ -164,10 +165,12 @@ pub type UnfocusedFn<Model> = fn(&App, &mut Model);
pub type ClosedFn<Model> = fn(&App, &mut Model);

/// Errors that might occur while building the window.
#[derive(Debug)]
#[derive(Debug, Error)]
pub enum BuildError {
#[error("no wgpu adapter available to build window")]
NoAvailableAdapter,
WinitOsError(winit::error::OsError),
#[error(transparent)]
WinitOsError(#[from] winit::error::OsError),
}

// A macro for generating a handle to a function that can be stored within the Window without
Expand Down Expand Up @@ -1571,23 +1574,6 @@ impl fmt::Debug for View {
}
}

// Error implementations.

impl fmt::Display for BuildError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
BuildError::NoAvailableAdapter => write!(f, "no available wgpu adapter detected"),
BuildError::WinitOsError(ref e) => e.fmt(f),
}
}
}

impl From<winit::error::OsError> for BuildError {
fn from(e: winit::error::OsError) -> Self {
BuildError::WinitOsError(e)
}
}

// Some WGPU helper implementations.

impl<'a> wgpu::WithDeviceQueuePair for &'a crate::window::Window {
Expand Down