From ad3874f8bf5b5451e910ceae02518c04695e63c6 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Wed, 1 Jan 2020 20:07:03 +0100 Subject: Replace serde_urlencoded with serde_qs. Simplifies serialization of various types quite a bit. --- Cargo.toml | 2 +- src/client.rs | 14 +++++++------- src/error.rs | 7 +++++++ src/group.rs | 18 +++++------------- src/lib.rs | 1 - src/xchan.rs | 18 ++++-------------- 6 files changed, 24 insertions(+), 36 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ca98051..8d63325 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ edition = "2018" [dependencies] reqwest = "0.9.1" serde = { version = "1.0", features = ["derive"] } -serde_urlencoded = "0.5.1" +serde_qs = "0.5.2" [dev-dependencies] base64 = "0.11.0" diff --git a/src/client.rs b/src/client.rs index 507f845..3a20133 100644 --- a/src/client.rs +++ b/src/client.rs @@ -28,7 +28,6 @@ use reqwest::{ StatusCode, }; use serde::Serialize; -use serde_urlencoded; use std::collections::BTreeMap; use std::io::Read; @@ -92,10 +91,10 @@ impl Client { } fn url(&self, path: &str, args: &T) -> String - where T: Serialize + where T: Serialize + std::fmt::Debug { let r = self.base_url.clone() + path; - if let Ok(a) = serde_urlencoded::to_string(args) { + if let Ok(a) = serde_qs::to_string(dbg!(args)) { r + "?" + &a } else { @@ -104,7 +103,7 @@ impl Client { } pub fn fetch_stream(&self, path: &str, args: &T) -> Result - where T: Serialize + where T: Serialize + std::fmt::Debug { let url = dbg!(self.url(path, args)); let res = self.inner.get(&url) @@ -116,14 +115,15 @@ impl Client { } pub fn post_data(&self, path: &str, data: &T) -> Result - where T: Serialize, + where T: Serialize + std::fmt::Debug, { let url = dbg!(self.url(path, &())); - let res = self.inner.post(&url) + let res = dbg!(self.inner.post(&url) .header(ACCEPT, "application/json") .header(CONTENT_TYPE, "application/x-www-form-urlencoded") .basic_auth(self.user.clone(), Some(self.pw.clone())) - .form(&data) + .body(serde_qs::to_string(&data)?)) + //.form(&data)) .send()?; handle_result(res) diff --git a/src/error.rs b/src/error.rs index d4f6b96..1e349d9 100644 --- a/src/error.rs +++ b/src/error.rs @@ -21,6 +21,7 @@ use std; pub enum Error { Http(reqwest::Error), Io(std::io::Error), + Qs(serde_qs::Error), Unauthorized, Unknown, } @@ -36,3 +37,9 @@ impl From for Error { Error::Io(e) } } + +impl From for Error { + fn from(e: serde_qs::Error) -> Error { + Error::Qs(e) + } +} diff --git a/src/group.rs b/src/group.rs index c01d5ae..43c4d65 100644 --- a/src/group.rs +++ b/src/group.rs @@ -18,7 +18,7 @@ use crate::{ client::{self, Client}, error::Error, }; -use serde::{Serialize, Serializer}; +use serde::Serialize; pub struct GroupFetcher<'a> { client: &'a Client, @@ -35,21 +35,13 @@ impl<'a> GroupFetcher<'a> { } +#[derive(Debug, Serialize)] enum GroupSelector<'a> { + #[serde(rename(serialize = "group_id"))] Id(u64), - Name(&'a str), -} -impl<'a> Serialize for GroupSelector<'a> { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - match *self { - GroupSelector::Id(id) => [("group_id", id)].serialize(serializer), - GroupSelector::Name(n) => [("group_name", n)].serialize(serializer), - } - } + #[serde(rename(serialize = "group_name"))] + Name(&'a str), } pub struct GroupMembersFetcher<'a> { diff --git a/src/lib.rs b/src/lib.rs index d636be7..0ef8d33 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,7 +16,6 @@ extern crate reqwest; extern crate serde; -extern crate serde_urlencoded; mod abconfig; mod abook; diff --git a/src/xchan.rs b/src/xchan.rs index c67179c..68f7622 100644 --- a/src/xchan.rs +++ b/src/xchan.rs @@ -18,27 +18,17 @@ use crate::{ client::{self, Client}, error::Error, }; -use serde::{Serialize, Serializer}; +//use serde::{Serialize, Serializer}; +use serde::Serialize; +#[derive(Debug, Serialize)] +#[serde(rename_all = "lowercase")] enum XChanSelector<'a> { Address(&'a str), Hash(&'a str), GUID(&'a str), } -impl<'a> Serialize for XChanSelector<'a> { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - match *self { - XChanSelector::Address(addr) => [("address", addr)].serialize(serializer), - XChanSelector::Hash(hash) => [("hash", hash)].serialize(serializer), - XChanSelector::GUID(guid) => [("guid", guid)].serialize(serializer), - } - } -} - pub struct XChanFetcher<'a> { client: &'a Client, data: Option>, -- cgit v1.2.3