]> git.proxmox.com Git - cargo.git/commitdiff
Add limit parameter to search command (fix issue #2402)
authorJesús Espino <jespinog@gmail.com>
Fri, 4 Mar 2016 17:34:30 +0000 (18:34 +0100)
committerJesús Espino <jespinog@gmail.com>
Sun, 6 Mar 2016 16:46:32 +0000 (17:46 +0100)
src/bin/search.rs
src/cargo/ops/registry.rs
src/crates-io/lib.rs
tests/test_cargo_search.rs

index fa8d792c2c195056418fb5a48baba5531721ac65..8b8c010e676072ee5f4dafbdb8dabd0e0859befa 100644 (file)
@@ -1,12 +1,15 @@
 use cargo::ops;
 use cargo::util::{CliResult, Config};
 
+use std::cmp;
+
 #[derive(RustcDecodable)]
 pub struct Options {
     flag_host: Option<String>,
     flag_verbose: Option<bool>,
     flag_quiet: Option<bool>,
     flag_color: Option<String>,
+    flag_limit: Option<u32>,
     arg_query: String
 }
 
@@ -23,6 +26,7 @@ Options:
     -v, --verbose            Use verbose output
     -q, --quiet              No output printed to stdout
     --color WHEN             Coloring: auto, always, never
+    --limit LIMIT            Limit the number of results (default: 10, max: 100)
 ";
 
 pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
@@ -31,10 +35,11 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
                                 &options.flag_color));
     let Options {
         flag_host: host,
+        flag_limit: limit,
         arg_query: query,
         ..
     } = options;
 
-    try!(ops::search(&query, config, host));
+    try!(ops::search(&query, config, host, cmp::min(100, limit.unwrap_or(10)) as u8));
     Ok(None)
 }
index 0b2f83e18a6886ade73a80d672a6e640c113aef1..da2d800220e6c5ea4620be493ddef0235db65fd4 100644 (file)
@@ -340,7 +340,10 @@ pub fn yank(config: &Config,
     Ok(())
 }
 
-pub fn search(query: &str, config: &Config, index: Option<String>) -> CargoResult<()> {
+pub fn search(query: &str,
+              config: &Config,
+              index: Option<String>,
+              limit: u8) -> CargoResult<()> {
     fn truncate_with_ellipsis(s: &str, max_length: usize) -> String {
         if s.len() < max_length {
             s.to_string()
@@ -350,7 +353,7 @@ pub fn search(query: &str, config: &Config, index: Option<String>) -> CargoResul
     }
 
     let (mut registry, _) = try!(registry(config, None, index));
-    let crates = try!(registry.search(query).map_err(|e| {
+    let crates = try!(registry.search(query, limit).map_err(|e| {
         human(format!("failed to retrieve search results from the registry: {}", e))
     }));
 
index ea19883be6b0e90db483deb1cce4c260282a780f..0586c76f2fcd15986f521094237240ebbe01817b 100644 (file)
@@ -170,8 +170,8 @@ impl Registry {
         Ok(())
     }
 
-    pub fn search(&mut self, query: &str) -> Result<Vec<Crate>> {
-        let body = try!(self.req(format!("/crates?q={}", query), None, Get,
+    pub fn search(&mut self, query: &str, limit: u8) -> Result<Vec<Crate>> {
+        let body = try!(self.req(format!("/crates?q={}&per_page={}", query, limit), None, Get,
                                  Auth::Unauthorized));
 
         Ok(json::decode::<Crates>(&body).unwrap().crates)
index db754ffe279ebd9c03a61ff9e16a705da5a49fd9..d4c4838722dcf6e27a66b560fcb98ec82ba48bf0 100644 (file)
@@ -76,7 +76,7 @@ test!(simple {
     // from source there anyway!
     File::create(&base).unwrap().write_all(contents.as_bytes()).unwrap();
     if !cfg!(windows) {
-        File::create(&base.with_file_name("crates?q=postgres")).unwrap()
+        File::create(&base.with_file_name("crates?q=postgres&per_page=10")).unwrap()
              .write_all(contents.as_bytes()).unwrap();
     }