educe/
panic.rs

1#![cfg_attr(not(feature = "default"), allow(dead_code))]
2
3use crate::Trait;
4
5#[inline]
6pub fn reuse_a_trait(t: Trait) -> ! {
7    panic!("The trait `{:?}` is repeatedly used.", t)
8}
9
10#[inline]
11pub fn trait_not_used(t: Trait) -> ! {
12    panic!("The `{:?}` trait is not used.", t)
13}
14
15#[inline]
16pub fn trait_not_support_union(t: Trait) -> ! {
17    panic!("The `{:?}` trait does not support to a union.", t)
18}
19
20#[inline]
21pub fn attribute_incorrect_format(attribute_name: &str, correct_usage: &[&str]) -> ! {
22    panic!(
23        "You are using an incorrect format of the `{}` attribute.{}",
24        attribute_name,
25        concat_string_slice_array(correct_usage)
26    )
27}
28
29#[inline]
30pub fn parameter_incorrect_format(parameter_name: &str, correct_usage: &[&str]) -> ! {
31    panic!(
32        "You are using an incorrect format of the `{}` parameter.{}",
33        parameter_name,
34        concat_string_slice_array(correct_usage)
35    )
36}
37
38#[inline]
39pub fn derive_attribute_not_set_up_yet(attribute_name: &str) -> ! {
40    panic!(
41        "You are using `{}` in the `derive` attribute, but it has not been set up yet.",
42        attribute_name
43    )
44}
45
46#[inline]
47pub fn reset_parameter(parameter_name: &str) -> ! {
48    panic!("Try to reset the `{}` parameter.", parameter_name)
49}
50
51#[inline]
52pub fn unknown_parameter(attribute_name: &str, parameter_name: &str) -> ! {
53    panic!("Unknown parameter `{}` used in the `{}` attribute.", parameter_name, attribute_name)
54}
55
56#[inline]
57pub fn set_value_expression() -> ! {
58    panic!("The default value and the expression parameter can not be set at the same time.")
59}
60
61#[inline]
62pub fn set_expression_bound() -> ! {
63    panic!("You don't need to set the expression and the bound at the same time.")
64}
65
66#[inline]
67pub fn no_default_field() -> ! {
68    panic!("There is no field set as default.")
69}
70
71#[inline]
72pub fn multiple_default_fields() -> ! {
73    panic!("Multiple default fields are set.")
74}
75
76#[inline]
77pub fn no_default_variant() -> ! {
78    panic!("There is no variant set as default.")
79}
80
81#[inline]
82pub fn multiple_default_variants() -> ! {
83    panic!("Multiple default variants are set.")
84}
85
86#[inline]
87pub fn no_deref_field() -> ! {
88    panic!("There is no field which is assigned for `Deref`.")
89}
90
91#[inline]
92pub fn no_deref_field_of_variant(variant_name: &str) -> ! {
93    panic!(
94        "There is no field for the `{variant_name}` variant which is assigned for `Deref`.",
95        variant_name = variant_name
96    )
97}
98
99#[inline]
100pub fn multiple_deref_fields() -> ! {
101    panic!("Multiple fields are set for `Deref`.")
102}
103
104#[inline]
105pub fn multiple_deref_fields_of_variant(variant_name: &str) -> ! {
106    panic!(
107        "Multiple fields of the `{variant_name}` variant are set for deref.",
108        variant_name = variant_name
109    )
110}
111
112#[inline]
113pub fn deref_cannot_support_unit_variant() -> ! {
114    panic!("The `Deref` trait cannot be implemented for an enum which has unit variants.")
115}
116
117#[inline]
118pub fn no_deref_mut_field() -> ! {
119    panic!("There is no field which is assigned for `DerefMut`.")
120}
121
122#[inline]
123pub fn no_deref_mut_field_of_variant(variant_name: &str) -> ! {
124    panic!(
125        "There is no field for the `{variant_name}` variant which is assigned for `DerefMut`.",
126        variant_name = variant_name
127    )
128}
129
130#[inline]
131pub fn multiple_deref_mut_fields() -> ! {
132    panic!("Multiple fields are set for `DerefMut`.")
133}
134
135#[inline]
136pub fn multiple_deref_mut_fields_of_variant(variant_name: &str) -> ! {
137    panic!(
138        "Multiple fields of the `{variant_name}` variant are set for `DerefMut`.",
139        variant_name = variant_name
140    )
141}
142
143#[inline]
144pub fn deref_mut_cannot_support_unit_variant() -> ! {
145    panic!("The `DerefMut` trait cannot be implemented for an enum which has unit variants.")
146}
147
148#[inline]
149pub fn disable_named_field_name() -> ! {
150    panic!("You can't disable the name of a named field.")
151}
152
153#[inline]
154pub fn empty_parameter(parameter_name: &str) -> ! {
155    panic!("You can't set the `{}` parameter to empty.", parameter_name)
156}
157
158#[inline]
159pub fn unit_struct_need_name() -> ! {
160    panic!("A unit struct needs to have a name.")
161}
162
163#[inline]
164pub fn unit_enum_need_name() -> ! {
165    panic!("A unit enum needs to have a name.")
166}
167
168#[inline]
169pub fn unit_variant_need_name() -> ! {
170    panic!("A unit variant which doesn't use an enum name needs to have a name.")
171}
172
173#[inline]
174pub fn ignore_ranked_field() -> ! {
175    panic!("You can't ignore a ranked field.")
176}
177
178#[inline]
179pub fn reuse_a_rank(rank: isize) -> ! {
180    panic!("The rank `{}` is repeatedly used.", rank)
181}
182
183#[inline]
184pub fn reuse_a_value(value: isize) -> ! {
185    panic!("The value `{}` is repeatedly used.", value)
186}
187
188// TODO patterns
189
190#[inline]
191pub fn educe_format_incorrect() -> ! {
192    attribute_incorrect_format("educe", &[stringify!(#[educe(Trait1, Trait2, ..., TraitN)])])
193}
194
195fn concat_string_slice_array(array: &[&str]) -> String {
196    let len = array.len();
197
198    if len == 0 {
199        String::new()
200    } else {
201        let mut string = String::from(" It needs to be formed into ");
202
203        let mut iter = array.iter();
204
205        let first = iter.next().unwrap();
206
207        string.push('`');
208        string.push_str(&first.replace('\n', ""));
209        string.push('`');
210
211        if len > 2 {
212            for s in iter.take(len - 2) {
213                string.push_str(", `");
214                string.push_str(&s.replace('\n', ""));
215                string.push('`');
216            }
217        }
218
219        if len > 1 {
220            string.push_str(", or `");
221            string.push_str(&array[len - 1].replace('\n', ""));
222            string.push('`');
223        }
224
225        string.push('.');
226
227        string
228    }
229}