-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructs.rs
More file actions
194 lines (170 loc) · 5.65 KB
/
structs.rs
File metadata and controls
194 lines (170 loc) · 5.65 KB
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
#[cfg(test)]
mod tests {
use std::fmt::{Debug, Display};
use error_stack_macros2::Error;
#[test]
fn unit_struct_works() {
#[derive(Debug, Error)]
#[display("unit struct")]
struct UnitStructType;
assert_eq!(UnitStructType.to_string(), "unit struct");
}
#[test]
fn named_field_struct_works_without_interpolation() {
#[derive(Debug, Error)]
#[display("named field struct")]
struct NamedFieldStructType {
_length: usize,
_is_ascii: bool,
_inner: String,
}
let test_val = NamedFieldStructType {
_length: 5,
_is_ascii: true,
_inner: String::from("hello"),
};
assert_eq!(test_val.to_string(), "named field struct");
}
#[test]
fn named_field_struct_works_with_interpolation_of_some_fields() {
#[derive(Debug, Error)]
#[display("named field struct: {inner:?} has {length} characters")]
struct NamedFieldStructType {
length: usize,
_is_ascii: bool,
inner: String,
}
let test_val = NamedFieldStructType {
length: 5,
_is_ascii: true,
inner: String::from("hello"),
};
assert_eq!(
test_val.to_string(),
"named field struct: \"hello\" has 5 characters"
);
}
#[test]
fn named_field_struct_works_with_interpolation_of_all_fields() {
#[derive(Debug, Error)]
#[display(
"named field struct: {inner:?} has {length} characters and is ascii={is_ascii}"
)]
struct NamedFieldStructType {
length: usize,
is_ascii: bool,
inner: String,
}
let test_val = NamedFieldStructType {
length: 5,
is_ascii: true,
inner: String::from("hello"),
};
assert_eq!(
test_val.to_string(),
"named field struct: \"hello\" has 5 characters and is ascii=true"
);
}
#[test]
fn named_field_struct_works_with_type_parameters() {
#[derive(Debug, Error)]
#[display("T = {t}, U = {u:?}")]
struct NamedFieldStructType<T: Display, U: Debug = Vec<u8>> {
t: T,
u: U,
}
let test_val = NamedFieldStructType {
t: String::from("string"),
u: vec![192, 168, 0, 254],
};
assert_eq!(test_val.to_string(), "T = string, U = [192, 168, 0, 254]");
}
#[test]
fn named_field_struct_works_with_lifetime_parameters() {
#[derive(Debug, Error)]
#[display(
"string_a = {string_a}, string_b = {string_b}, slice = {slice:?}"
)]
struct NamedFieldStructType<'a, 'b: 'a> {
string_a: &'a str,
string_b: &'b str,
slice: &'b [u8],
}
let test_val = NamedFieldStructType {
string_a: "string a",
string_b: "string b",
slice: &[192, 168, 0, 254],
};
assert_eq!(
test_val.to_string(),
"string_a = string a, string_b = string b, slice = [192, 168, 0, 254]"
);
}
#[test]
fn named_field_struct_works_with_const_parameters() {
#[derive(Debug, Error)]
#[display("inner = {inner}")]
struct NamedFieldStructType<const LENGTH: usize, const BYTE: u8 = 172> {
inner: u8,
}
let test_val = NamedFieldStructType::<8> { inner: 8 };
assert_eq!(test_val.to_string(), "inner = 8");
}
// TODO: move #[expect(redundant_lifetimes)] to type when fixed
#[test]
#[expect(
redundant_lifetimes,
reason = "this test requires a where clause with both lifetime and trait bounds"
)]
fn named_field_struct_works_with_where_clause() {
const STRING: &str = "t ref";
#[derive(Debug, Error)]
#[display("t_ref = {t_ref:?}")]
struct NamedFieldStructType<'a, T>
where
'a: 'static,
T: Debug,
{
t_ref: &'a T,
}
let test_val = NamedFieldStructType { t_ref: &STRING };
assert_eq!(test_val.to_string(), "t_ref = \"t ref\"");
}
#[test]
#[expect(
dead_code,
reason = "this test requires `TupleStructType`'s fields to exist, even though they won't be read"
)]
fn tuple_struct_works_without_interpolation() {
#[derive(Debug, Error)]
#[display("tuple struct")]
struct TupleStructType(isize, isize, isize);
let test_val = TupleStructType(5, 10, 15);
assert_eq!(test_val.to_string(), "tuple struct");
}
#[test]
#[expect(
dead_code,
reason = "this test requires `TupleStructType`to have multiple fields, even though not all of them will be read"
)]
fn tuple_struct_works_with_interpolation_of_some_fields() {
#[derive(Debug, Error)]
#[display("tuple struct: point with y value {1}")]
struct TupleStructType(isize, isize, isize);
let test_val = TupleStructType(5, 10, 15);
assert_eq!(test_val.to_string(), "tuple struct: point with y value 10");
}
#[test]
fn tuple_struct_works_with_interpolation_of_all_fields() {
#[derive(Debug, Error)]
#[display(
"tuple struct: point {2} units in front of the origin, and with x and y coords ({0}, {1})"
)]
struct TupleStructType(isize, isize, isize);
let test_val = TupleStructType(5, 10, 15);
assert_eq!(
test_val.to_string(),
"tuple struct: point 15 units in front of the origin, and with x and y coords (5, 10)"
);
}
}