Skip to content

Commit

Permalink
🧪 Using error type pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
oovm committed Apr 9, 2024
1 parent 0d8cb7d commit 57ec46a
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ exclude = [
"projects/.DS_Store",
]

[workspace.dependencies]

[profile.release]
lto = true
panic = "abort"
6 changes: 4 additions & 2 deletions projects/sub_projects/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ publish = false
version = "0.0.0"
authors = ["Aster <[email protected]>"]
description = "..."
repository = "https://github.com/oovm/sub_projects"
categories = ["rust-patterns"]
homepage = "https://github.com/oovm/RustTemplate"
repository = "https://github.com/oovm/RustTemplate"
documentation = "https://docs.rs/sub_projects"
readme = "Readme.md"
readme = "readme.md"
license = "MPL-2.0"
edition = "2021"
exclude = ["package.json", "tests/**"]
Expand Down
6 changes: 0 additions & 6 deletions projects/sub_projects/src/errors.rs

This file was deleted.

9 changes: 9 additions & 0 deletions projects/sub_projects/src/errors/convert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use super::*;

impl From<ExampleErrorKind> for ExampleError {
fn from(value: ExampleErrorKind) -> Self {
Self {
kind: Box::new(value),
}
}
}
23 changes: 23 additions & 0 deletions projects/sub_projects/src/errors/display.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use super::*;

impl Error for ExampleError {}


impl Debug for ExampleError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Debug::fmt(&self.kind, f)
}
}

impl Display for ExampleError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.kind, f)
}
}


impl Display for ExampleErrorKind {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self { ExampleErrorKind::UnknownError => { write!(f, "UnknownError") } }
}
}
24 changes: 24 additions & 0 deletions projects/sub_projects/src/errors/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::fmt::{Debug, Formatter};
use std::error::Error;
use std::fmt::Display;

mod display;
mod convert;

/// The result type of this crate.
pub type Result<T> = std::result::Result<T, ExampleError>;

/// A boxed error kind, wrapping an [ExampleErrorKind].
#[derive(Clone)]
pub struct ExampleError {
kind: Box<ExampleErrorKind>,
}

/// The kind of [ExampleError].
#[derive(Debug, Copy, Clone)]
pub enum ExampleErrorKind {
/// An unknown error.
UnknownError
}


2 changes: 1 addition & 1 deletion projects/sub_projects/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

mod errors;

pub use crate::errors::{Error, Result};
pub use crate::errors::{ExampleErrorKind, Result, ExampleError};

0 comments on commit 57ec46a

Please sign in to comment.