Repository: AlexEne/twiggy (Rust)

Issue:
Change `svelte top`'s sort-by option to be a boolean flag
Right now it is `--sort-by (shallow|retained)` with default to `shallow`. But that means that one would never do `--sort-by shallow`, so this option only exists to enable sorting by retained size. So it should just be `-r/--retained` instead of the current behavior.

This mostly involves changing the option parsing in `opt/opt.rs`.


Discussion:
we are already using `-r` for retained_path, we can use the long path (`--retained`) alone here 



Patch:
diff --git a/analyze/analyze.rs b/analyze/analyze.rs
index 12752bcd..c7f48a38 100644
--- a/analyze/analyze.rs
+++ b/analyze/analyze.rs
@@ -126,9 +126,10 @@ impl traits::Emit for Top {
     ) -> Result<(), failure::Error> {
         let mut dest = dest.open().context("could not open output destination")?;
 
-        let sort_label = match self.opts.sort_by {
-            opt::SortBy::Shallow => "Shallow",
-            opt::SortBy::Retained => "Retained",
+        let sort_label = if self.opts.retained {
+            "Retained"
+        } else {
+            "Shallow"
         };
 
         let mut table = Table::with_header(vec![
@@ -140,9 +141,10 @@ impl traits::Emit for Top {
         for &id in &self.items {
             let item = &items[id];
 
-            let size = match self.opts.sort_by {
-                opt::SortBy::Shallow => item.size(),
-                opt::SortBy::Retained => items.retained_size(id),
+            let size = if self.opts.retained {
+                items.retained_size(id)
+            } else {
+                item.size()
             };
 
             let size_percent = (size as f64) / (items.size() as f64) * 100.0;
@@ -164,7 +166,7 @@ pub fn top(items: &mut ir::Items, opts: &opt::Top) -> Result<Box<traits::Emit>,
         bail!("retaining paths are not yet implemented");
     }
 
-    if opts.sort_by == opt::SortBy::Retained {
+    if opts.retained {
         items.compute_retained_sizes();
     }
 
@@ -173,9 +175,9 @@ pub fn top(items: &mut ir::Items, opts: &opt::Top) -> Result<Box<traits::Emit>,
         .filter(|item| item.id() != items.meta_root())
         .collect();
 
-    top_items.sort_unstable_by(|a, b| match opts.sort_by {
-        opt::SortBy::Shallow => b.size().cmp(&a.size()),
-        opt::SortBy::Retained => items
+    top_items.sort_unstable_by(|a, b| match opts.retained {
+        false => b.size().cmp(&a.size()),
+        true => items
             .retained_size(b.id())
             .cmp(&items.retained_size(a.id())),
     });
diff --git a/opt/opt.rs b/opt/opt.rs
index 0babecf6..d3b4f66c 100755
--- a/opt/opt.rs
+++ b/opt/opt.rs
@@ -92,9 +92,9 @@ pub struct Top {
     #[structopt(short = "r", long = "retaining-paths")]
     pub retaining_paths: bool,
 
-    /// Choose how to sort the list. Choices are "shallow" or "retained".
-    #[structopt(short = "s", long = "sort-by", default_value = "shallow")]
-    pub sort_by: SortBy,
+    /// Sort list by retained
+    #[structopt(long = "retained")]
+    pub retained: bool,
 }
 
 impl CommonOptions for Top {
@@ -175,25 +175,6 @@ impl CommonOptions for Paths {
     }
 }
 
-/// Whether to sort by shallow or retained size.
-#[derive(Clone, Debug, PartialEq, Eq)]
-pub enum SortBy {
-    Shallow,
-    Retained,
-}
-
-impl FromStr for SortBy {
-    type Err = failure::Error;
-
-    fn from_str(s: &str) -> Result<SortBy, failure::Error> {
-        match s {
-            "shallow" => Ok(SortBy::Shallow),
-            "retained" => Ok(SortBy::Retained),
-            _ => bail!("unknown sort order: '{}'", s),
-        }
-    }
-}
-
 /// Where to output results.
 #[derive(Clone, Debug)]
 pub enum OutputDestination {


Test patch:
diff --git a/svelte/tests/tests.rs b/svelte/tests/tests.rs
index ee2c49a8..91b32766 100644
--- a/svelte/tests/tests.rs
+++ b/svelte/tests/tests.rs
@@ -85,8 +85,7 @@ test!(
     "top",
     "-n",
     "10",
-    "-s",
-    "retained",
+    "--retained",
     "./fixtures/wee_alloc.wasm"
 );
 test!(
@@ -94,8 +93,7 @@ test!(
     "top",
     "-n",
     "10",
-    "-s",
-    "retained",
+    "--retained",
     "./fixtures/mappings.wasm"
 );