From 0662c351e1e7adb33e47e146d1910386e694bce3 Mon Sep 17 00:00:00 2001 From: kageru Date: Wed, 30 Oct 2019 22:04:32 +0100 Subject: [PATCH] Generify query and output --- src/commands/mod.rs | 34 +++++++++++++++++++++++++ src/commands/pacman.rs | 56 +++++++++++------------------------------- 2 files changed, 49 insertions(+), 41 deletions(-) diff --git a/src/commands/mod.rs b/src/commands/mod.rs index b20d8ea..ab60e9a 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,6 +1,10 @@ use serenity::model::channel::Message; +use std::fmt; +use serenity::model::id::ChannelId; +use serde::de::DeserializeOwned; use serenity::prelude::*; mod pacman; +extern crate reqwest; pub struct Handler; pub type CommandHandler = dyn Fn(Context, Message); @@ -45,3 +49,33 @@ impl Command { } } } + +pub fn send(target: ChannelId, message: &str, ctx: &Context) { + if let Err(cause) = target.say(&ctx.http, message) { + println!("Could not send message: {}", cause); + } +} + +pub fn respond_with_results(target: ChannelId, results: &Vec, ctx: &Context) { + send( + target, + &format!( + "```{}```", + results + .into_iter() + .take(5) + .map(|p| format!("{}\n", p)) + .collect::() + ), + ctx, + ); +} + +pub fn search(url: &str, fallback: T) -> T { + return search_inner(url).unwrap_or(fallback); +} + +fn search_inner(url: &str) -> Result { + let resp = reqwest::get(url)?.json::()?; + Ok(resp) +} diff --git a/src/commands/pacman.rs b/src/commands/pacman.rs index 5ae55bd..2d57d9c 100644 --- a/src/commands/pacman.rs +++ b/src/commands/pacman.rs @@ -1,9 +1,7 @@ +use crate::commands::*; use serde::{Deserialize, Serialize}; use serenity::model::channel::Message; -use serenity::model::id::ChannelId; -use serenity::prelude::*; use std::fmt; -extern crate reqwest; pub fn query_pacman(ctx: Context, msg: Message) { let mut args = msg.content.split_whitespace().collect::>(); @@ -19,10 +17,13 @@ pub fn query_pacman(ctx: Context, msg: Message) { } if args.len() == 1 { // try by full package name - let response = search(&format!( - "https://www.archlinux.org/packages/search/json/?name={}", - &args[0] - )); + let response: Response = search( + &format!( + "https://www.archlinux.org/packages/search/json/?name={}", + &args[0] + ), + EMPTY_RESULT, + ); // this is 1 for most packages and 2 if there’s a second version in testing if response.results.len() != 0 { respond_with_results(msg.channel_id, &response.results, &ctx); @@ -30,10 +31,13 @@ pub fn query_pacman(ctx: Context, msg: Message) { } } let query = args.join(" "); - let response = search(&format!( - "https://www.archlinux.org/packages/search/json/?q={}", - &query - )); + let response: Response = search( + &format!( + "https://www.archlinux.org/packages/search/json/?q={}", + &query + ), + EMPTY_RESULT, + ); if response.results.len() == 0 { send(msg.channel_id, "No results", &ctx); return; @@ -41,36 +45,6 @@ pub fn query_pacman(ctx: Context, msg: Message) { respond_with_results(msg.channel_id, &response.results, &ctx); } -fn respond_with_results(target: ChannelId, results: &Vec, ctx: &Context) { - send( - target, - &format!( - "```{}```", - results - .into_iter() - .take(5) - .map(|p| format!("{}\n", p)) - .collect::() - ), - ctx, - ); -} - -fn send(target: ChannelId, message: &str, ctx: &Context) { - if let Err(cause) = target.say(&ctx.http, message) { - println!("Could not send message: {}", cause); - } -} - -fn search(url: &str) -> Response { - return search_inner(url).unwrap_or(EMPTY_RESULT); -} - -fn search_inner(url: &str) -> Result { - let resp = reqwest::get(url)?.json::()?; - Ok(resp) -} - const EMPTY_RESULT: Response = Response { version: 0, limit: 0,