@@ -54,7 +54,7 @@ use iron::{self, Handler, status};
54
54
use iron:: headers:: { CacheControl , CacheDirective , ContentType } ;
55
55
use router:: { Router , NoRoute } ;
56
56
use staticfile:: Static ;
57
- use handlebars_iron:: { HandlebarsEngine , DirectorySource } ;
57
+ use handlebars_iron:: { HandlebarsEngine , DirectorySource , SourceError } ;
58
58
use time;
59
59
use postgres:: Connection ;
60
60
use semver:: { Version , VersionReq } ;
@@ -66,6 +66,16 @@ const STATIC_FILE_CACHE_DURATION: u64 = 60 * 60 * 24 * 30 * 12; // 12 months
66
66
const STYLE_CSS : & ' static str = include_str ! ( concat!( env!( "OUT_DIR" ) , "/style.css" ) ) ;
67
67
const OPENSEARCH_XML : & ' static [ u8 ] = include_bytes ! ( "opensearch.xml" ) ;
68
68
69
+ fn handlebars_engine ( ) -> Result < HandlebarsEngine , SourceError > {
70
+ // TODO: Use DocBuilderOptions for paths
71
+ let mut hbse = HandlebarsEngine :: new ( ) ;
72
+ hbse. add ( Box :: new ( DirectorySource :: new ( "./templates" , ".hbs" ) ) ) ;
73
+
74
+ // load templates
75
+ try!( hbse. reload ( ) ) ;
76
+
77
+ Ok ( hbse)
78
+ }
69
79
70
80
struct CratesfyiHandler {
71
81
shared_resource_handler : Box < Handler > ,
@@ -77,14 +87,10 @@ struct CratesfyiHandler {
77
87
78
88
impl CratesfyiHandler {
79
89
fn chain < H : Handler > ( base : H ) -> Chain {
80
- // TODO: Use DocBuilderOptions for paths
81
- let mut hbse = HandlebarsEngine :: new ( ) ;
82
- hbse. add ( Box :: new ( DirectorySource :: new ( "./templates" , ".hbs" ) ) ) ;
83
-
84
- // load templates
85
- if let Err ( e) = hbse. reload ( ) {
86
- panic ! ( "Failed to load handlebar templates: {}" , e. description( ) ) ;
87
- }
90
+ let hbse = match handlebars_engine ( ) {
91
+ Ok ( hbse) => hbse,
92
+ Err ( e) => panic ! ( "Failed to load handlebar templates: {}" , e. description( ) ) ,
93
+ } ;
88
94
89
95
let mut chain = Chain :: new ( base) ;
90
96
chain. link_before ( pool:: Pool :: new ( ) ) ;
@@ -560,4 +566,11 @@ mod test {
560
566
assert_eq ! ( latest_version( & versions, "0.9.0" ) , Some ( "1.1.0" . to_owned( ) ) ) ;
561
567
assert_eq ! ( latest_version( & versions, "invalidversion" ) , None ) ;
562
568
}
569
+
570
+ #[ test]
571
+ fn test_templates_are_valid ( ) {
572
+ if let Err ( e) = handlebars_engine ( ) {
573
+ panic ! ( "Failed to load handlebar templates: {}" , e. description( ) ) ;
574
+ }
575
+ }
563
576
}
0 commit comments