2019-10-30 22:04:32 +01:00
use crate ::commands ::* ;
2019-10-30 23:00:51 +01:00
use serde ::Deserialize ;
2019-10-29 22:24:41 +01:00
use serenity ::model ::channel ::Message ;
2019-10-30 21:05:48 +01:00
use std ::fmt ;
2019-10-29 22:24:41 +01:00
2019-10-30 23:00:51 +01:00
pub fn query_pacman ( ctx : Context , msg : Message , args : Vec < & str > ) {
if args . len ( ) = = 1 {
2019-10-30 21:05:48 +01:00
// try by full package name
2019-10-30 22:04:32 +01:00
let response : Response = search (
& format! (
" https://www.archlinux.org/packages/search/json/?name={} " ,
& args [ 0 ]
) ,
2019-10-31 10:39:28 +01:00
| _e | EMPTY_RESULT ,
2019-10-30 22:04:32 +01:00
) ;
2019-10-30 21:05:48 +01:00
// this is 1 for most packages and 2 if there’s a second version in testing
2019-10-31 12:06:06 +01:00
if response . results . len ( ) ! = 0 {
2019-10-30 21:05:48 +01:00
respond_with_results ( msg . channel_id , & response . results , & ctx ) ;
return ;
2019-10-29 22:24:41 +01:00
}
}
2019-10-30 21:05:48 +01:00
let query = args . join ( " " ) ;
2019-10-30 22:04:32 +01:00
let response : Response = search (
& format! (
" https://www.archlinux.org/packages/search/json/?q={} " ,
& query
) ,
2019-10-31 10:39:28 +01:00
| _e | EMPTY_RESULT ,
2019-10-30 22:04:32 +01:00
) ;
2019-10-31 11:45:16 +01:00
if response . results . is_empty ( ) {
2019-10-30 21:05:48 +01:00
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 ( ) ,
} ;
2019-10-30 23:00:51 +01:00
#[ derive(Deserialize) ]
2019-10-29 22:24:41 +01:00
struct Response {
results : Vec < Package > ,
}
2019-10-30 23:00:51 +01:00
#[ derive(Deserialize) ]
2019-10-29 22:24:41 +01:00
struct Package {
pkgname : String ,
2019-10-30 23:00:51 +01:00
//pkgbase: String,
2019-10-29 22:24:41 +01:00
repo : String ,
pkgver : String ,
pkgdesc : String ,
2019-10-30 23:00:51 +01:00
//url: String,
//compressed_size: i64,
//installed_size: i64,
//build_date: String,
//last_update: String,
//flag_date: Option<String>,
2019-10-29 22:24:41 +01:00
}
2019-10-30 21:05:48 +01:00
impl fmt ::Display for Package {
fn fmt ( & self , f : & mut fmt ::Formatter < '_ > ) -> fmt ::Result {
write! (
f ,
" {}/{} {} \n {} " ,
self . repo , self . pkgname , self . pkgver , self . pkgdesc
)
}
}