@@ -17,14 +17,15 @@ use highlight::highlight;
1717use io:: { generate_id, get_paste, store_paste} ;
1818use params:: { HostHeader , IsPlaintextRequest } ;
1919
20- use askama:: { MarkupDisplay , Template } ;
20+ use askama:: { Html as AskamaHtml , MarkupDisplay , Template } ;
2121
2222use rocket:: http:: { ContentType , Status } ;
2323use rocket:: request:: Form ;
2424use rocket:: response:: content:: { Content , Html } ;
2525use rocket:: response:: Redirect ;
2626use rocket:: Data ;
2727
28+ use std:: borrow:: Cow ;
2829use std:: io:: Read ;
2930
3031///
@@ -39,7 +40,7 @@ struct Index;
3940fn index ( ) -> Result < Html < String > , Status > {
4041 Index
4142 . render ( )
42- . map ( |h| Html ( h ) )
43+ . map ( Html )
4344 . map_err ( |_| Status :: InternalServerError )
4445}
4546
@@ -82,8 +83,8 @@ fn submit_raw(input: Data, host: HostHeader) -> std::io::Result<String> {
8283
8384#[ derive( Template ) ]
8485#[ template( path = "paste.html" ) ]
85- struct ShowPaste {
86- content : MarkupDisplay < String > ,
86+ struct ShowPaste < ' a > {
87+ content : MarkupDisplay < AskamaHtml , Cow < ' a , String > > ,
8788}
8889
8990#[ get( "/<key>" ) ]
@@ -94,22 +95,24 @@ fn show_paste(key: String, plaintext: IsPlaintextRequest) -> Result<Content<Stri
9495
9596 // get() returns a read-only lock, we're not going to be writing to this key
9697 // again so we can hold this for as long as we want
97- let entry = get_paste ( key) . ok_or_else ( || Status :: NotFound ) ?;
98+ let entry = & * get_paste ( key) . ok_or_else ( || Status :: NotFound ) ?;
9899
99100 if * plaintext {
100- Ok ( Content ( ContentType :: Plain , entry) )
101+ Ok ( Content ( ContentType :: Plain , entry. to_string ( ) ) )
101102 } else {
102103 let content = match ext {
103- None => MarkupDisplay :: Unsafe ( entry) ,
104- Some ( extension) => highlight ( & entry, extension)
105- . map ( |h| MarkupDisplay :: Safe ( h) )
106- . ok_or_else ( || Status :: NotFound ) ?,
104+ Some ( extension) => match highlight ( & entry, extension) {
105+ Some ( html) => MarkupDisplay :: new_safe ( Cow :: Owned ( html) , AskamaHtml ) ,
106+ None => return Err ( Status :: NotFound ) ,
107+ } ,
108+ None => MarkupDisplay :: new_unsafe ( Cow :: Borrowed ( entry) , AskamaHtml ) ,
107109 } ;
108110
109- ShowPaste { content }
110- . render ( )
111- . map ( |html| Content ( ContentType :: HTML , html) )
112- . map_err ( |_| Status :: InternalServerError )
111+ let template = ShowPaste { content } ;
112+ match template. render ( ) {
113+ Ok ( html) => Ok ( Content ( ContentType :: HTML , html) ) ,
114+ Err ( _) => Err ( Status :: InternalServerError ) ,
115+ }
113116 }
114117}
115118
0 commit comments