From 54b1855db583b5b1907d867d7998f605319a9d07 Mon Sep 17 00:00:00 2001 From: kageru Date: Thu, 31 Oct 2019 22:55:29 +0100 Subject: [PATCH] Add aur queries --- src/commands/aur.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++ src/commands/mod.rs | 2 ++ 2 files changed, 57 insertions(+) create mode 100644 src/commands/aur.rs diff --git a/src/commands/aur.rs b/src/commands/aur.rs new file mode 100644 index 0000000..9c42895 --- /dev/null +++ b/src/commands/aur.rs @@ -0,0 +1,55 @@ +use crate::commands::*; +use serde::Deserialize; +use serenity::model::channel::Message; +use std::fmt; + +pub fn query_aur(ctx: Context, msg: Message, args: Vec<&str>) { + let query = args.join(" "); + let response: Response = search( + &format!( + "https://aur.archlinux.org/rpc/?v=5&type=search&by=name&arg={}", + &query + ), + |_e| EMPTY_RESULT, + ); + if response.results.is_empty() { + send(msg.channel_id, "No results", &ctx); + return; + } + respond_with_results(msg.channel_id, &response.results, &ctx); +} + +const EMPTY_RESULT: Response = Response { + results: Vec::new(), +}; + +#[derive(Deserialize)] +struct Response { + results: Vec, +} + +#[derive(Deserialize)] +struct Package { + #[serde(rename = "Name")] + name: String, + #[serde(rename = "Version")] + version: String, + #[serde(rename = "Description")] + description: String, + #[serde(rename = "URL")] + url: String, + #[serde(rename = "NumVotes")] + votes: u16, + //Popularity 3.159745 + //OutOfDate null +} + +impl fmt::Display for Package { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "aur/{} {} (+{})\n {}\n url: {}", + self.name, self.version, self.votes, self.description, self.url + ) + } +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index c30a678..de60fe5 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -7,6 +7,7 @@ mod apt; mod dnf; mod nix; mod pacman; +mod aur; extern crate reqwest; pub struct Handler; @@ -64,6 +65,7 @@ lazy_static! { command_list.push(Command::new("apt", apt::query_apt)); 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("pacbot help", help)); command_list.push(Command::new("pb help", help)); command_list