From 1ba083af3576d20b5c1fc1c6ebacefd91ceb6636 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 4 Jan 2020 13:15:00 +0100 Subject: abconfig: reorg + parse result into struct. --- src/abconfig.rs | 21 +++++++++++++-------- src/error.rs | 7 +++++++ src/lib.rs | 3 +-- 3 files changed, 21 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/abconfig.rs b/src/abconfig.rs index 7957dea..294467a 100644 --- a/src/abconfig.rs +++ b/src/abconfig.rs @@ -15,15 +15,20 @@ // along with this program. If not, see . use crate::{client::Client, error::Error}; +use serde::Deserialize; -pub struct ABConfig; - -pub fn abconfig() -> ABConfig { - ABConfig {} +#[derive(Debug, Deserialize)] +pub struct ABConfig { + pub id: u32, + pub chan: u32, + pub xchan: String, + pub cat: String, + pub k: String, + pub v: String, } -impl ABConfig { - pub fn fetch(&self, client: &Client) -> Result { - client.fetch_stream("abconfig", &()) - } +pub fn fetch(client: &Client) -> Result, Error> { + let payload = client.fetch_stream("abconfig", &())?; + Ok(serde_json::from_str(&payload)?) } + diff --git a/src/error.rs b/src/error.rs index 1e349d9..9559ae6 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), + Json(serde_json::Error), Qs(serde_qs::Error), Unauthorized, Unknown, @@ -38,6 +39,12 @@ impl From for Error { } } +impl From for Error { + fn from(e: serde_json::Error) -> Error { + Error::Json(e) + } +} + impl From for Error { fn from(e: serde_qs::Error) -> Error { Error::Qs(e) diff --git a/src/lib.rs b/src/lib.rs index 12396ca..7a17269 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -mod abconfig; +pub mod abconfig; mod abook; mod channel_stream; mod client; @@ -24,7 +24,6 @@ mod item; mod network_stream; mod xchan; -pub use abconfig::abconfig; pub use abook::abook; pub use channel_stream::channel_stream; pub use client::*; -- cgit v1.2.3