From 01891ee6efe7876bb6cd2aec5cbc5124c32e6d40 Mon Sep 17 00:00:00 2001 From: kageru Date: Sat, 2 Nov 2019 09:41:34 +0100 Subject: [PATCH] Add xbps queries --- src/commands/mod.rs | 2 ++ src/commands/xbps.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/commands/xbps.rs diff --git a/src/commands/mod.rs b/src/commands/mod.rs index de60fe5..2d2ae03 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -8,6 +8,7 @@ mod dnf; mod nix; mod pacman; mod aur; +mod xbps; extern crate reqwest; pub struct Handler; @@ -66,6 +67,7 @@ lazy_static! { command_list.push(Command::new("nix", nix::query_nix)); command_list.push(Command::new("dnf", dnf::query_dnf)); command_list.push(Command::new("aur", aur::query_aur)); + command_list.push(Command::new("xbps", xbps::query_xbps)); command_list.push(Command::new("pacbot help", help)); command_list.push(Command::new("pb help", help)); command_list diff --git a/src/commands/xbps.rs b/src/commands/xbps.rs new file mode 100644 index 0000000..cde02bf --- /dev/null +++ b/src/commands/xbps.rs @@ -0,0 +1,61 @@ +use crate::commands::*; +use serde::Deserialize; +use serenity::model::channel::Message; +use std::fmt; + +pub fn query_xbps(ctx: Context, msg: Message, args: Vec<&str>) { + if args.len() == 1 { + // try by exact package name + let response: SingleResponse = search( + &format!("http://127.0.0.1:8197/v1/packages/x86_64/{}", &args[0]), + |_e| Default::default(), + ); + if response.data.name != "" { + respond_with_results(msg.channel_id, &[response.data], &ctx); + return; + } + } + // fall back to regular queries + let query = args.join(" "); + let response: QueryResponse = search( + &format!("http://127.0.0.1:8197/v1/query/x86_64?q={}", &query), + |_e| unreachable!(), + ); + if response.data.is_empty() { + send(msg.channel_id, "No results", &ctx); + return; + } + respond_with_results(msg.channel_id, &response.data, &ctx); +} + +#[derive(Deserialize, Default)] +struct SingleResponse { + data: Package, +} + +#[derive(Deserialize)] +struct QueryResponse { + data: Vec, +} + +#[derive(Deserialize, Default)] +struct Package { + name: String, + version: String, + short_desc: String, +} + +impl fmt::Display for Package { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + // We want to pad the name+version to correctly align the description, + // but that only seems to be possible if they already are one string, + // so we format them with a second `format!` call. + // There must be a better way. + "[ ] {: <30} {}", + &format!("{}–{}", self.name, self.version), + self.short_desc + ) + } +}