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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//! # Overview
//!
//! The `synonym` library is a Rust crate designed to simplify the creation of newtypes. It provides a customizable `#[derive(Synonym)]` macro that automatically implements various traits based on the underlying type of your newtype. This saves you from the boilerplate code usually required when defining newtypes.
//!
//! # Usage
//!
//! To use `synonym`, add it to your Cargo.toml:
//!
//! ```toml
//! [dependencies]
//! synonym = "0.1.5"
//! ```
//!
//! ## Basic example
//!
//! Import the `Synonym` trait into your Rust file:
//!
//! ```rust
//! use synonym::Synonym;
//! ```
//!
//! Then, define your newtype and annotate it with `#[derive(Synonym)]:`
//! ```rust
//! #[derive(Synonym)]
//! pub struct MyInt(i32);
//! ```
//!
//! ## Customization with Attributes
//! You can customize which traits are implemented or skipped using the `#[synonym(skip(...))]` and `#[synonym(force(...))]` attributes:
//! ```rust
//! #[derive(Synonym)]
//! #[synonym(skip(Eq, PartialEq))]
//! pub struct MyString(String);
//! ```
//!
//! Supported `skip` and `force` values are listed in the *Trait implementation table* below.
//!
//! # Generated code
//! When you use `#[derive(Synonym)]`, the library generates implementations for various traits. Here's a simplified example for a newtype `MyInt(i32)`:
//! ```rust
//! impl Eq for MyInt {}
//! impl PartialEq for MyInt {
//! fn eq(&self, other: &Self) -> bool {
//! self.0 == other.0
//! }
//! }
//! // ... and so on for other traits
//! ```
//!
//! # Trait implementation table
//!
//! Custom methods
//!
//! | | skip/force | `Integer` [1] | `NonZero*` | `Float` | `String` | `Box<str>` | `&'static str` | `char` |
//! |------------------|-------------|---------------|------------|---------|----------|------------|----------------|--------|
//! | .as_str() | String | | | | v | v | v | v |
//! | .value() [2] | Value | v | v | v | v | v | v | v |
//!
//!
//! Conversion
//!
//! | | skip/force | `Integer` [1] | `NonZero*` | `Float` | `String` | `Box<str>` | `&'static str` | `char` |
//! |------------------|-------------|---------------|------------|---------|----------|------------|----------------|--------|
//! | AsRef<Inner> | AsRef | v | v | v | v | v | v | v |
//! | Borrow<str> | String | | | | v | v | v | |
//! | From<&'a str> | String | | | | v | v [4] | | |
//! | From<String> | String | | | | | v | | |
//! | Deref<Inner> [3] | Deref | | | | | | | |
//! | DerefMut [3] | DerefMut | | | | | | | |
//! | From<Inner> | From | v | v | v | v | v | v | v |
//! | FromStr | FromStr | v | v | v | v | v | | v |
//!
//! Fundamental traits
//!
//! | | skip/force | `Integer` [1] | `NonZero*` | `Float` | `String` | `Box<str>` | `&'static str` | `char` |
//! |------------------|-------------|---------------|------------|---------|----------|------------|----------------|--------|
//! | Clone | Clone | v | v | v | v | v | v | v |
//! | Copy | Copy | v | v | v | | v | v |
//! | Debug | Debug | v | v | v | v | v | v | v |
//! | Default | Default | v | | v | v | v | v | v |
//! | Display [5] | Display | v | v | v | v | v | v | v |
//! | Hash | Hash | v | v | | v | v | v | v |
//!
//! Comparison
//!
//! | | skip/force | `Integer` [1] | `NonZero*` | `Float` | `String` | `Box<str>` | `&'static str` | `char` |
//! |------------------|-------------|---------------|------------|---------|----------|------------|----------------|--------|
//! | PartialOrd | PartialOrd | v | v | v | v | v | v | v |
//! | Ord | Ord | v | v | | v | v | v | v |
//! | PartialEq | PartialEq | v | v | v | v | v | v | v |
//! | Eq | Eq | v | v | | v | v | v | v |
//!
//! Serde [6]
//!
//! | | skip/force | `Integer` [1] | `NonZero*` | `Float` | `String` | `Box<str>` | `&'static str` | `char` |
//! |------------------|-------------|---------------|------------|---------|----------|------------|----------------|--------|
//! | Serialize | Serialize | v | v | v | v | v | | v |
//! | Deserialize | Deserialize | v | v | v | v | v | | v |
//!
//! Maths [7]
//!
//! | | skip/force | `Integer` [1] | `NonZero*` | `Float` | `String` | `Box<str>` | `&'static str` | `char` |
//! |------------------|-------------|---------------|------------|---------|----------|------------|----------------|--------|
//! | Add<Self>=Self | Number | v | | v | | | |
//! | AddAssign<Self> | Number | v | | v | | | |
//! | Sub<Self>=Self | Number | v | | v | | | |
//! | SubAssign<Self> | Number | v | | v | | | |
//! | Mul<Self>=Self | Number | v | | v | | | |
//! | MulAssign<Self> | Number | v | | v | | | |
//! | Div<Self>=Self | Number | v | | v | | | |
//! | DivAssign<Self> | Number | v | | v | | | |
//!
//!
//! [1] Integers are: `u8`, `u16`, `u32`, `u64`, `u128`, `usize`, `i8`, `i16`, `i32`, `i64`, `i128`, `isize`
//! [2] .value() returns `Inner` for `Copy` types and `&Inner` for non-`Copy` types
//! [3] `Deref` and `DerefMut` are never implemented unless they are forced with `#[synonym(force(Deref,DerefMut))]`
//! [4] In constrast to other strings, `FromStr` for `Box<str>` synonyms uses `Inner::From<&'str>` instead of `Inner::FromStr` since there is no `FromStr` implementation for `Box<str>`
//! [5] Display implementation can be configured, see below
//! [6] Only provided when feature `with_serde` is enabled
//! [7] This is subject to change
//!
//! # Fine-tuning
//!
//! ## Display
//! To specify how the Display trait should be implemented, you can use the `#[synonym(display = "...")]` attribute. Here are the available options:
//!
//! * `Opaque`: Formats the output as TypeName(Value).
//! * `Transparent`: Directly uses the inner type's Display implementation.
//! * `UpperCase`: Converts the inner value to uppercase before displaying.
//! * `LowerCase`: Converts the inner value to lowercase before displaying.
//! * `OpaqueUpperCase`: Formats the output as TypeName(VALUE) where VALUE is uppercase.
//! * `OpaqueLowerCase`: Formats the output as TypeName(value) where value is lowercase.
//! * `Custom string`: Allows for a custom format string
//!
//! ### Examples
//! ```rust
//! #[derive(Synonym)]
//! #[synonym(display = "UpperCase")]
//! struct CountryName(String);
//!
//! #[derive(Synonym)]
//! #[synonym(display = "::<> {} <>::")]
//! struct Turbo(String);
//! ```
//!
//! ## Serde Support
//!
//! To enable Serde support for serialization and deserialization, you'll need to enable the `with_serde` feature flag in your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! synonym = { version = "0.1.5", features = ["with_serde"] }
//! ```
//!
//! With this feature enabled, the `Serialize` and `Deserialize` traits will be automatically implemented for your type.
//!
//! ---
//! This documentation was generated with the assistance of ChatGPT-4 by OpenAI.
extern crate proc_macro;
use crateTokenStream;
use crate::;
use quote;
use TokenStreamExt;
use ;