macro_rules! provided_attachments {
(@declare $getter_name:ident (single: $attachment_type:ty) -> $return_type:ty {
// Transformation closure, receiving type Option<&$attachment_type> and returning $return_type.
|$bind:ident| $transform:expr
}) => { ... };
(@implement $getter_name:ident (single: $attachment_type:ty) -> $return_type:ty {
// Transformation closure, receiving type Option<&$attachment_type> and returning $return_type.
|$bind:ident| $transform:expr
}) => { ... };
(@declare $getter_name:ident (multiple: $attachment_type:ty) -> $return_type:ty {
// Transformation closure, receiving type impl Iterator<Item = &$attachment_type> and returning $return_type.
|$bind:ident| $transform:expr
}) => { ... };
(@implement $getter_name:ident (multiple: $attachment_type:ty) -> $return_type:ty {
// Transformation closure, receiving type impl Iterator<Item = &$attachment_type> and returning $return_type.
|$bind:ident| $transform:expr
}) => { ... };
($(
$getter_name:ident ($multiplicity_matcher:ident : $attachment_type:ty) -> $return_type:ty { |$bind:ident| $transform:expr }
);* $(;)?) => { ... };
}Expand description
Create a helper trait NeuErrAttachments that is implemented for
NeuErr, which allows to directly retrieve your attachments. You can
modify visibility and name by re-exporting via pub use if needed.
This improves discoverability and allows you to unwrap potential new-types you might have had to use (or wanted to use).
ยงUsage
Simple getters without type transformation:
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
enum Retryable { Yes, No }
provided_attachments!(
retryable(single: Retryable) -> Option<&Retryable> { |v| v };
);This will create a method fn retryable(&self) -> Option<&Retryable> on NeuErr.
You can also make use of the transformation expression that will be applied to the attachment before returning it:
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
enum Retryable { Yes, No }
provided_attachments!(
retryable(single: Retryable) -> Retryable { |retry| retry.copied().unwrap_or(Retryable::No) };
);This will create a method fn retryable(&self) -> Retryable on NeuErr. The closure receives
the Option<&Retryable> and returns a Retryable.
Finally, you can also retrieve multiple attachments of the same type and transform the iterator into your return type:
#[derive(Debug, PartialEq, Clone)]
struct UserInfo(String);
provided_attachments!(
user_info(multiple: UserInfo) -> String { |iter| iter.map(|UserInfo(s)| s.as_str()).collect() };
);This will create a method fn user_info(&self) -> String on NeuErr, which collects all
UserInfo attachments, unpacks them and collects them into a single String.