|
1 | 1 | #![allow(clippy::disallowed_names)] // For the use of `foo` in test cases
|
2 | 2 |
|
| 3 | +use std::cell::{RefCell, RefMut}; |
3 | 4 | use std::collections::HashMap;
|
| 5 | +use std::fmt; |
| 6 | +use std::pin::Pin; |
| 7 | +use std::sync::{Arc, Mutex, MutexGuard}; |
4 | 8 |
|
5 | 9 | use rinja::Template;
|
| 10 | +use rinja::filters::HtmlSafe; |
6 | 11 |
|
7 | 12 | #[derive(Template)]
|
8 | 13 | #[template(path = "simple.html")]
|
@@ -560,3 +565,31 @@ struct SplitTemplateDeclaration;
|
560 | 565 | fn test_split_template_declaration() {
|
561 | 566 | assert_eq!(SplitTemplateDeclaration.to_string(), "🙂");
|
562 | 567 | }
|
| 568 | + |
| 569 | +#[test] |
| 570 | +fn test_ref_custom_type() { |
| 571 | + const TEXT: &str = "&this <is >not 'safe"; |
| 572 | + |
| 573 | + struct MySafeType; |
| 574 | + |
| 575 | + impl HtmlSafe for MySafeType {} |
| 576 | + |
| 577 | + impl fmt::Display for MySafeType { |
| 578 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 579 | + f.write_str(TEXT) |
| 580 | + } |
| 581 | + } |
| 582 | + |
| 583 | + #[derive(Template)] |
| 584 | + #[template(ext = "html", source = "{{ data }}")] |
| 585 | + struct MyTemplate<'a, 'b, 'c> { |
| 586 | + data: &'a mut RefMut<'b, Pin<Arc<MutexGuard<'c, MySafeType>>>>, |
| 587 | + } |
| 588 | + |
| 589 | + let mutex = Mutex::new(MySafeType); |
| 590 | + let cell = RefCell::new(Arc::pin(mutex.try_lock().unwrap())); |
| 591 | + let tmpl = MyTemplate { |
| 592 | + data: &mut cell.try_borrow_mut().unwrap(), |
| 593 | + }; |
| 594 | + assert_eq!(tmpl.render().unwrap(), TEXT); |
| 595 | +} |
0 commit comments