From b587f8e7fb9eb64f204afcf5511708cfd02cb502 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Thu, 28 Oct 2021 15:19:24 +0200 Subject: [PATCH] Replaced the json crate with serde_json Signed-off-by: Louis Vallat --- Cargo.lock | 8 +------- Cargo.toml | 2 +- src/albums.rs | 6 +++--- src/main.rs | 2 +- src/session.rs | 16 ++++++++-------- 5 files changed, 14 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6ac2380..05d3fb7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -406,12 +406,6 @@ version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4" -[[package]] -name = "json" -version = "0.12.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "078e285eafdfb6c4b434e0d31e8cfcb5115b651496faca5749b88fafd4f23bfd" - [[package]] name = "lazy_static" version = "1.4.0" @@ -451,9 +445,9 @@ dependencies = [ "dirs", "hyper", "hyper-tls", - "json", "mockito", "serde", + "serde_json", "tokio", "tokio-test", "toml", diff --git a/Cargo.toml b/Cargo.toml index 9738979..6f9ae3a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,12 +10,12 @@ hyper = { version = "0.14", features = ["full"] } tokio = { version = "1", features = ["full"] } tokio-test = "0.4.2" hyper-tls = "0.5.0" -json = "0.12.4" clap = "2.33.3" dirs = "4.0.0" cookie = "0.15.1" toml = "0.5.8" serde = { version = "1", features = ["derive"] } +serde_json = "1" [dev-dependencies] mockito = "0.30.0" diff --git a/src/albums.rs b/src/albums.rs index 438befc..0618585 100644 --- a/src/albums.rs +++ b/src/albums.rs @@ -1,9 +1,9 @@ use hyper::{Body, Method, Request, header::{AUTHORIZATION, COOKIE}}; -use json::JsonValue; +use serde_json::{Value, json}; use crate::{LycheeClient, body_to_str}; -pub async fn get_albums(c: &LycheeClient, lychee_session: &str) -> JsonValue { +pub async fn get_albums(c: &LycheeClient, lychee_session: &str) -> Value { let req = Request::builder() .method(Method::POST) .uri(c.endpoint.to_string() + "/api/Albums::get") @@ -12,5 +12,5 @@ pub async fn get_albums(c: &LycheeClient, lychee_session: &str) -> JsonValue { .body(Body::empty()) .expect("Cannot request /api/Albums::get."); let res = c.client.request(req).await.unwrap(); - return json::parse(body_to_str(res.into_body()).await.as_str()).unwrap(); + return json!(body_to_str(res.into_body()).await); } diff --git a/src/main.rs b/src/main.rs index 97e7952..86623b6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -76,7 +76,7 @@ async fn main() -> Result<(), Box> { logout(&client, &lychee_session_cookie).await; } else if let Some(matches) = matches.subcommand_matches("albums") { if matches.is_present("get") { - println!("{}", get_albums(&client, &lychee_session_cookie).await.pretty(4)); + println!("{}", get_albums(&client, &lychee_session_cookie).await); } else { App::print_long_help(&mut app).unwrap(); } diff --git a/src/session.rs b/src/session.rs index 95799ac..b83b42c 100644 --- a/src/session.rs +++ b/src/session.rs @@ -1,17 +1,17 @@ use hyper::{Body, Method, Request, header::{AUTHORIZATION, CONTENT_TYPE, COOKIE, SET_COOKIE}}; -use json::object; +use serde_json::json; use crate::{LycheeClient, body_to_str}; pub async fn login(client: &LycheeClient, login: &str, password: &str) -> String { - let login_data = object! { "username": login, "password": password }; + let login_data = json!({ "username": login, "password": password }); let req = Request::builder() .method(Method::POST) .uri(client.endpoint.clone() + "/api/Session::login") .header(AUTHORIZATION, client.api_key.clone()) .header(CONTENT_TYPE, "application/json") - .body(Body::from(login_data.dump())) + .body(Body::from(login_data.to_string())) .expect("Cannot request Session::login."); let res = client.client.request(req).await.unwrap(); let headers = res.headers().get_all(SET_COOKIE) @@ -39,7 +39,7 @@ pub async fn logout(client: &LycheeClient, lychee_session: &str) { mod session_tests { use hyper_tls::HttpsConnector; use hyper::Client; - use json::object; + use serde_json::json; use mockito::mock; use crate::{LycheeClient, session::{login, logout}}; @@ -143,24 +143,24 @@ mod session_tests { #[test] fn login_check_username_password() { - let body1 = object!{ "username": "username", "password": "password" }; + let body1 = json!({ "username": "username", "password": "password" }); let client = setup(); let m1 = mock("POST", "/api/Session::login") .with_header("content-type", "application/json") .with_header("set-cookie", "lychee_session=demo") .with_body("true") - .match_body(body1.dump().as_str()) + .match_body(body1.to_string().as_str()) .create(); tokio_test::block_on(login(&client, "username", "password")); m1.assert(); - let body2 = object!{ "username": "u", "password": "p" }; + let body2 = json!({ "username": "u", "password": "p" }); let client = setup(); let m2 = mock("POST", "/api/Session::login") .with_header("content-type", "application/json") .with_header("set-cookie", "lychee_session=demo") .with_body("true") - .match_body(body2.dump().as_str()) + .match_body(body2.to_string().as_str()) .create(); tokio_test::block_on(login(&client, "u", "p")); m2.assert();