Skip to content

Commit

Permalink
with -> attach
Browse files Browse the repository at this point in the history
  • Loading branch information
mkatychev committed Oct 15, 2024
1 parent aa4643d commit cda942e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
2 changes: 1 addition & 1 deletion bigerror/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl NotFound {
}

pub fn with_index<T, K: Display>(key: K) -> Report<Self> {
Self::with_kv(Index(key), ty!(T))
Self::attach_kv(Index(key), ty!(T))
}
}

Expand Down
41 changes: 21 additions & 20 deletions bigerror/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ where
Self: Sized + Context,
{
const VALUE: Self;

fn report<C: Context>(ctx: C) -> Report<Self> {
Report::new(ctx).change_context(Self::VALUE)
}
Expand Down Expand Up @@ -61,23 +62,23 @@ where
Self::attach(Dbg(value))
}
#[track_caller]
fn with_kv<K, V>(key: K, value: V) -> Report<Self>
fn attach_kv<K, V>(key: K, value: V) -> Report<Self>
where
K: Display,
V: Display,
{
Self::attach(KeyValue(key, value))
}
#[track_caller]
fn with_kv_dbg<K, V>(key: K, value: V) -> Report<Self>
fn attach_kv_dbg<K, V>(key: K, value: V) -> Report<Self>
where
K: Display,
V: Debug,
{
Self::attach(KeyValue::dbg(key, value))
}
#[track_caller]
fn with_field_status<S: Display>(key: &'static str, status: S) -> Report<Self> {
fn attach_field<S: Display>(key: &'static str, status: S) -> Report<Self> {
Self::attach(Field::new(key, status))
}

Expand All @@ -87,23 +88,23 @@ where
}

#[track_caller]
fn with_variant<A: Display>(value: A) -> Report<Self> {
Self::with_kv(attachment::Type::of::<A>(), value)
fn attach_ty_val<A: Display>(value: A) -> Report<Self> {
Self::attach_kv(ty!(A), value)
}

#[track_caller]
fn with_variant_dbg<A: Debug>(value: A) -> Report<Self> {
Self::with_kv_dbg(attachment::Type::of::<A>(), value)
fn attach_ty_dbg<A: Debug>(value: A) -> Report<Self> {
Self::attach_kv_dbg(ty!(A), value)
}

#[track_caller]
fn with_type<A>() -> Report<Self> {
Self::attach(attachment::Type::of::<A>())
fn attach_ty<A>() -> Report<Self> {
Self::attach(ty!(A))
}

#[track_caller]
fn with_type_status<A: Send + Sync + 'static>(status: impl Display) -> Report<Self> {
Self::attach(Field::new(attachment::Type::of::<A>(), status))
fn attach_ty_status<A: Send + Sync + 'static>(status: impl Display) -> Report<Self> {
Self::attach(Field::new(ty!(A), status))
}
}

Expand Down Expand Up @@ -238,12 +239,12 @@ pub trait AttachExt {
A: Debug;

#[must_use]
fn attach_variant<A>(self, value: A) -> Self
fn attach_ty_val<A>(self, value: A) -> Self
where
Self: Sized,
A: Display,
{
self.attach_kv(attachment::Type::of::<A>(), value)
self.attach_kv(ty!(A), value)
}
}

Expand Down Expand Up @@ -484,7 +485,7 @@ impl<T> OptionReport<T> for Option<T> {
// self.ok_or_else(|| Report::new(NotFound))
match self {
Some(v) => Ok(v),
None => Err(NotFound::with_type::<T>()),
None => Err(NotFound::attach_ty::<T>()),
}
}

Expand All @@ -497,7 +498,7 @@ impl<T> OptionReport<T> for Option<T> {
{
match self {
Some(v) => Ok(v),
None => Err(NotFound::with_kv(key, value)),
None => Err(NotFound::attach_kv(key, value)),
}
}

Expand Down Expand Up @@ -702,10 +703,10 @@ mod test {
}

#[test]
fn with_type_status() {
fn attach_ty_status() {
fn try_even(num: usize) -> Result<(), Report<MyError>> {
if num % 2 != 0 {
return Err(InvalidInput::with_type_status::<usize>(Invalid).into_ctx());
return Err(InvalidInput::attach_ty_status::<usize>(Invalid).into_ctx());
}
Ok(())
}
Expand Down Expand Up @@ -744,11 +745,11 @@ mod test {
}

#[test]
fn attach_variant() {
fn attach_ty_val() {
fn compare(mine: usize, other: usize) -> Result<(), Report<MyError>> {
if other != mine {
bail!(InvalidInput::attach("expected my number!")
.attach_variant(other)
.attach_ty_val(other)
.into_ctx());
}
Ok(())
Expand All @@ -760,7 +761,7 @@ mod test {
assert_err!(compare(my_number, other_number));
}

// should behave the same as `test::attach_variant`
// should behave the same as `test::attach_ty_val`
// but displays lazy allocation of attachment
#[test]
fn attach_kv_macro() {
Expand Down

0 comments on commit cda942e

Please sign in to comment.