Skip to main content

Merge pull request #12 from alexwlchan/formatting

ID
977c9a8
date
2022-04-03 22:50:07+00:00
author
Alex Chan <alex@alexwlchan.net>
parents
8b28219, 8c70761
message
Merge pull request #12 from alexwlchan/formatting

Run autoformatting over the codebase; check it in CI
changed files
3 files, 86 additions, 63 deletions

Changed files

.github/workflows/build.yml (388) → .github/workflows/build.yml (514)

diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index fa1f47a..ad4367f 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -16,3 +16,8 @@ jobs:
         uses: actions-rs/cargo@v1
         with:
           command: test
+      - name: check formatting
+        uses: actions-rs/cargo@v1
+        with:
+          command: fmt
+          args: --check

src/get_bytes.rs (3921) → src/get_bytes.rs (3819)

diff --git a/src/get_bytes.rs b/src/get_bytes.rs
index 0072217..530459c 100644
--- a/src/get_bytes.rs
+++ b/src/get_bytes.rs
@@ -1,8 +1,8 @@
 use std::fs::File;
 
-use image::{AnimationDecoder, Frame, DynamicImage};
 use image::codecs::gif::GifDecoder;
 use image::imageops::FilterType;
+use image::{AnimationDecoder, DynamicImage, Frame};
 
 pub fn get_bytes_for_image(path: &str) -> Vec<u8> {
     let img = match image::open(&path) {
@@ -10,7 +10,7 @@ pub fn get_bytes_for_image(path: &str) -> Vec<u8> {
         Err(e) => {
             eprintln!("{}", e);
             std::process::exit(1);
-        },
+        }
     };
 
     // Resize the image after we open it.  For this tool I'd rather get a good answer
@@ -39,7 +39,7 @@ pub fn get_bytes_for_gif(path: &str) -> Vec<u8> {
         Err(e) => {
             eprintln!("{}", e);
             std::process::exit(1);
-        },
+        }
     };
 
     let decoder = GifDecoder::new(f).ok().unwrap();
