1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//! Derive macros for rust that are both powerful and flexible.
//!
//! Named after the mathematical symbol for the gradient (multivariable derivative).
//!
//! Available derive macros:
//! - [`Display`](core::fmt::Display) works similar to the [`thiserror::Error`] macro,
//! but only implements the `Display` trait and not `std::error::Error`.
//!
//! [`thiserror::Error`]: https://docs.rs/thiserror/2/thiserror/derive.Error.html
/// Derive [`core::fmt::Display`] in a manner similar to the [`thiserror::Error`] derive macro.
///
/// Each struct or enum variant can have a `#[display("fmtstr")]` attribute
/// where `"fmtstr"` has access to the fields of the struct/variant.
///
/// The `#[nabla(display(...))` attribute means the same thing as `#[display(...)]`.
/// This may be useful to avoid conflicts or for clarity.
///
/// # Examples
/// Using a traditional struct:
/// ```
/// # extern crate nabla_macros as nabla;
/// #[derive(nabla::Display, Debug)]
/// #[display("Hello World from {a} and {b}")]
/// struct Hello {
/// a: u32,
/// b: &'static str,
/// extra: Option<bool>,
/// }
/// assert_eq!(
/// Hello { a: 4, b: "bob", extra: None }.to_string(),
/// "Hello World from 4 and bob"
/// );
/// ```
/// Using an enum:
/// ```
/// #[derive(nabla::Display, Debug)]
/// enum SimpleError {
/// #[display("Expected {expected} items, but got {actual}")]
/// WrongCount {
/// expected: usize,
/// actual: usize
/// },
/// #[display("{0}")]
/// Io(std::io::Error),
/// #[display("Request is too large")]
/// RequestTooLarge,
/// }
/// assert_eq!(
/// SimpleError::WrongCount { expected: 3, actual: 4 }.to_string(),
/// "Expected 3 items, but got 4"
/// );
/// assert_eq!(
/// SimpleError::RequestTooLarge.to_string(),
/// "Request is too large"
/// );
/// let err = std::io::Error::other("IO error message");
/// assert_eq!(
/// SimpleError::Io(err).to_string(),
/// "IO error message",
/// )
/// ```
///
///
/// # Not Yet Implemented
/// - Use of `.0` or `.field` shorthand to reference fields in format args
/// - A `#[display(transparent)]` attribute similar to `#[error(transparent)]` in thisserror.
/// This can be easily emulated by `#[display("{0}")]
///
/// [`thiserror::Error`]: https://docs.rs/thiserror/2/thiserror/derive.Error.html
pub use Display;
/// Derive [`core::convert::From`] for newtype structs.
///
/// # Examples
/// ```
/// extern crate nabla_macros as nabla;
///
/// #[derive(nabla::From, Eq, PartialEq, Debug)]
/// struct Wrapper(u32);
/// assert_eq!(
/// <Wrapper as From<u32>>::from(3),
/// Wrapper(3)
/// );
/// ```
pub use From;