Skip to main content

battler_wamprat_error/
lib.rs

1//! # battler-wamprat-error
2//!
3//! **battler-wamprat-error** is a utility crate for [`battler-wamprat`](https://crates.io/crates/battler-wamprat). It provides a procedural macro for custom WAMP errors.
4//!
5//! `battler_wamp` and `battler_wamprat` use the `battler_wamp::core::error::WampError` type for
6//! errors transmitted between peers and routers. Errors are categorized by their URI and may be
7//! contextualized by an error message.
8//!
9//! Custom WAMP error types can implement conversions to and from the core
10//! `battler_wamp::core::error::WampError` type for use in application code (and as a part of the
11//! `battler_wamprat`) framework. The [`WampError`] derive macro generates these conversions for
12//! you. It works for structs or enums. All structs and enum variants must have the following
13//! properties:
14//!
15//! 1. Have a valid URI attribute for identification.
16//! 1. Be a unit struct/variant or be constructible from a single [`&str`][`str`] (the error
17//!    message).
18//!
19//! ## Example
20//!
21//! ```
22//! use battler_wamp::core::error::WampError;
23//! use battler_wamp_uri::Uri;
24//! use battler_wamprat_error::WampError;
25//!
26//! #[derive(Debug, PartialEq, WampError)]
27//! enum CustomError {
28//!     #[uri("com.test.missing_input")]
29//!     MissingInput,
30//!     #[uri("com.test.failed")]
31//!     Failed(String),
32//! }
33//!
34//! impl std::fmt::Display for CustomError {
35//!     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36//!         match self {
37//!             Self::MissingInput => write!(f, "missing input!"),
38//!             Self::Failed(msg) => write!(f, "{msg}"),
39//!         }
40//!     }
41//! }
42//!
43//! fn main() {
44//!     // Into.
45//!     assert_eq!(
46//!         Into::<WampError>::into(CustomError::MissingInput),
47//!         WampError::new(
48//!             Uri::try_from("com.test.missing_input").unwrap(),
49//!             "missing input!"
50//!         )
51//!     );
52//!     assert_eq!(
53//!         Into::<WampError>::into(CustomError::Failed("connection lost".to_owned())),
54//!         WampError::new(Uri::try_from("com.test.failed").unwrap(), "connection lost")
55//!     );
56//!
57//!     // From.
58//!     assert_eq!(
59//!         TryInto::<CustomError>::try_into(WampError::new(
60//!             Uri::try_from("com.test.missing_input").unwrap(),
61//!             ""
62//!         ))
63//!         .unwrap(),
64//!         CustomError::MissingInput
65//!     );
66//!     assert_eq!(
67//!         TryInto::<CustomError>::try_into(WampError::new(
68//!             Uri::try_from("com.test.failed").unwrap(),
69//!             "deadline exceeded"
70//!         ))
71//!         .unwrap(),
72//!         CustomError::Failed("deadline exceeded".to_owned())
73//!     );
74//! }
75//! ```
76
77pub use battler_wamprat_error_proc_macro::WampError;