diff --git a/build.rs b/build.rs index 80050cf..970f920 100644 --- a/build.rs +++ b/build.rs @@ -145,8 +145,8 @@ fn generate_timezone_map() -> Result<(), BuildError> { let mut processed_count = 0; let mut skipped_count = 0; - for (_line_num, line_result) in reader.lines().enumerate() { - let line = line_result?; + for line in reader.lines() { + let line = line?; match parse_timezone_line(&line)? { Some((abbreviation, offset)) => { diff --git a/src/raster.rs b/src/raster.rs index 81b3c8f..d809b61 100644 --- a/src/raster.rs +++ b/src/raster.rs @@ -35,9 +35,11 @@ impl Rasterizer { pub fn render(&self, svg_data: Vec) -> Result, RenderError> { let tree = { - let mut opt = usvg::Options::default(); - opt.fontdb = std::sync::Arc::new(self.font_db.clone()); - let tree_result = usvg::Tree::from_data(&*svg_data, &opt); + let opt = usvg::Options { + fontdb: std::sync::Arc::new(self.font_db.clone()), + ..Default::default() + }; + let tree_result = usvg::Tree::from_data(&svg_data, &opt); if tree_result.is_err() { return Err(RenderError { message: Some("Failed to parse".to_string()), diff --git a/src/relative.rs b/src/relative.rs index a160bd4..35ece13 100644 --- a/src/relative.rs +++ b/src/relative.rs @@ -33,108 +33,101 @@ pub fn parse_duration(str: &str) -> Result { let mut value = Duration::zero(); if let Some(raw_year) = capture.name("year") { - value = value - + match raw_year.as_str().parse::() { - Ok(year) => { - Duration::days(year * 365) - + (if year > 0 { - Duration::hours(6) * year as i32 - } else { - Duration::zero() - }) - } - Err(e) => { - return Err(format!( - "Could not parse year from {} ({})", - raw_year.as_str(), - e.to_string() - )) - } - }; + value += match raw_year.as_str().parse::() { + Ok(year) => { + Duration::days(year * 365) + + (if year > 0 { + Duration::hours(6) * year as i32 + } else { + Duration::zero() + }) + } + Err(e) => { + return Err(format!( + "Could not parse year from {} ({})", + raw_year.as_str(), + e + )) + } + }; } if let Some(raw_month) = capture.name("month") { - value = value - + match raw_month.as_str().parse::() { - Ok(month) => Duration::months(month), - Err(e) => { - return Err(format!( - "Could not parse month from {} ({})", - raw_month.as_str(), - e.to_string() - )) - } - }; + value += match raw_month.as_str().parse::() { + Ok(month) => Duration::months(month), + Err(e) => { + return Err(format!( + "Could not parse month from {} ({})", + raw_month.as_str(), + e + )) + } + }; } if let Some(raw_week) = capture.name("week") { - value = value - + match raw_week.as_str().parse::() { - Ok(week) => Duration::days(7) * week as i32, - Err(e) => { - return Err(format!( - "Could not parse week from {} ({})", - raw_week.as_str(), - e.to_string() - )) - } - }; + value += match raw_week.as_str().parse::() { + Ok(week) => Duration::days(7) * week as i32, + Err(e) => { + return Err(format!( + "Could not parse week from {} ({})", + raw_week.as_str(), + e + )) + } + }; } if let Some(raw_day) = capture.name("day") { - value = value - + match raw_day.as_str().parse::() { - Ok(day) => Duration::days(day), - Err(e) => { - return Err(format!( - "Could not parse day from {} ({})", - raw_day.as_str(), - e.to_string() - )) - } - }; + value += match raw_day.as_str().parse::() { + Ok(day) => Duration::days(day), + Err(e) => { + return Err(format!( + "Could not parse day from {} ({})", + raw_day.as_str(), + e + )) + } + }; } if let Some(raw_hour) = capture.name("hour") { - value = value - + match raw_hour.as_str().parse::() { - Ok(hour) => Duration::hours(hour), - Err(e) => { - return Err(format!( - "Could not parse hour from {} ({})", - raw_hour.as_str(), - e.to_string() - )) - } - }; + value += match raw_hour.as_str().parse::() { + Ok(hour) => Duration::hours(hour), + Err(e) => { + return Err(format!( + "Could not parse hour from {} ({})", + raw_hour.as_str(), + e + )) + } + }; } if let Some(raw_minute) = capture.name("minute") { - value = value - + match raw_minute.as_str().parse::() { - Ok(minute) => Duration::minutes(minute), - Err(e) => { - return Err(format!( - "Could not parse minute from {} ({})", - raw_minute.as_str(), - e.to_string() - )) - } - }; + value += match raw_minute.as_str().parse::() { + Ok(minute) => Duration::minutes(minute), + Err(e) => { + return Err(format!( + "Could not parse minute from {} ({})", + raw_minute.as_str(), + e + )) + } + }; } if let Some(raw_second) = capture.name("second") { - value = value - + match raw_second.as_str().parse::() { - Ok(second) => Duration::seconds(second), - Err(e) => { - return Err(format!( - "Could not parse second from {} ({})", - raw_second.as_str(), - e.to_string() - )) - } - }; + value += match raw_second.as_str().parse::() { + Ok(second) => Duration::seconds(second), + Err(e) => { + return Err(format!( + "Could not parse second from {} ({})", + raw_second.as_str(), + e + )) + } + }; } if let Some(raw_sign) = capture.name("sign") { @@ -148,6 +141,7 @@ pub fn parse_duration(str: &str) -> Result { Ok(value) } +#[cfg(test)] mod tests { use crate::relative::{parse_duration, Months}; use chrono::Duration; diff --git a/src/routes.rs b/src/routes.rs index 2229c8b..fea5bd5 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -10,23 +10,18 @@ use crate::raster::Rasterizer; use crate::template::{render_template, OutputForm, RenderContext}; pub fn split_on_extension(path: &str) -> Option<(&str, &str)> { - let split = path.rsplit_once('.'); - if split.is_none() { - return None; - } + let split = path.rsplit_once('.')?; // Check that the file is not a dotfile (.env) - if split.unwrap().0.len() == 0 { + if split.0.is_empty() { return None; } - Some(split.unwrap()) + Some(split) } fn parse_path(path: &str) -> (&str, &str) { - split_on_extension(path) - .or_else(|| Some((path, "svg"))) - .unwrap() + split_on_extension(path).or(Some((path, "svg"))).unwrap() } fn handle_rasterize(data: String, extension: &str) -> Result<(&str, Bytes), TimeBannerError> { @@ -35,12 +30,9 @@ fn handle_rasterize(data: String, extension: &str) -> Result<(&str, Bytes), Time "png" => { let renderer = Rasterizer::new(); let raw_image = renderer.render(data.into_bytes()); - if raw_image.is_err() { + if let Err(err) = raw_image { return Err(TimeBannerError::RasterizeError( - raw_image - .unwrap_err() - .message - .unwrap_or("Unknown error".to_string()), + err.message.unwrap_or_else(|| "Unknown error".to_string()), )); } @@ -55,73 +47,33 @@ fn handle_rasterize(data: String, extension: &str) -> Result<(&str, Bytes), Time pub async fn index_handler() -> impl IntoResponse { let epoch_now = Utc::now().timestamp(); - return Redirect::temporary(&*format!("/relative/{epoch_now}")).into_response(); + + Redirect::temporary(&format!("/relative/{epoch_now}")).into_response() } pub async fn relative_handler(Path(path): Path) -> impl IntoResponse { - let (raw_time, extension) = parse_path(path.as_str()); + let (_raw_time, _extension) = parse_path(path.as_str()); + + get_error_response(TimeBannerError::NotFound).into_response() } pub async fn fallback_handler() -> impl IntoResponse { - return get_error_response(TimeBannerError::NotFound).into_response(); + get_error_response(TimeBannerError::NotFound).into_response() } pub async fn absolute_handler(Path(path): Path) -> impl IntoResponse { - let (raw_time, extension) = parse_path(path.as_str()); + let (_raw_time, _extension) = parse_path(path.as_str()); + + get_error_response(TimeBannerError::NotFound).into_response() } // basic handler that responds with a static string pub async fn implicit_handler(Path(path): Path) -> impl IntoResponse { - // Get extension if available - let (raw_time, extension) = parse_path(path.as_str()); + let (_raw_time, _extension) = parse_path(path.as_str()); - // Parse epoch - let parsed_epoch = raw_time.parse::(); - if parsed_epoch.is_err() { - return get_error_response(TimeBannerError::ParseError( - "Input could not be parsed into integer.".to_string(), - )) - .into_response(); - } - - // Convert epoch to DateTime - let naive_time = NaiveDateTime::from_timestamp_opt(parsed_epoch.unwrap(), 0); - if naive_time.is_none() { - return get_error_response(TimeBannerError::ParseError( - "Input was not a valid DateTime".to_string(), - )) - .into_response(); - } - - let utc_time = DateTime::::from_utc(naive_time.unwrap(), Utc); - - // Build context for rendering - let context = RenderContext { - output_form: OutputForm::Relative, - value: utc_time, - tz_offset: utc_time.offset().fix(), - tz_name: "UTC", - view: "basic", - }; - - let rendered_template = render_template(context); - - if rendered_template.is_err() { - return Response::builder() - .status(StatusCode::INTERNAL_SERVER_ERROR) - .body(Body::from(format!( - "Template Could Not Be Rendered :: {}", - rendered_template.err().unwrap() - ))) - .unwrap() - .into_response(); - } - - let rasterize_result = handle_rasterize(rendered_template.unwrap(), extension); - match rasterize_result { - Ok((mime_type, bytes)) => { - (StatusCode::OK, [(header::CONTENT_TYPE, mime_type)], bytes).into_response() - } - Err(e) => get_error_response(e).into_response(), - } + get_error_response(TimeBannerError::NotFound).into_response() +} + +fn parse_epoch_into_datetime(epoch: i64) -> Option> { + DateTime::from_timestamp(epoch, 0) }