Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed Feb 15, 2024
1 parent 46de73d commit 0ff36e6
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 19 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Dunge

<div align="center">
<h1>Dunge</h1>
<p>
Simple and portable 3d render based on <a href="https://github.com/gfx-rs/wgpu">WGPU</a>.
</p>
Expand Down
13 changes: 10 additions & 3 deletions dunge/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ impl Context {

/// An error returned from the [context](Context) constructor.
#[derive(Debug)]
pub enum Error {
pub enum MakeContextError {
BackendSelection,
RequestDevice(wgpu::RequestDeviceError),
}

impl fmt::Display for Error {
impl fmt::Display for MakeContextError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::BackendSelection => write!(f, "failed to select backend"),
Expand All @@ -135,4 +135,11 @@ impl fmt::Display for Error {
}
}

impl error::Error for Error {}
impl error::Error for MakeContextError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Self::BackendSelection => None,
Self::RequestDevice(err) => Some(err),
}
}
}
8 changes: 4 additions & 4 deletions dunge/src/init.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
crate::{
context::{self, Context},
context::{Context, MakeContextError},
state::State,
},
wgpu::Instance,
Expand All @@ -9,7 +9,7 @@ use {
#[cfg(feature = "winit")]
use crate::{element::Element, window::WindowBuilder};

pub(crate) async fn make() -> Result<(Context, Instance), context::Error> {
pub(crate) async fn make() -> Result<(Context, Instance), MakeContextError> {
use wgpu::{Backends, InstanceDescriptor, InstanceFlags};

let backends;
Expand Down Expand Up @@ -44,8 +44,8 @@ pub(crate) async fn make() -> Result<(Context, Instance), context::Error> {
///
/// # Errors
/// Returns an error when the context could not be created.
/// See [`Error`](context::Error) for details.
pub async fn context() -> Result<Context, context::Error> {
/// See [`MakeContextError`] for details.
pub async fn context() -> Result<Context, MakeContextError> {
make().await.map(|(cx, _)| cx)
}

Expand Down
2 changes: 1 addition & 1 deletion dunge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub mod prelude {

pub use {
crate::{
context::{Context, Error},
context::{Context, MakeContextError},
draw::{draw, Draw},
format::Format,
init::context,
Expand Down
8 changes: 4 additions & 4 deletions dunge/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use {
crate::{
color::Rgba,
context::Error,
context::MakeContextError,
draw::Draw,
format::Format,
layer::{Layer, SetLayer},
Expand All @@ -23,7 +23,7 @@ pub(crate) struct State {
}

impl State {
pub async fn new(instance: &Instance) -> Result<Self, Error> {
pub async fn new(instance: &Instance) -> Result<Self, MakeContextError> {
let adapter = {
use wgpu::{PowerPreference, RequestAdapterOptions};

Expand All @@ -35,7 +35,7 @@ impl State {
instance
.request_adapter(&options)
.await
.ok_or(Error::BackendSelection)?
.ok_or(MakeContextError::BackendSelection)?
};

let backend = adapter.get_info().backend;
Expand All @@ -58,7 +58,7 @@ impl State {
adapter
.request_device(&desc, None)
.await
.map_err(Error::RequestDevice)?
.map_err(MakeContextError::RequestDevice)?
};

Ok(Self {
Expand Down
8 changes: 4 additions & 4 deletions dunge/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use {
crate::{
context::{self, Context},
context::{Context, MakeContextError},
el::Loop,
element::Element,
format::Format,
Expand Down Expand Up @@ -320,8 +320,8 @@ impl From<CreateSurfaceError> for Error {
}
}

impl From<context::Error> for Error {
fn from(v: context::Error) -> Self {
impl From<MakeContextError> for Error {
fn from(v: MakeContextError) -> Self {
Self(ErrorKind::Context(v))
}
}
Expand Down Expand Up @@ -356,5 +356,5 @@ enum ErrorKind {
EventLoop(EventLoopError),
Os(OsError),
Surface(CreateSurfaceError),
Context(context::Error),
Context(MakeContextError),
}
2 changes: 1 addition & 1 deletion xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn run() -> Result<(), Error> {
let module = env::args().nth(1).ok_or("no module specified")?;
let root = Path::new(&env!("CARGO_MANIFEST_DIR"))
.parent()
.expect("root dir");
.ok_or("root dir not found")?;

let status = Command::new("wasm-pack")
.current_dir(root)
Expand Down

0 comments on commit 0ff36e6

Please sign in to comment.