lib.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // lib.rs
  3. // Copyright (C) 2018 Paul Schaefer <schaefer.pp@gmail.com>
  4. // Distributed under terms of the LGPL-3.0+ license.
  5. //
  6. extern crate clap;
  7. extern crate ini;
  8. extern crate libalpm;
  9. extern crate reqwest;
  10. #[macro_use]
  11. extern crate serde_derive;
  12. extern crate serde;
  13. extern crate serde_json;
  14. pub mod aur;
  15. pub mod alpm;
  16. pub use aur::*;
  17. use std::fmt;
  18. #[derive(Debug)]
  19. pub enum Error {
  20. ConfigError(ini::ini::Error),
  21. HttpError(reqwest::Error),
  22. AlpmError(libalpm::Error),
  23. FormatError(serde_json::error::Error),
  24. AurError(String),
  25. GenericError(String),
  26. }
  27. pub enum Package {
  28. ALPM(alpm::RepoPackage),
  29. AUR(aur::AurPackage),
  30. }
  31. impl fmt::Debug for Package {
  32. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  33. match self {
  34. Package::ALPM(pkg) => write!(f, "Package::ALPM {:?}", pkg),
  35. Package::AUR(pkg) => write!(f, "Package::AUR {:?}", pkg),
  36. }
  37. }
  38. }
  39. pub fn search(query: &str) -> Result<Vec<Package>, Error> {
  40. let repopkgs = alpm::Context::new()?.search(query);
  41. let aurpkgs = aur::search(query, aur::SearchType::NameDesc)?;
  42. let mut res: Vec<Package> = Vec::new();
  43. repopkgs.into_iter().map(|pkg| Package::ALPM(pkg)).for_each(|pkg| res.push(pkg));
  44. aurpkgs.into_iter().map(|pkg| Package::AUR(pkg)).for_each(|pkg| res.push(pkg));
  45. // res.append(.collect());
  46. Ok(res)
  47. }
  48. #[cfg(test)]
  49. mod tests {
  50. use super::*;
  51. #[test]
  52. fn it_works() {}
  53. }