pacbot/src/commands/aur.rs

65 lines
1.6 KiB
Rust

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 mut 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;
}
response.results.sort_by(|a, b| {
b.popularity
.partial_cmp(&a.popularity)
.unwrap_or(std::cmp::Ordering::Less)
});
respond_with_results(msg.channel_id, &response.results, &ctx);
}
const EMPTY_RESULT: Response = Response {
results: Vec::new(),
};
#[derive(Deserialize)]
struct Response {
results: Vec<Package>,
}
#[derive(Deserialize)]
struct Package {
#[serde(rename = "Name")]
name: String,
#[serde(rename = "Version")]
version: String,
#[serde(rename = "Description")]
description: Option<String>,
#[serde(rename = "URL")]
url: Option<String>,
#[serde(rename = "NumVotes")]
votes: u16,
#[serde(rename = "Popularity")]
popularity: f32,
}
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.clone().unwrap_or(String::from("none")),
self.url.clone().unwrap_or(String::from("none")),
)
}
}