rustviz_lib/
hover_messages.rs

1/* Event Dot messages: shows up when someone hovers over a dot */
2
3// Add styling to string name with <span>
4fn fmt_style(plain: &String) -> String {
5    let span_begin = String::from(
6        "&lt;span style=&quot;font-family: 'Source Code Pro',
7        Consolas, 'Ubuntu Mono', Menlo, 'DejaVu Sans Mono',
8        monospace, monospace !important;&quot;&gt;"
9    );
10    let span_end = "&lt;/span&gt;";
11
12    span_begin + plain + span_end
13}
14
15/* The Event dot does not connect to any arrows, we typically follow the following format:
16   ... happens
17 */
18
19// 1   0 (0 is initialized by let 0 = &1)
20//     |
21//     *   the star event
22// 
23// example: 
24// fn calculate_length(s: &String) -> usize { // s is a reference to a String
25//     s.len()      // a REF_GO_OUT_OF_SCOPE event
26// } // Here, s goes out of scope. But because it does not have ownership of what
27//   // it refers to, nothing happens.
28pub fn event_dot_ref_go_out_out_scope(my_name: &String) -> String {
29    // update styling
30    let my_name_fmt = fmt_style(my_name);
31    
32    format!(
33        "{0} goes out of scope",
34        my_name_fmt
35    )
36}
37
38// 0
39// |
40// *   the star event
41//
42pub fn event_dot_owner_go_out_out_scope(my_name: &String) -> String {
43    // update styling
44    let my_name_fmt = fmt_style(my_name);
45    
46    format!(
47        "{0} goes out of scope", //we shouldn't say "the resource is dropped"
48        my_name_fmt              //because we don't distinguish if the resource
49    )                             //was moved from the variable earlier.
50}
51
52//     0
53//     
54// f1  *   the star event
55// example: 
56// fn calculate_length(s: &String) -> usize { // here s is initialized to some value
57//    /* something happens */
58// }
59pub fn event_dot_init_param(my_name: &String) -> String {
60    // update styling
61    let my_name_fmt = fmt_style(my_name);
62    
63    format!(
64        "{0} is initialized as the function argument",
65        my_name_fmt
66    )
67}
68
69
70/* The Event dot is the source of an arrow, we typically follow the following format: 
71   ... to <Resource Owner 1>
72 */
73
74// 1   0
75//     |
76// o<--*   the star event
77// |   |
78pub fn event_dot_copy_to(my_name: &String, _target_name: &String) -> String {
79    // update styling
80    let my_name_fmt = fmt_style(my_name);
81    
82    format!(
83        "{0}'s resource is copied",
84        my_name_fmt
85    )
86}
87
88// 1   0
89//     |
90// o<--*   the star event
91// |
92pub fn event_dot_move_to(my_name: &String, _target_name: &String) -> String {
93    // update styling
94    let my_name_fmt = fmt_style(my_name);
95    
96    format!(
97        "{0}'s resource is moved",
98        my_name_fmt
99    )
100}
101
102// def fn(p):
103//  p
104//  |
105//  *-->   the return event
106//
107pub fn event_dot_move_to_caller(my_name: &String, _target_name: &String) -> String {
108    // update styling
109    let my_name_fmt = fmt_style(my_name);
110    
111    format!(
112        "{0}'s resource is moved to the caller",
113        my_name_fmt
114    )
115}
116
117// 1   0
118//     |
119// o<--*   the star event (&)
120// |   |
121pub fn event_dot_static_lend(my_name: &String, _target_name: &String) -> String {
122    // update styling
123    let my_name_fmt = fmt_style(my_name);
124
125    format!(
126        "{0}'s resource is immutably borrowed",
127        my_name_fmt
128    )
129}
130
131// 1   0
132//     |
133// o<--*   the star event (&mut)
134// |
135pub fn event_dot_mut_lend(my_name: &String, _target_name: &String) -> String {
136    // update styling
137    let my_name_fmt = fmt_style(my_name);
138
139    format!(
140        "{0}'s resource is mutably borrowed",
141        my_name_fmt
142    )
143}
144
145// 0   1
146//     |
147// o<--o
148// |   |
149// *-->o   the star event (&)
150pub fn event_dot_static_return(my_name: &String, _target_name: &String) -> String {
151    // update styling
152    let my_name_fmt = fmt_style(my_name);
153    
154    format!(
155        "{0}'s mutable borrow ends",
156        my_name_fmt
157    )
158}
159
160// 0   1
161//     |
162// o<--o
163// |
164// *-->o   the star event (&mut)
165pub fn event_dot_mut_return(my_name: &String, _target_name: &String) -> String {
166    // update styling
167    let my_name_fmt = fmt_style(my_name);
168    
169    format!(
170        "{0}'s immutable borrow ends",
171        my_name_fmt
172    )
173}
174
175/* The Event dot is the destination of an arrow, we typically follow the following format: 
176   ... from <Resource Owner 1>
177 */
178
179// 1   0        1   0
180// |            |   
181// o-->*   or   o-->*     the star event
182// |   |            |
183pub fn event_dot_acquire(my_name: &String, _target_name: &String) -> String {
184    // update styling
185    let my_name_fmt = fmt_style(my_name);
186    
187    format!(
188        "{0} acquires ownership of a resource",
189        my_name_fmt
190    )
191}
192
193// 1   0        1   0
194// |            |   
195// o-->*   or   o-->*     the star event
196// |   |            |
197pub fn event_dot_copy_from(my_name: &String, target_name: &String) -> String {
198    format!(
199        "{0} is initialized by copy from {1}",
200        my_name,
201        target_name
202    )
203}
204
205// 0   1
206//     |
207// *<--o   the star event (&mut)
208// |   |
209pub fn event_dot_mut_borrow(my_name: &String, _target_name: &String) -> String {
210    // update styling
211    let my_name_fmt = fmt_style(my_name);
212    
213    format!(
214        "{0} mutably borrows a resource",
215        my_name_fmt
216    )
217}
218
219// 0   1
220//     |
221// *<--o   the star event (&)
222// |   |
223pub fn event_dot_static_borrow(my_name: &String, _target_name: &String) -> String {
224    // update styling
225    let my_name_fmt = fmt_style(my_name);
226    
227    format!(
228        "{0} immutably borrows a resource",
229        my_name_fmt
230    )
231}
232
233// 1   0
234//     |
235// o<--o
236// |   |
237// o-->*   the star event (&)
238pub fn event_dot_static_reacquire(my_name: &String, _target_name: &String) -> String {
239    // update styling
240    let my_name_fmt = fmt_style(my_name);
241    
242    format!(
243        "{0}'s resource is no longer immutably borrowed",
244        my_name_fmt
245    )
246}
247
248// 1   0
249//     |
250// o<--o
251// |
252// o-->*   the star event (&mut)
253pub fn event_dot_mut_reacquire(my_name: &String, _target_name: &String) -> String {
254    // update styling
255    let my_name_fmt = fmt_style(my_name);
256    
257    format!(
258        "{0}'s resource is no longer mutably borrowed",
259        my_name_fmt
260    )
261}
262
263
264
265/* Arrow messages: shows up when someone hovers over an arrow */
266
267// 1   0
268//     |
269// o<--o
270// |
271pub fn arrow_move_val_to_val(from_name: &String, to_name: &String) -> String {
272    // update styling
273    let from_name_fmt = fmt_style(from_name);
274    let to_name_fmt = fmt_style(to_name);
275    
276    format!(
277        "{0}'s resource is moved to {1}",
278        from_name_fmt,
279        to_name_fmt
280    )
281}
282
283// 1   0
284//     |
285// o<--o
286// |   |
287pub fn arrow_copy_val_to_val(from_name: &String, to_name: &String) -> String {
288    // update styling
289    let from_name_fmt = fmt_style(from_name);
290    let to_name_fmt = fmt_style(to_name);
291    
292    format!(
293        "{0}'s resource is copied to {1}",
294        from_name_fmt,
295        to_name_fmt
296    )
297}
298
299// f1  0
300//     |
301// o<--o
302// |
303pub fn arrow_move_val_to_func(from_name: &String, to_name: &String) -> String {
304    // update styling
305    let from_name_fmt = fmt_style(from_name);
306    let to_name_fmt = fmt_style(to_name);
307    
308    format!(
309        "{0}'s resource is moved to function {1}",
310        from_name_fmt,
311        to_name_fmt
312    )
313}
314
315// f1  0
316//     |
317// o<--o
318// |   |
319pub fn arrow_copy_val_to_func(from_name: &String, to_name: &String) -> String {
320    // update styling
321    let from_name_fmt = fmt_style(from_name);
322    let to_name_fmt = fmt_style(to_name);
323    
324    format!(
325        "{0}'s resource is copied to function {1}",
326        from_name_fmt,
327        to_name_fmt
328    )
329}
330
331// 1  f0
332//     |
333// o<--o
334// |
335pub fn arrow_move_func_to_val(from_name: &String, to_name: &String) -> String {
336    // update styling
337    let from_name_fmt = fmt_style(from_name);
338    let to_name_fmt = fmt_style(to_name);
339    
340    format!(
341        "Function {0}'s resource is moved to {1}",
342        from_name_fmt,
343        to_name_fmt
344    )
345}
346
347// 1   0
348//     |
349// o<--o
350// |   |
351pub fn arrow_static_lend_val_to_val(from_name: &String, to_name: &String) -> String {
352    // update styling
353    let from_name_fmt = fmt_style(from_name);
354    let to_name_fmt = fmt_style(to_name);
355    
356    format!(
357        "{0}'s resource is immutably borrowed by {1}",
358        from_name_fmt,
359        to_name_fmt
360    )
361}
362
363// 1   0
364//     |
365// o<->o
366//     |
367pub fn arrow_static_lend_val_to_func(from_name: &String, to_name: &String) -> String {
368    // update styling
369    let from_name_fmt = fmt_style(from_name);
370    let to_name_fmt = fmt_style(to_name);
371    
372    format!(
373        "{0}'s resource is immutably borrowed by function {1}",
374        from_name_fmt,
375        to_name_fmt
376    )
377}
378
379// 1   0
380//     |
381// o<--o
382// |
383pub fn arrow_mut_lend_val_to_val(from_name: &String, to_name: &String) -> String {
384    // update styling
385    let from_name_fmt = fmt_style(from_name);
386    let to_name_fmt = fmt_style(to_name);
387    
388    format!(
389        "{0}'s resource is mutably borrowed by {1}",
390        from_name_fmt,
391        to_name_fmt
392    )
393}
394
395// 1   0
396//     |
397// o<->o
398//     |
399pub fn arrow_mut_lend_val_to_func(from_name: &String, to_name: &String) -> String {
400    // update styling
401    let from_name_fmt = fmt_style(from_name);
402    let to_name_fmt = fmt_style(to_name);
403    
404    format!(
405        "{0}'s resource is mutably borrowed by function {1}",
406        from_name_fmt,
407        to_name_fmt
408    )
409}
410
411// 0   1
412//     |
413// o<--o
414// |   |
415// o-->o   this arrow (&)
416pub fn arrow_static_return(from_name: &String, to_name: &String) -> String {
417    // update styling
418    let from_name_fmt = fmt_style(from_name);
419    let to_name_fmt = fmt_style(to_name);
420    
421    format!(
422        "{0}'s immutable borrow of {1}'s resource ends",
423        from_name_fmt,
424        to_name_fmt
425    )
426}
427
428// 0   1
429//     |
430// o<--o
431// |
432// o-->o   the star event (&mut)
433pub fn arrow_mut_return(from_name: &String, to_name: &String) -> String {
434    // update styling
435    let from_name_fmt = fmt_style(from_name);
436    let to_name_fmt = fmt_style(to_name);
437    
438    format!(
439        "{0}'s mutable borrow of {1}'s resource ends",
440        from_name_fmt,
441        to_name_fmt
442    )
443}
444
445
446
447/* State messages: shows up on the vertical lines for every value/reference */
448
449// The viable is no longer in the scope after this line.
450pub fn state_out_of_scope(my_name: &String) -> String {
451    // update styling
452    let my_name_fmt = fmt_style(my_name);
453    
454    format!(
455        "{0} is out of scope",
456        my_name_fmt
457    )
458}
459
460// The resource is transferred on this line or before this line due to move,
461// thus it is impossible to access this variable anymore. This is an invisible line in the timeline.
462pub fn state_resource_moved(my_name: &String, _to_name: &String) -> String {
463    // update styling
464    let my_name_fmt = fmt_style(my_name);
465    
466    format!(
467        "{0}'s resource was moved, so {0} no longer has ownership",
468        my_name_fmt
469    )
470}
471
472// temporarily no read or write access right to the resource, but eventually
473// the privilege will come back. Occurs when mutably borrowed. This is an invisible line in the timeline.
474pub fn state_resource_revoked(my_name: &String, _to_name: &String) -> String {
475    // update styling
476    let my_name_fmt = fmt_style(my_name);
477    
478    format!(
479        "{0}'s resource is mutably borrowed, so it cannot access the resource",
480        my_name_fmt,
481    )
482}
483
484// This ResourceOwner is the unique object that holds the ownership to the underlying resource.
485pub fn state_full_privilege(my_name: &String) -> String {
486    // update styling
487    let my_name_fmt = fmt_style(my_name);
488    
489    format!(
490        "{0} is the owner of the resource", //not necessarily write if let was used rather than let mut
491        my_name_fmt
492    )
493}
494
495// More than one ResourceOwner has access to the underlying resource
496// This means that it is not possible to create a mutable reference
497// on the next line.
498// About borrow_count: this value is at least one at any time.
499//      When the first static reference of this ResourceOwner is created,
500//          this value is set to 1;
501//      When a new static reference is borrowed from this variable, increment by 1;
502//      When a static reference goes out of scope, decrement this value by 1;
503//      When a decrement happens while the borrow_count is 1, the state becomes
504//          FullPrivilege once again.
505pub fn state_partial_privilege(my_name: &String) -> String {
506    // update styling
507    let my_name_fmt = fmt_style(my_name);
508    
509    format!(
510        "{0}'s resource is being shared by one or more variables",
511        my_name_fmt
512    )
513}
514
515// should not appear for visualization in a correct program
516pub fn state_invalid(my_name: &String) -> String {
517    // update styling
518    let my_name_fmt = fmt_style(my_name);
519    
520    format!(
521        "something is wrong with the timeline of {0}",
522        my_name_fmt
523    )
524}
525
526pub fn structure(my_name: &String) -> String {
527    // update styling
528    let my_name_fmt = fmt_style(my_name);
529    
530    format!(
531        "the components in the box belong to struct {0}",
532        my_name_fmt
533    )
534}