diff options
Diffstat (limited to 'src/abconfig.rs')
-rw-r--r-- | src/abconfig.rs | 36 |
1 files changed, 20 insertions, 16 deletions
diff --git a/src/abconfig.rs b/src/abconfig.rs index 723747c..6348030 100644 --- a/src/abconfig.rs +++ b/src/abconfig.rs @@ -20,7 +20,7 @@ use serde::Deserialize; /// A struct for storing a key value pair with a category in /// relation to a contact. Typically used to store permissions /// a given contact has on a given channel. -#[derive(Debug, Deserialize)] +#[derive(Debug, Default, Deserialize)] pub struct ABConfig { /// Database ID for this entry pub id: u32, @@ -41,30 +41,34 @@ pub struct ABConfig { pub v: String, } -/// Fetch ABConfig for all contacts -pub fn fetch(client: &Client) -> Result<Vec<ABConfig>, Error> { - let payload = client.fetch_stream("abconfig", &())?; - Ok(serde_json::from_str(&payload)?) +impl ABConfig { + pub fn z() -> ABConfigRequest { + ABConfigRequest::default() + } } #[derive(Default)] pub struct ABConfigRequest { - abook_id: u32, + abook_id: Option<u32>, } impl ABConfigRequest { + /// Limit to ABConfigs for a given contact + /// + /// `abook` is the abook id of the given contact + pub fn with_abook_id(mut self, abook: u32) -> ABConfigRequest { + self.abook_id = Some(abook); + self + } + pub fn fetch(&self, client: &Client) -> Result<Vec<ABConfig>, Error> { - let mut res = client.get("abconfig") - .query(&[("abook_id", self.abook_id.to_string())]) - .send()?; + let mut req = client.get("abconfig"); - Ok(serde_json::from_str(&res.text()?)?) + if let Some(id) = self.abook_id { + req = req.query(&[("abook_id", id.to_string())]); + } + + Ok(serde_json::from_str(&req.send()?.text()?)?) } } -/// Fetch ABConfig for a given contact -/// -/// `abook` is the abook id of the given contact -pub fn with_abook_id(abook: u32) -> ABConfigRequest { - ABConfigRequest { abook_id: abook } -} |