pacbot/src/commands/aur.rs

65 lines
1.6 KiB
Rust
Raw Permalink Normal View History

2019-10-31 22:55:29 +01:00
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(" ");
2020-06-09 20:28:19 +02:00
let mut response: Response = search(
2019-10-31 22:55:29 +01:00
&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;
}
2020-06-09 20:28:19 +02:00
response.results.sort_by(|a, b| {
2020-06-09 20:53:28 +02:00
b.popularity
.partial_cmp(&a.popularity)
2020-06-09 20:28:19 +02:00
.unwrap_or(std::cmp::Ordering::Less)
});
2019-10-31 22:55:29 +01:00
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>,
2019-10-31 22:55:29 +01:00
#[serde(rename = "URL")]
2020-06-09 20:46:49 +02:00
url: Option<String>,
2019-10-31 22:55:29 +01:00
#[serde(rename = "NumVotes")]
votes: u16,
2020-06-09 20:28:19 +02:00
#[serde(rename = "Popularity")]
popularity: f32,
2019-10-31 22:55:29 +01:00
}
impl fmt::Display for Package {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"aur/{} {} (+{})\n {}\n url: {}",
2020-06-09 20:46:49 +02:00
self.name,
self.version,
self.votes,
self.description.clone().unwrap_or(String::from("none")),
self.url.clone().unwrap_or(String::from("none")),
2019-10-31 22:55:29 +01:00
)
}
}