Add xbps queries

This commit is contained in:
kageru 2019-11-02 09:41:34 +01:00
parent 193d93a0bf
commit 01891ee6ef
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
2 changed files with 63 additions and 0 deletions

View File

@ -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

61
src/commands/xbps.rs Normal file
View File

@ -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<Package>,
}
#[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
)
}
}