Validate the limit argument
authorAleksey Kladov <aleksey.kladov@gmail.com>
Sat, 10 Mar 2018 14:42:08 +0000 (17:42 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Sat, 10 Mar 2018 15:03:44 +0000 (18:03 +0300)
src/bin/cli.rs
src/bin/command_prelude.rs
src/cargo/ops/registry.rs
src/crates-io/lib.rs

index d24ad2fc3449d6184d500386d937f9bdc77c0e39..f0125b0f81556013a57250ce04d037b0f890ad0c 100644 (file)
@@ -442,8 +442,7 @@ fn execute_subcommand(config: &mut Config, args: ArgMatches) -> CliResult {
         ("search", Some(args)) => {
             let registry = args.registry(config)?;
             let index = args.index(config)?;
-            let limit: Option<u8> = args.value_of("limit")
-                .and_then(|v| v.parse().ok()); //FIXME: validation
+            let limit = args.value_of_u32("limit")?;
             let limit = min(100, limit.unwrap_or(10));
             let query: Vec<&str> = args.values_of("query").unwrap_or_default().collect();
             let query: String = query.join("+");
index 569c8423b8df0ef3822d6a63af27e7772bfb695b..83c6acb470679899b9027e84975748091d0dee75 100644 (file)
@@ -171,6 +171,18 @@ pub fn subcommand(name: &'static str) -> App {
 
 
 pub trait ArgMatchesExt {
+    fn value_of_u32(&self, name: &str) -> CargoResult<Option<u32>> {
+        let arg = match self._value_of(name) {
+            None => None,
+            Some(arg) => Some(arg.parse::<u32>().map_err(|_| {
+                clap::Error::value_validation_auto(
+                    format!("could not parse `{}` as a number", arg)
+                )
+            })?)
+        };
+        Ok(arg)
+    }
+
     fn root_manifest(&self, config: &Config) -> CargoResult<PathBuf> {
         let manifest_path = self._value_of("manifest-path");
         find_root_manifest_for_wd(manifest_path, config.cwd())
@@ -182,15 +194,7 @@ pub trait ArgMatchesExt {
     }
 
     fn jobs(&self) -> CargoResult<Option<u32>> {
-        let jobs = match self._value_of("jobs") {
-            None => None,
-            Some(jobs) => Some(jobs.parse::<u32>().map_err(|_| {
-                clap::Error::value_validation_auto(
-                    format!("could not parse `{}` as a number", jobs)
-                )
-            })?)
-        };
-        Ok(jobs)
+        self.value_of_u32("jobs")
     }
 
     fn target(&self) -> Option<String> {
index 88aba60be0dea7dcd0e74dd2b71d0848c8bb1a93..9edd720f2092f4e17fee3dafbfd06ea7eea4c36f 100644 (file)
@@ -486,7 +486,7 @@ pub fn yank(config: &Config,
 pub fn search(query: &str,
               config: &Config,
               index: Option<String>,
-              limit: u8,
+              limit: u32,
               reg: Option<String>) -> CargoResult<()> {
     fn truncate_with_ellipsis(s: &str, max_width: usize) -> String {
         // We should truncate at grapheme-boundary and compute character-widths,
index 637fa4303980f7b2b63c009812865db51a31b587..a68a5119ad209095ba607b40bd8ea775411cf243 100644 (file)
@@ -202,10 +202,10 @@ impl Registry {
         })
     }
 
-    pub fn search(&mut self, query: &str, limit: u8) -> Result<(Vec<Crate>, u32)> {
-        let formated_query = percent_encode(query.as_bytes(), QUERY_ENCODE_SET);
+    pub fn search(&mut self, query: &str, limit: u32) -> Result<(Vec<Crate>, u32)> {
+        let formatted_query = percent_encode(query.as_bytes(), QUERY_ENCODE_SET);
         let body = self.req(
-            format!("/crates?q={}&per_page={}", formated_query, limit),
+            format!("/crates?q={}&per_page={}", formatted_query, limit),
             None, Auth::Unauthorized
         )?;