Skip to content

Commit

Permalink
Auto merge of #2439 - jespino:issue-2402, r=alexcrichton
Browse files Browse the repository at this point in the history
  • Loading branch information
bors committed Mar 6, 2016
2 parents 75412ce + 53c9374 commit 075e824
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/bin/search.rs
Original file line number Diff line number Diff line change
@@ -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
}

Expand All @@ -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<()>> {
Expand All @@ -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)
}
7 changes: 5 additions & 2 deletions src/cargo/ops/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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))
}));

Expand Down
4 changes: 2 additions & 2 deletions src/crates-io/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cargo_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

0 comments on commit 075e824

Please sign in to comment.