From 16a56f12cd2e66e10f8bb5e2c4026d3a7148e680 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Thu, 2 Jan 2020 21:10:55 +0100 Subject: Cargo fmt --- src/abconfig.rs | 4 +--- src/abook.rs | 4 +--- src/client.rs | 61 ++++++++++++++++++++++++++++++++++----------------------- src/group.rs | 4 ++-- src/item.rs | 18 +++++++++-------- src/lib.rs | 3 +-- src/xchan.rs | 8 +++----- 7 files changed, 55 insertions(+), 47 deletions(-) (limited to 'src') diff --git a/src/abconfig.rs b/src/abconfig.rs index 78eec4b..f1622c7 100644 --- a/src/abconfig.rs +++ b/src/abconfig.rs @@ -25,9 +25,7 @@ pub struct ABConfigFetcher<'a> { impl<'a> ABConfigFetcher<'a> { pub fn new(client: &'a Client) -> ABConfigFetcher<'a> { - ABConfigFetcher { - client, - } + ABConfigFetcher { client } } pub fn fetch(&self) -> Result { diff --git a/src/abook.rs b/src/abook.rs index f7469e3..bcedf81 100644 --- a/src/abook.rs +++ b/src/abook.rs @@ -25,9 +25,7 @@ pub struct AbookFetcher<'a> { impl<'a> AbookFetcher<'a> { pub fn new(client: &'a Client) -> AbookFetcher<'a> { - AbookFetcher { - client, - } + AbookFetcher { client } } pub fn fetch(&self) -> Result { diff --git a/src/client.rs b/src/client.rs index 3a20133..ab021fb 100644 --- a/src/client.rs +++ b/src/client.rs @@ -15,11 +15,11 @@ // along with this program. If not, see . use crate::{ + abconfig::ABConfigFetcher, + abook::AbookFetcher, error::Error, group::{GroupFetcher, GroupMembersFetcher}, item::ItemBuilder, - abook::AbookFetcher, - abconfig::ABConfigFetcher, xchan::XChanFetcher, }; use reqwest::{ @@ -31,14 +31,14 @@ use serde::Serialize; use std::collections::BTreeMap; use std::io::Read; -pub const ZOTAPI_ABOOK_PATH : &str = "/api/z/1.0/abook"; -pub const ZOTAPI_ABCONFIG_PATH : &str = "/api/z/1.0/abconfig"; -pub const ZOTAPI_CHANNEL_STREAM_PATH : &str = "/api/z/1.0/channel/stream"; -pub const ZOTAPI_NETWORK_STREAM_PATH : &str = "/api/z/1.0/network/stream"; -pub const ZOTAPI_GROUP_PATH : &str = "/api/z/1.0/group"; -pub const ZOTAPI_GROUP_MEMBERS_PATH : &str = "/api/z/1.0/group_members"; -pub const ZOTAPI_ITEM_UPDATE_PATH : &str = "/api/z/1.0/item/update"; -pub const ZOTAPI_XCHAN_PATH : &str = "/api/z/1.0/xchan"; +pub const ZOTAPI_ABOOK_PATH: &str = "/api/z/1.0/abook"; +pub const ZOTAPI_ABCONFIG_PATH: &str = "/api/z/1.0/abconfig"; +pub const ZOTAPI_CHANNEL_STREAM_PATH: &str = "/api/z/1.0/channel/stream"; +pub const ZOTAPI_NETWORK_STREAM_PATH: &str = "/api/z/1.0/network/stream"; +pub const ZOTAPI_GROUP_PATH: &str = "/api/z/1.0/group"; +pub const ZOTAPI_GROUP_MEMBERS_PATH: &str = "/api/z/1.0/group_members"; +pub const ZOTAPI_ITEM_UPDATE_PATH: &str = "/api/z/1.0/item/update"; +pub const ZOTAPI_XCHAN_PATH: &str = "/api/z/1.0/xchan"; #[derive(Debug)] pub struct Client { @@ -91,22 +91,25 @@ impl Client { } fn url(&self, path: &str, args: &T) -> String - where T: Serialize + std::fmt::Debug + where + T: Serialize + std::fmt::Debug, { let r = self.base_url.clone() + path; if let Ok(a) = serde_qs::to_string(dbg!(args)) { r + "?" + &a - } - else { + } else { r } } pub fn fetch_stream(&self, path: &str, args: &T) -> Result - where T: Serialize + std::fmt::Debug + where + T: Serialize + std::fmt::Debug, { let url = dbg!(self.url(path, args)); - let res = self.inner.get(&url) + let res = self + .inner + .get(&url) .header(ACCEPT, "application/json") .basic_auth(self.user.clone(), Some(self.pw.clone())) .send()?; @@ -115,22 +118,31 @@ impl Client { } pub fn post_data(&self, path: &str, data: &T) -> Result - where T: Serialize + std::fmt::Debug, + where + T: Serialize + std::fmt::Debug, { let url = dbg!(self.url(path, &())); - let res = dbg!(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())) .body(serde_qs::to_string(&data)?)) - //.form(&data)) - .send()?; + //.form(&data)) + .send()?; handle_result(res) } - pub fn post_multipart(&self, path: &str, data: &BTreeMap<&str, T>, files: &Vec<&str>) -> Result - where T: ToString, + pub fn post_multipart( + &self, + path: &str, + data: &BTreeMap<&str, T>, + files: &Vec<&str>, + ) -> Result + where + T: ToString, { let url = dbg!(self.url(path, &())); let mut form = reqwest::multipart::Form::new(); @@ -143,7 +155,9 @@ impl Client { form = form.file("media", f).unwrap(); } - let res = self.inner.post(&url) + let res = self + .inner + .post(&url) .basic_auth(self.user.clone(), Some(self.pw.clone())) .multipart(form) .send()?; @@ -162,11 +176,10 @@ fn handle_result(mut res: reqwest::Response) -> Result { let mut body = String::new(); res.read_to_string(&mut body)?; Ok(body) - }, + } _ => { eprintln!("Received unknown status: {:?}", res.status()); Err(Error::Unknown) } } } - diff --git a/src/group.rs b/src/group.rs index 43c4d65..98d83d2 100644 --- a/src/group.rs +++ b/src/group.rs @@ -34,7 +34,6 @@ impl<'a> GroupFetcher<'a> { } } - #[derive(Debug, Serialize)] enum GroupSelector<'a> { #[serde(rename(serialize = "group_id"))] @@ -67,6 +66,7 @@ impl<'a> GroupMembersFetcher<'a> { pub fn fetch(&self) -> Result { self.client.fetch_stream( client::ZOTAPI_GROUP_MEMBERS_PATH, - &self.id.as_ref().unwrap()) + &self.id.as_ref().unwrap(), + ) } } diff --git a/src/item.rs b/src/item.rs index 5507577..3d9ad1e 100644 --- a/src/item.rs +++ b/src/item.rs @@ -21,7 +21,6 @@ use crate::{ use serde::Serialize; use std::collections::BTreeMap; - /// Data type for values that an Item can hold. #[derive(Debug, Serialize)] #[serde(untagged)] @@ -85,8 +84,8 @@ fn convert_itemdata_list_with_one_member_to_a_string() { /// ``` #[derive(Debug)] pub struct ItemBuilder<'a> { - client : &'a Client, - data : BTreeMap<&'a str, ItemData<'a>>, + client: &'a Client, + data: BTreeMap<&'a str, ItemData<'a>>, files: Vec<&'a str>, } @@ -119,7 +118,9 @@ impl<'a> ItemBuilder<'a> { /// Set groups allowed to access item pub fn group_allow(&mut self, group: &'a str) -> &mut ItemBuilder<'a> { - let groups_allow = self.data.entry("groups_allow") + let groups_allow = self + .data + .entry("groups_allow") .or_insert(ItemData::List(Vec::<&str>::new())); groups_allow.push(group); @@ -130,10 +131,11 @@ impl<'a> ItemBuilder<'a> { pub fn create(&self) -> Result { dbg!(self); if self.files.is_empty() { - self.client.post_data(client::ZOTAPI_ITEM_UPDATE_PATH, &self.data) - } - else { - self.client.post_multipart(client::ZOTAPI_ITEM_UPDATE_PATH, &self.data, &self.files) + self.client + .post_data(client::ZOTAPI_ITEM_UPDATE_PATH, &self.data) + } else { + self.client + .post_multipart(client::ZOTAPI_ITEM_UPDATE_PATH, &self.data, &self.files) } } } diff --git a/src/lib.rs b/src/lib.rs index e9b6bf9..eb5d897 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,8 +25,7 @@ mod xchan; pub use client::Client; pub use error::Error; -pub fn client(url: &str, user: &str, pw: &str) -> Client -{ +pub fn client(url: &str, user: &str, pw: &str) -> Client { Client::new(url, user, pw) } diff --git a/src/xchan.rs b/src/xchan.rs index 68f7622..9b920d5 100644 --- a/src/xchan.rs +++ b/src/xchan.rs @@ -36,10 +36,7 @@ pub struct XChanFetcher<'a> { impl<'a> XChanFetcher<'a> { pub fn new(client: &'a Client) -> XChanFetcher<'a> { - XChanFetcher { - client, - data: None, - } + XChanFetcher { client, data: None } } pub fn by_address(&mut self, addr: &'a str) -> &mut XChanFetcher<'a> { @@ -58,6 +55,7 @@ impl<'a> XChanFetcher<'a> { } pub fn fetch(&self) -> Result { - self.client.fetch_stream(client::ZOTAPI_XCHAN_PATH, &self.data.as_ref().unwrap()) + self.client + .fetch_stream(client::ZOTAPI_XCHAN_PATH, &self.data.as_ref().unwrap()) } } -- cgit v1.2.3