Rust library to generate static websites
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor: logging code

+69 -91
+67 -89
crates/framework/src/lib.rs
··· 55 55 info!(target: "SKIP_FORMAT", "{}", " generating pages ".on_green().bold()); 56 56 let pages_start = SystemTime::now(); 57 57 58 + let route_format_options = FormatElapsedTimeOptions { 59 + additional_fn: Some(Box::new(|msg: ColoredString| { 60 + let formatted_msg = format!("(+{})", msg); 61 + if msg.fgcolor.is_none() { 62 + formatted_msg.dimmed() 63 + } else { 64 + formatted_msg.into() 65 + } 66 + })), 67 + ..Default::default() 68 + }; 69 + 70 + let section_format_options = FormatElapsedTimeOptions { 71 + sec_red_threshold: 5, 72 + sec_yellow_threshold: 1, 73 + millis_red_threshold: None, 74 + millis_yellow_threshold: None, 75 + ..Default::default() 76 + }; 77 + 58 78 for route in &router.routes { 59 79 let routes = route.routes(); 60 80 match routes.is_empty() { ··· 64 84 params: HashMap::new(), 65 85 }; 66 86 67 - let file_path = PathBuf::from_str("./dist/") 68 - .unwrap() 69 - .join(route.file_path(ctx.params.clone())); 70 - 71 - // Create the parent directories if it doesn't exist 72 - let parent_dir = Path::new(file_path.parent().unwrap()); 73 - fs::create_dir_all(parent_dir)?; 74 - 75 - // Create file 76 - let mut file = File::create(file_path.clone()).unwrap(); 77 - 78 - let rendered = route.render(&ctx); 79 - match rendered { 80 - page::RenderResult::Html(html) => { 81 - file.write_all(html.into_string().as_bytes()).unwrap(); 82 - } 83 - page::RenderResult::Text(text) => { 84 - file.write_all(text.as_bytes()).unwrap(); 85 - } 86 - } 87 + let (file_path, file) = create_route_file(&**route, ctx.params.clone())?; 88 + render_route(file, &**route, &ctx)?; 87 89 88 - let formatted_elasped_time = format_elapsed_time( 89 - route_start.elapsed(), 90 - FormatElapsedTimeOptions { 91 - additional_fn: Some(Box::new(|msg: ColoredString| { 92 - let formatted_msg = format!("(+{})", msg); 93 - if msg.fgcolor.is_none() { 94 - formatted_msg.dimmed() 95 - } else { 96 - formatted_msg.into() 97 - } 98 - })), 99 - ..Default::default() 100 - }, 101 - )?; 90 + let formatted_elasped_time = 91 + format_elapsed_time(route_start.elapsed(), &route_format_options)?; 102 92 info!(target: "build", "{} -> {} {}", route.route(ctx.params), file_path.to_string_lossy().dimmed(), formatted_elasped_time); 103 93 } 104 94 false => { ··· 108 98 let route_start = SystemTime::now(); 109 99 let ctx = RouteContext { params }; 110 100 111 - let file_path = PathBuf::from_str("./dist/") 112 - .unwrap() 113 - .join(route.file_path(ctx.params.clone())); 114 - 115 - // Create the parent directories if it doesn't exist 116 - let parent_dir = Path::new(file_path.parent().unwrap()); 117 - fs::create_dir_all(parent_dir).unwrap(); 118 - 119 - // Create file 120 - let mut file = File::create(file_path.clone()).unwrap(); 121 - 122 - let rendered = route.render(&ctx); 123 - match rendered { 124 - page::RenderResult::Html(html) => { 125 - file.write_all(html.into_string().as_bytes()).unwrap(); 126 - } 127 - page::RenderResult::Text(text) => { 128 - file.write_all(text.as_bytes()).unwrap(); 129 - } 130 - } 101 + let (file_path, file) = create_route_file(&**route, ctx.params.clone()).unwrap(); 102 + render_route(file, &**route, &ctx).unwrap(); 131 103 132 - let formatted_elasped_time = format_elapsed_time( 133 - route_start.elapsed(), 134 - FormatElapsedTimeOptions { 135 - additional_fn: Some(Box::new(|msg: ColoredString| { 136 - let formatted_msg = format!("(+{})", msg); 137 - if msg.fgcolor.is_none() { 138 - formatted_msg.dimmed() 139 - } else { 140 - formatted_msg.into() 141 - } 142 - })), 143 - ..Default::default() 144 - }, 145 - ).unwrap(); 104 + let formatted_elasped_time = format_elapsed_time(route_start.elapsed(), &route_format_options).unwrap(); 146 105 info!(target: "build", "├─ {} {}", file_path.to_string_lossy().dimmed(), formatted_elasped_time); 147 106 }); 148 107 } 149 108 } 150 109 } 151 110 152 - let formatted_elasped_time = format_elapsed_time( 153 - pages_start.elapsed(), 154 - FormatElapsedTimeOptions { 155 - sec_red_threshold: 5, 156 - sec_yellow_threshold: 1, 157 - millis_red_threshold: None, 158 - millis_yellow_threshold: None, 159 - ..Default::default() 160 - }, 161 - )?; 111 + let formatted_elasped_time = 112 + format_elapsed_time(pages_start.elapsed(), &section_format_options)?; 162 113 info!(target: "build", "{}", format!("Pages generated in {}", formatted_elasped_time).bold()); 163 114 164 115 // Check if static directory exists ··· 170 121 copy_recursively("./static", "./dist")?; 171 122 172 123 let formatted_elasped_time = 173 - format_elapsed_time(assets_start.elapsed(), FormatElapsedTimeOptions::default())?; 124 + format_elapsed_time(assets_start.elapsed(), &FormatElapsedTimeOptions::default())?; 174 125 info!(target: "build", "{}", format!("Assets copied in {}", formatted_elasped_time).bold()); 175 126 } 176 127 177 - let formatted_elasped_time = format_elapsed_time( 178 - build_start.elapsed(), 179 - FormatElapsedTimeOptions { 180 - sec_red_threshold: 5, 181 - sec_yellow_threshold: 1, 182 - millis_red_threshold: None, 183 - millis_yellow_threshold: None, 184 - ..Default::default() 185 - }, 186 - )?; 128 + let formatted_elasped_time = 129 + format_elapsed_time(build_start.elapsed(), &section_format_options)?; 187 130 info!(target: "build", "{}", format!("Build completed in {}", formatted_elasped_time).bold()); 188 131 189 132 Ok(()) ··· 202 145 } 203 146 Ok(()) 204 147 } 148 + 149 + fn create_route_file( 150 + route: &dyn page::FullPage, 151 + params: HashMap<String, String>, 152 + ) -> Result<(PathBuf, File), Box<dyn std::error::Error>> { 153 + let file_path = PathBuf::from_str("./dist/") 154 + .unwrap() 155 + .join(route.file_path(params.clone())); 156 + 157 + // Create the parent directories if it doesn't exist 158 + let parent_dir = Path::new(file_path.parent().unwrap()); 159 + fs::create_dir_all(parent_dir)?; 160 + 161 + // Create file 162 + let file = File::create(file_path.clone())?; 163 + 164 + Ok((file_path, file)) 165 + } 166 + 167 + fn render_route( 168 + mut file: File, 169 + route: &dyn page::FullPage, 170 + ctx: &RouteContext, 171 + ) -> Result<(), Box<dyn std::error::Error>> { 172 + let rendered = route.render(ctx); 173 + match rendered { 174 + page::RenderResult::Html(html) => { 175 + file.write_all(html.into_string().as_bytes())?; 176 + } 177 + page::RenderResult::Text(text) => { 178 + file.write_all(text.as_bytes())?; 179 + } 180 + } 181 + Ok(()) 182 + }
+2 -2
crates/framework/src/logging.rs
··· 23 23 24 24 pub fn format_elapsed_time( 25 25 elapsed: Result<Duration, SystemTimeError>, 26 - options: FormatElapsedTimeOptions, 26 + options: &FormatElapsedTimeOptions, 27 27 ) -> Result<ColoredString, SystemTimeError> { 28 28 let elapsed = match elapsed { 29 29 Ok(elapsed) => elapsed, ··· 54 54 }, 55 55 }; 56 56 57 - if let Some(additional_fn) = options.additional_fn { 57 + if let Some(additional_fn) = &options.additional_fn { 58 58 Ok(additional_fn(result)) 59 59 } else { 60 60 Ok(result)