Skip to content
This repository has been archived by the owner on Jun 3, 2023. It is now read-only.

Commit

Permalink
Use NaiveDate all the way down
Browse files Browse the repository at this point in the history
  • Loading branch information
mlafeldt committed Aug 31, 2021
1 parent 0fc1bd2 commit f8b72d9
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 18 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ edition = "2018"

[dependencies]
aws-sdk-s3 = { git = "https://github.com/awslabs/aws-sdk-rust", tag = "v0.0.16-alpha", package = "aws-sdk-s3", features = ["rustls"] }
chrono = "0.4"
chrono = { version = "0.4", features = ["serde"] }
derive_builder = "0.10"
futures = "0.3"
lambda_runtime = "0.4"
Expand Down
25 changes: 9 additions & 16 deletions src/bin/get-strip/dilbert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, PartialEq, Debug)]
pub struct Comic {
pub date: String,
pub date: NaiveDate,
pub title: String,
pub image_url: String,
pub strip_url: String,
Expand All @@ -30,8 +30,8 @@ impl Dilbert {
}
}

pub async fn scrape_comic(&self, date: Option<String>) -> Result<Comic, Error> {
let date = Self::date_or_today(date)?;
pub async fn scrape_comic(&self, date: Option<NaiveDate>) -> Result<Comic, Error> {
let date = date.unwrap_or_else(|| Utc::today().naive_utc());
let strip_url = self.strip_url(date);
let resp = reqwest::get(&strip_url).await?.error_for_status()?;
let body = resp.text().await?;
Expand All @@ -43,7 +43,7 @@ impl Dilbert {
.ok_or("comic metadata not found")?;

if container.attr("data-id").unwrap_or_default() != date.to_string() {
return Err("comic not found for date".into());
return Err(format!("no comic found for date {}", date).into());
}

let title = container
Expand All @@ -58,7 +58,7 @@ impl Dilbert {
.to_string();

Ok(Comic {
date: date.to_string(),
date,
title,
image_url,
strip_url,
Expand All @@ -68,13 +68,6 @@ impl Dilbert {
fn strip_url(&self, date: NaiveDate) -> String {
format!("{}/strip/{}", self.base_url, date)
}

fn date_or_today(date: Option<String>) -> Result<NaiveDate, Error> {
match date {
Some(date) => Ok(NaiveDate::parse_from_str(&date, "%Y-%m-%d").or(Err("invalid date format"))?),
None => Ok(Utc::now().naive_utc().date()),
}
}
}

#[cfg(test)]
Expand All @@ -93,7 +86,7 @@ mod tests {
vec![
Test {
comic: Comic {
date: "2000-01-01".to_string(),
date: NaiveDate::from_ymd(2000, 1, 1),
title: "Dilbert Comic for 2000-01-01".to_string(),
image_url: "https://assets.amuniversal.com/bdc8a4d06d6401301d80001dd8b71c47".to_string(),
strip_url: format!("{}/strip/2000-01-01", base_url),
Expand All @@ -102,7 +95,7 @@ mod tests {
},
Test {
comic: Comic {
date: "2018-10-30".to_string(),
date: NaiveDate::from_ymd(2018, 10, 30),
title: "Intentionally Underbidding".to_string(),
image_url: "https://assets.amuniversal.com/cda546d0a88c01365b26005056a9545d".to_string(),
strip_url: format!("{}/strip/2018-10-30", base_url),
Expand All @@ -111,7 +104,7 @@ mod tests {
},
Test {
comic: Comic {
date: "2019-11-02".to_string(),
date: NaiveDate::from_ymd(2019, 11, 2),
title: "Multiple Choice".to_string(),
image_url: "https://assets.amuniversal.com/ce7ec130d6480137c832005056a9545d".to_string(),
strip_url: format!("{}/strip/2019-11-02", base_url),
Expand All @@ -120,7 +113,7 @@ mod tests {
},
Test {
comic: Comic {
date: "2020-11-11".to_string(),
date: NaiveDate::from_ymd(2020, 11, 11),
title: "Elbonian Words".to_string(),
image_url: "https://assets.amuniversal.com/f25312c0fb5b01382ef9005056a9545d".to_string(),
strip_url: format!("{}/strip/2020-11-11", base_url),
Expand Down
3 changes: 2 additions & 1 deletion src/bin/get-strip/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use aws_sdk_s3::{ByteStream, Client};
use chrono::NaiveDate;
use lambda_runtime::{handler_fn, Context, Error};
use log::{debug, info};
use serde::{Deserialize, Serialize};
Expand All @@ -9,7 +10,7 @@ use dilbert::{Comic, Dilbert};

#[derive(Deserialize, Debug)]
struct Input {
date: Option<String>,
date: Option<NaiveDate>,
}

#[derive(Serialize, PartialEq, Debug)]
Expand Down

0 comments on commit f8b72d9

Please sign in to comment.