From d5620442b4b9b5a016c748e7a41011a8e2d5751e Mon Sep 17 00:00:00 2001 From: Tim Hoiberg Date: Mon, 3 Jul 2023 23:06:47 +0900 Subject: [PATCH 1/2] Accept &str instead of String Because String can be coerced to &str using &str in the argument type creates a more flexible api. Unfortunately, it ruins the simpler syntax of just passing the method name to map, there might be a nicer way to automatically reference the value, but I don't know about it at the moment. --- backend/src/api/bunpro/request.rs | 6 +++--- backend/src/api/wanikani/request.rs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/backend/src/api/bunpro/request.rs b/backend/src/api/bunpro/request.rs index f59ed79..b84c76c 100644 --- a/backend/src/api/bunpro/request.rs +++ b/backend/src/api/bunpro/request.rs @@ -26,11 +26,11 @@ async fn get_review_queue() -> anyhow::Result { .await? .text() .await - .map(serialize_response)? + .map(|body| serialize_response(&body))? } -fn serialize_response(body: String) -> anyhow::Result { - let json = serde_json::from_str(body.as_str())?; +fn serialize_response(body: &str) -> anyhow::Result { + let json = serde_json::from_str(body)?; Ok(json) } diff --git a/backend/src/api/wanikani/request.rs b/backend/src/api/wanikani/request.rs index 41b333e..26e78f5 100644 --- a/backend/src/api/wanikani/request.rs +++ b/backend/src/api/wanikani/request.rs @@ -27,11 +27,11 @@ async fn get_summary_data() -> anyhow::Result { .await? .text() .await - .map(deserialize_response)? + .map(|body| deserialize_response(&body))? } -fn deserialize_response(response_body: String) -> anyhow::Result { - let json_data = serde_json::from_str(response_body.as_str())?; +fn deserialize_response(response_body: &str) -> anyhow::Result { + let json_data = serde_json::from_str(response_body)?; Ok(json_data) } From 6451339e9a6b897b43ae35a1a84fc6c501e53aa7 Mon Sep 17 00:00:00 2001 From: Tim Hoiberg Date: Mon, 3 Jul 2023 23:10:25 +0900 Subject: [PATCH 2/2] Update tests to use the correct types --- backend/src/api/bunpro/request.rs | 4 ++-- backend/src/api/wanikani/request.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/backend/src/api/bunpro/request.rs b/backend/src/api/bunpro/request.rs index b84c76c..b195799 100644 --- a/backend/src/api/bunpro/request.rs +++ b/backend/src/api/bunpro/request.rs @@ -42,13 +42,13 @@ mod test_super { #[test] fn test_bunpro_with_reviews() { let with_reviews = include_str!("./fixtures/bunpro_with_reviews.json"); - let response = serialize_response(String::from(with_reviews)); + let response = serialize_response(with_reviews); assert!(response.is_ok()); } #[test] fn test_bunpro_with_no_reviews() { let with_no_reviews = include_str!("./fixtures/bunpro_with_no_reviews.json"); - assert!(serialize_response(String::from(with_no_reviews)).is_ok()); + assert!(serialize_response(with_no_reviews).is_ok()); } } diff --git a/backend/src/api/wanikani/request.rs b/backend/src/api/wanikani/request.rs index 26e78f5..18e24bc 100644 --- a/backend/src/api/wanikani/request.rs +++ b/backend/src/api/wanikani/request.rs @@ -44,7 +44,7 @@ mod test_super { fn test_can_deserialize_empty_reviews() { let response_data = include_str!("./fixtures/wanikani_with_no_reviews.json"); - let response = deserialize_response(response_data.into()); + let response = deserialize_response(response_data); assert!(response.is_ok()); } @@ -53,7 +53,7 @@ mod test_super { fn test_can_deserialize_with_reviews() { let response_data = include_str!("./fixtures/wanikani_with_reviews.json"); - let response = deserialize_response(response_data.into()); + let response = deserialize_response(response_data); assert!(response.is_ok()); }