Skip to content

Commit

Permalink
[misc]: Add TWError doc comments
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshiotomakan committed Apr 22, 2024
1 parent 6e2ed5c commit 43d5e3a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 5 additions & 0 deletions rust/tw_coin_entry/src/error/tw_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct TWError<E> {
}

impl<E> TWError<E> {
/// Converts `PrevE` into `E` and wraps it as [`TWError<E>`].
pub fn new<PrevE>(error: PrevE) -> Self
where
E: From<PrevE>,
Expand All @@ -22,13 +23,15 @@ impl<E> TWError<E> {
}
}

/// Converts `PrevE` into `E` and wraps it as [`Err(TWError<E>)`].
pub fn err<T, PrevE>(error: PrevE) -> TWResult<T, E>
where
E: From<PrevE>,
{
Err(TWError::new(error))
}

/// Adds an error context.
pub fn context<C>(mut self, context: C) -> Self
where
C: fmt::Display,
Expand All @@ -37,10 +40,12 @@ impl<E> TWError<E> {
self
}

/// Returns an inner error type.
pub fn error_type(&self) -> &E {
&self.error
}

/// Converts [`TWError<E>`] into [`TWError<NewE>`].
pub fn map_err<F, NewE>(self, f: F) -> TWError<NewE>
where
F: FnOnce(E) -> NewE,
Expand Down
8 changes: 6 additions & 2 deletions rust/tw_coin_entry/src/error/tw_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ use std::fmt;
pub type TWResult<T, E> = Result<T, TWError<E>>;

pub trait ResultContext {
/// Wrap the error value with additional context.
/// Wraps the error value with additional context.
fn context<C>(self, context: C) -> Self
where
C: fmt::Display;

/// Wrap the error value with additional context that is evaluated lazily
/// Wraps the error value with additional context that is evaluated lazily
/// only once an error does occur.
fn with_context<C, F>(self, f: F) -> Self
where
Expand All @@ -22,16 +22,20 @@ pub trait ResultContext {
}

pub trait IntoTWError<T, E> {
/// Wraps the inner `E` error into [`TWError<E>`].
fn into_tw(self) -> TWResult<T, E>;
}

pub trait MapTWError<T, E, PrevE> {
/// Maps `PrevE` into [`TWError<E>`] with an `F` mapper.
fn tw_err<F>(self, f: F) -> TWResult<T, E>
where
F: FnOnce(PrevE) -> E;
}

pub trait OrTWError<T, E> {
/// Transforms the [`Option<T>`] into a [`Result<T, TWError<E>>`], mapping [`Some(v)`] to
/// [`Ok(v)`] and [`None`] to [`Err(TWError<E>)`].
fn or_tw_err(self, error: E) -> TWResult<T, E>;
}

Expand Down

0 comments on commit 43d5e3a

Please sign in to comment.