@@ -53,11 +53,7 @@ pub fn get_bytes_for_gif(path: &str) -> Vec<u8> {
     //
     // For that reason, we select a sample of up to 50 frames and use those
     // as the basis for analysis.
-    let frames: Vec<Frame> =
-        decoder
-            .into_frames()
-            .collect_frames()
-            .unwrap();
+    let frames: Vec<Frame> = decoder.into_frames().collect_frames().unwrap();
 
     // How this works: it tells us we should be looking at the nth frame.
     // Examples:
@@ -78,14 +74,11 @@ pub fn get_bytes_for_gif(path: &str) -> Vec<u8> {
         ((frames.len() as f32) / (25 as f32)) as i32
     };
 
-    let selected_frames =
-        frames
-            .iter()
-            .enumerate()
-            .filter(|(i, _)| {
-                (*i as f32 / nth_frame as f32).floor() == (*i as f32 / nth_frame as f32)
-            })
-            .map(|(_, frame)| frame);
+    let selected_frames = frames
+        .iter()
+        .enumerate()
+        .filter(|(i, _)| (*i as f32 / nth_frame as f32).floor() == (*i as f32 / nth_frame as f32))
+        .map(|(_, frame)| frame);
 
     // Now we go through the frames and extract all the pixels.  The k-means
     // process doesn't care about position, so we can concatenate the pixels
@@ -98,12 +91,12 @@ pub fn get_bytes_for_gif(path: &str) -> Vec<u8> {
     let resize = if frames.len() == 1 { 400 } else { 100 };
 
     selected_frames
-        .map(|frame|
+        .map(|frame| {
             DynamicImage::ImageRgba8(frame.buffer().clone())
                 .resize(resize, resize, FilterType::Nearest)
                 .into_rgba8()
                 .into_raw()
-        )
+        })
         .into_iter()
         .flatten()
         .collect()

src/main.rs (8523) → src/main.rs (8732)

diff --git a/src/main.rs b/src/main.rs
index 4e419ed..bf7a924 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -12,31 +12,30 @@ mod get_bytes;
 const VERSION: &str = env!("CARGO_PKG_VERSION");
 
 fn main() {
-    let matches =
-        App::new("dominant_colours")
-            .version(VERSION)
-            .author("Alex Chan <alex@alexwlchan.net>")
-            .about("Find the dominant colours in an image")
-            .arg(
-                Arg::with_name("PATH")
-                    .help("path to the image to inspect")
-                    .required(true)
-                    .index(1)
-            )
-            .arg(
-                Arg::with_name("MAX-COLOURS")
-                    .long("max-colours")
-                    .help("how many colours to find")
-                    .default_value("5")
-                    .takes_value(true)
-            )
-            .arg(
-                Arg::with_name("no-palette")
-                    .long("no-palette")
-                    .help("Just print the hex values, not colour previews")
-                    .takes_value(false)
-            )
-            .get_matches();
+    let matches = App::new("dominant_colours")
+        .version(VERSION)
+        .author("Alex Chan <alex@alexwlchan.net>")
+        .about("Find the dominant colours in an image")
+        .arg(
+            Arg::with_name("PATH")
+                .help("path to the image to inspect")
+                .required(true)
+                .index(1),
+        )
+        .arg(
+            Arg::with_name("MAX-COLOURS")
+                .long("max-colours")
+                .help("how many colours to find")
+                .default_value("5")
+                .takes_value(true),
+        )
+        .arg(
+            Arg::with_name("no-palette")
+                .long("no-palette")
+                .help("Just print the hex values, not colour previews")
+                .takes_value(false),
+        )
+        .get_matches();
 
     // This .unwrap() is safe because "path" is a required param
     let path = matches.value_of("PATH").unwrap();
@@ -68,7 +67,8 @@ fn main() {
 
     let result = get_kmeans_hamerly(max_colours, max_iterations, converge, verbose, &lab, seed);
 
-    let rgb = &result.centroids
+    let rgb = &result
+        .centroids
         .iter()
         .map(|x| Srgb::from(*x).into_format())
         .collect::<Vec<Srgb<u8>>>();
@@ -82,7 +82,10 @@ fn main() {
         if matches.is_present("no-palette") {
             println!("{}", display_value);
         } else {
-            println!("\x1B[38;2;{};{};{}m▇ {}\x1B[0m", c.red, c.green, c.blue, display_value);
+            println!(
+                "\x1B[38;2;{};{};{}m▇ {}\x1B[0m",
+                c.red, c.green, c.blue, display_value
+            );
         }
     }
 }
@@ -104,9 +107,10 @@ mod tests {
         assert_eq!(output.exit_code, 0);
 
         assert!(
-            output.stdout == "\u{1b}[38;2;255;0;0m▇ #ff0000\u{1b}[0m\n" ||
-            output.stdout == "\u{1b}[38;2;254;0;0m▇ #fe0000\u{1b}[0m\n",
-            "stdout = {:?}", output.stdout
+            output.stdout == "\u{1b}[38;2;255;0;0m▇ #ff0000\u{1b}[0m\n"
+                || output.stdout == "\u{1b}[38;2;254;0;0m▇ #fe0000\u{1b}[0m\n",
+            "stdout = {:?}",
+            output.stdout
         );
 
         assert_eq!(output.stderr, "");
@@ -137,9 +141,9 @@ mod tests {
         assert_eq!(output.exit_code, 0);
 
         assert!(
-            output.stdout == "#ff0000\n" ||
-            output.stdout == "#fe0000\n",
-            "stdout = {:?}", output.stdout
+            output.stdout == "#ff0000\n" || output.stdout == "#fe0000\n",
+            "stdout = {:?}",
+            output.stdout
         );
 
         assert_eq!(output.stderr, "");
@@ -149,14 +153,24 @@ mod tests {
     fn it_defaults_to_five_colours() {
         let output = get_success(&["./src/tests/noise.jpg"]);
 
-        assert_eq!(output.stdout.matches("\n").count(), 5, "stdout = {:?}", output.stdout);
+        assert_eq!(
+            output.stdout.matches("\n").count(),
+            5,
+            "stdout = {:?}",
+            output.stdout
+        );
     }
 
     #[test]
     fn it_lets_you_choose_the_max_colours() {
         let output = get_success(&["./src/tests/noise.jpg", "--max-colours=8"]);
 
-        assert_eq!(output.stdout.matches("\n").count(), 8, "stdout = {:?}", output.stdout);
+        assert_eq!(
+            output.stdout.matches("\n").count(),
+            8,
+            "stdout = {:?}",
+            output.stdout
+        );
     }
 
     // The image created in the next two tests was created with the
@@ -169,14 +183,24 @@ mod tests {
     fn it_looks_at_multiple_frames_in_an_animated_gif() {
         let output = get_success(&["./src/tests/animated_squares.gif"]);
 
-        assert_eq!(output.stdout.matches("\n").count(), 2, "stdout = {:?}", output.stdout);
+        assert_eq!(
+            output.stdout.matches("\n").count(),
+            2,
+            "stdout = {:?}",
+            output.stdout
+        );
     }
 
     #[test]
     fn it_looks_at_multiple_frames_in_an_animated_gif_uppercase() {
         let output = get_success(&["./src/tests/animated_upper_squares.GIF"]);
 
-        assert_eq!(output.stdout.matches("\n").count(), 2, "stdout = {:?}", output.stdout);
+        assert_eq!(
+            output.stdout.matches("\n").count(),
+            2,
+            "stdout = {:?}",
+            output.stdout
+        );
     }
 
     #[test]
@@ -185,7 +209,10 @@ mod tests {
 
         assert_eq!(output.exit_code, 1);
         assert_eq!(output.stdout, "");
-        assert_eq!(output.stderr, "error: Invalid value: The argument 'NaN' isn't a valid value\n");
+        assert_eq!(
+            output.stderr,
+            "error: Invalid value: The argument 'NaN' isn't a valid value\n"
+        );
     }
 
     #[test]
@@ -212,7 +239,10 @@ mod tests {
 
         assert_eq!(output.exit_code, 1);
         assert_eq!(output.stdout, "");
-        assert_eq!(output.stderr, "The file extension `.\"md\"` was not recognized as an image format\n");
+        assert_eq!(
+            output.stderr,
+            "The file extension `.\"md\"` was not recognized as an image format\n"
+        );
     }
 
     #[test]
@@ -249,12 +279,7 @@ mod tests {
 
     fn get_failure(args: &[&str]) -> DcOutput {
         let mut cmd = Command::cargo_bin("dominant_colours").unwrap();
-        let output = cmd
-            .args(args)
-            .unwrap_err()
-            .as_output()
-            .unwrap()
-            .to_owned();
+        let output = cmd.args(args).unwrap_err().as_output().unwrap().to_owned();
 
         DcOutput {
             exit_code: output.status.code().unwrap(),