aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2020-01-01 20:07:03 +0100
committerHarald Eilertsen <haraldei@anduin.net>2020-01-01 20:07:03 +0100
commitad3874f8bf5b5451e910ceae02518c04695e63c6 (patch)
tree7bda5499c4894dea763c3077383721131e55d993 /src
parentaf92ce5991aee0f6bd9ae6f84f9c668d5407ba09 (diff)
downloadrust-zotapi-ad3874f8bf5b5451e910ceae02518c04695e63c6.tar.gz
rust-zotapi-ad3874f8bf5b5451e910ceae02518c04695e63c6.tar.bz2
rust-zotapi-ad3874f8bf5b5451e910ceae02518c04695e63c6.zip
Replace serde_urlencoded with serde_qs.
Simplifies serialization of various types quite a bit.
Diffstat (limited to 'src')
-rw-r--r--src/client.rs14
-rw-r--r--src/error.rs7
-rw-r--r--src/group.rs18
-rw-r--r--src/lib.rs1
-rw-r--r--src/xchan.rs18
5 files changed, 23 insertions, 35 deletions
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<T>(&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<T>(&self, path: &str, args: &T) -> Result<String, Error>
- 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<T>(&self, path: &str, data: &T) -> Result<String, Error>
- 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<std::io::Error> for Error {
Error::Io(e)
}
}
+
+impl From<serde_qs::Error> 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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
- 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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
- 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<XChanSelector<'a>>,