@@ -13,7 +13,6 @@ pub(crate) const WARNING_TXT: &str = "warning";
13
13
/// Top-level user message
14
14
#[ derive( Clone , Debug ) ]
15
15
pub struct Message < ' a > {
16
- pub ( crate ) id : Option < Id < ' a > > , // for "correctness", could be sloppy and be on Title
17
16
pub ( crate ) groups : Vec < Group < ' a > > ,
18
17
}
19
18
@@ -26,7 +25,15 @@ impl<'a> Message<'a> {
26
25
///
27
26
/// </div>
28
27
pub fn id ( mut self , id : & ' a str ) -> Self {
29
- self . id . get_or_insert ( Id :: default ( ) ) . id = Some ( id) ;
28
+ let Some ( Element :: Title ( title) ) =
29
+ self . groups . get_mut ( 0 ) . and_then ( |g| g. elements . first_mut ( ) )
30
+ else {
31
+ panic ! (
32
+ "Expected first element to be a Title, got: {:?}" ,
33
+ self . groups
34
+ ) ;
35
+ } ;
36
+ title. id . get_or_insert ( Id :: default ( ) ) . id = Some ( id) ;
30
37
self
31
38
}
32
39
@@ -36,7 +43,15 @@ impl<'a> Message<'a> {
36
43
///
37
44
/// </div>
38
45
pub fn id_url ( mut self , url : & ' a str ) -> Self {
39
- self . id . get_or_insert ( Id :: default ( ) ) . url = Some ( url) ;
46
+ let Some ( Element :: Title ( title) ) =
47
+ self . groups . get_mut ( 0 ) . and_then ( |g| g. elements . first_mut ( ) )
48
+ else {
49
+ panic ! (
50
+ "Expected first element to be a Title, got: {:?}" ,
51
+ self . groups
52
+ ) ;
53
+ } ;
54
+ title. id . get_or_insert ( Id :: default ( ) ) . url = Some ( url) ;
40
55
self
41
56
}
42
57
@@ -174,10 +189,41 @@ pub struct Padding;
174
189
#[ derive( Clone , Debug ) ]
175
190
pub struct Title < ' a > {
176
191
pub ( crate ) level : Level < ' a > ,
192
+ pub ( crate ) id : Option < Id < ' a > > ,
177
193
pub ( crate ) title : & ' a str ,
178
194
pub ( crate ) is_pre_styled : bool ,
179
195
}
180
196
197
+ impl < ' a > Title < ' a > {
198
+ /// <div class="warning">
199
+ ///
200
+ /// This is only relevant if the title is the first element of a group.
201
+ ///
202
+ /// </div>
203
+ /// <div class="warning">
204
+ ///
205
+ /// Text passed to this function is considered "untrusted input", as such
206
+ /// all text is passed through a normalization function. Pre-styled text is
207
+ /// not allowed to be passed to this function.
208
+ ///
209
+ /// </div>
210
+ pub fn id ( mut self , id : & ' a str ) -> Self {
211
+ self . id . get_or_insert ( Id :: default ( ) ) . id = Some ( id) ;
212
+ self
213
+ }
214
+
215
+ /// <div class="warning">
216
+ ///
217
+ /// This is only relevant if the title is the first element of a group and
218
+ /// `id` present
219
+ ///
220
+ /// </div>
221
+ pub fn id_url ( mut self , url : & ' a str ) -> Self {
222
+ self . id . get_or_insert ( Id :: default ( ) ) . url = Some ( url) ;
223
+ self
224
+ }
225
+ }
226
+
181
227
/// A source view [`Element`] in a [`Group`]
182
228
///
183
229
/// If you do not have [source][Snippet::source] available, see instead [`Origin`]
0 commit comments