Repository: doprz/dipc (language: rust)

Issue:
Compiling from source return errors 
```
  Compiling dipc v0.5.0 (/home/rochalaj/dipc)
warning: unused imports: `fs::File`, `io::BufReader`
 --> src/cli.rs:1:11
  |
1 | use std::{fs::File, io::BufReader, path::PathBuf, str::FromStr};
  |           ^^^^^^^^  ^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused import: `serde_json::Value`
 --> src/cli.rs:4:5
  |
4 | use serde_json::Value;
  |     ^^^^^^^^^^^^^^^^^

error[E0599]: no variant named `RawJSON` found for enum `ColorPalette`
   --> src/config.rs:151:23
    |
151 |         ColorPalette::RawJSON { .. } => String::new(),
    |                       ^^^^^^^ variant not found in `ColorPalette`
    |
   ::: src/cli.rs:107:1
    |
107 | pub enum ColorPalette {
    | --------------------- variant `RawJSON` not found here

error[E0277]: `ColorPaletteArgGroup` doesn't implement `std::fmt::Display`
  --> src/main.rs:53:13
   |
53 |             cli.color_palette, cli.styles
   |             ^^^^^^^^^^^^^^^^^ `ColorPaletteArgGroup` cannot be formatted with the default formatter
   |
   = help: the trait `std::fmt::Display` is not implemented for `ColorPaletteArgGroup`
   = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
   = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `writeln` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0599]: no method named `clone` found for struct `ColorPaletteArgGroup` in the current scope
  --> src/main.rs:60:58
   |
60 |     let palettes = match parse_palette(cli.color_palette.clone().get_json(), &cli.styles) {
   |                                                          ^^^^^ method not found in `ColorPaletteArgGroup`
   |
  ::: src/cli.rs:45:1
   |
45 | struct ColorPaletteArgGroup {
   | --------------------------- method `clone` not found for this struct
   |
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following trait defines an item `clone`, perhaps you need to implement it:
           candidate #1: `Clone`

error[E0308]: mismatched types
   --> src/main.rs:162:50
    |
162 |             let new_rgb = lab.to_nearest_palette(&palettes).to_rgb();
    |                               ------------------ ^^^^^^^^^ expected `&[Lab]`, found `&Vec<Palette>`
    |                               |
    |                               arguments to this method are incorrect
    |
    = note: expected reference `&[delta::Lab]`
               found reference `&Vec<Palette>`
note: method defined here
   --> src/delta.rs:35:12
    |
35  |     pub fn to_nearest_palette(self, palette: &[Lab]) -> Self {
    |            ^^^^^^^^^^^^^^^^^^       ---------------

error[E0308]: mismatched types
   --> src/main.rs:166:56
    |
166 |         let output_file_name = output_file_name(&path, &cli.color_palette, &palettes);
    |                                ----------------        ^^^^^^^^^^^^^^^^^^ expected `&ColorPalette`, found `&ColorPaletteArgGroup`
    |                                |
    |                                arguments to this function are incorrect
    |
    = note: expected reference `&ColorPalette`
               found reference `&ColorPaletteArgGroup`
note: function defined here
   --> src/config.rs:137:8
    |
137 | pub fn output_file_name(
    |        ^^^^^^^^^^^^^^^^
138 |     input_file_path: &PathBuf,
139 |     color_palette: &ColorPalette,
    |     ----------------------------

Some errors have detailed explanations: E0277, E0308, E0599.
For more information about an error, try `rustc --explain E0277`.
warning: `dipc` (bin "dipc") generated 2 warnings
error: could not compile `dipc` due to 5 previous errors; 2 warnings emitted
error: failed to compile `dipc v0.5.0 (/home/rochalaj/dipc)`, intermediate artifacts can be found at `/home/rochalaj/dipc/target`
```

cargo v1.69.0
OpenSuse Tumbleweed

how to reproduce
1. git clone https://github.com/doprz/dipc.git 
2. cd dipc
3. cargo install --path . 

I did not obtain any kind of error installing directly from cargo with: `cargo install dipc `



Discussion / hints:
Thank you for reporting this issue. We're currently working on releasing `v1.0.0` and we're moving some things around.
`cargo install dipc` is on `v0.5.0` and as such doesn't have these new changes.

I will momentarily re-enable these features and update this thread when it's finished.

Fix patch:
diff --git a/src/cli.rs b/src/cli.rs
index 52e2fd7..c1a28de 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -32,8 +32,24 @@ pub struct Cli {
     pub verbose: u8,
 
     // Arguments
-    #[command(flatten)]
-    pub color_palette: ColorPaletteArgGroup,
+    // TODO: tmp disabled
+    // #[command(flatten)]
+    // pub color_palette: ColorPaletteArgGroup,
+
+    /// The color palette to use, name of a builtin theme, path to a theme in JSON, or a JSON string with the theme (starting with `JSON: {}`)
+    /// Run with --help instead of -h for a list of all builtin themes
+    ///
+    /// Builtin themes:
+    /// - catppuccin
+    /// - edge
+    /// - everforest
+    /// - gruvbox
+    /// - gruvbox-material
+    /// - nord
+    /// - rose-pine
+    /// - tokyo-night
+    #[arg(value_name = "PALETTE", verbatim_doc_comment)]
+    pub color_palette: ColorPalette,
 
     /// The image(s) to process
     #[arg(value_name = "FILE")]
@@ -105,7 +121,7 @@ impl FromStr for ColorPaletteStyles {
 
 #[derive(Clone, Debug)]
 pub enum ColorPalette {
-    // RawJSON { map: serde_json::Map<String, Value> },
+    RawJSON { map: serde_json::Map<String, Value> },
     Catppuccin,
     Edge,
     Everforest,
@@ -121,9 +137,9 @@ pub enum ColorPalette {
 impl std::fmt::Display for ColorPalette {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         match self {
-            // ColorPalette::RawJSON { map } => {
-            //     write!(f, "JSON: {}", serde_json::to_string(map).unwrap())
-            // }
+            ColorPalette::RawJSON { map } => {
+                write!(f, "JSON: {}", serde_json::to_string(map).unwrap())
+            }
             ColorPalette::Catppuccin => write!(f, "catppuccin"),
             ColorPalette::Edge => write!(f, "edge"),
             ColorPalette::Everforest => write!(f, "everforest"),
@@ -143,14 +159,14 @@ impl FromStr for ColorPalette {
 
     fn from_str(s: &str) -> Result<Self, Self::Err> {
         // TODO
-        // if s.starts_with("JSON: ") {
-        //     let jsonstr = &s[5..];
-        //     let json: Value = serde_json::from_str(jsonstr).map_err(|err| err.to_string())?;
-        //     let Value::Object(map) = json else {
-        //         return Err(format!("Encountered error while parsing inline JSON string: the string appears to not be a JSON object"))
-        //     };
-        //     return Ok(ColorPalette::RawJSON { map });
-        // };
+        if s.starts_with("JSON: ") {
+            let jsonstr = &s[5..];
+            let json: Value = serde_json::from_str(jsonstr).map_err(|err| err.to_string())?;
+            let Value::Object(map) = json else {
+                return Err(format!("Encountered error while parsing inline JSON string: the string appears to not be a JSON object"))
+            };
+            return Ok(ColorPalette::RawJSON { map });
+        };
         let palette = match s {
             "catppuccin" => ColorPalette::Catppuccin,
             "edge" => ColorPalette::Edge,
@@ -166,20 +182,20 @@ impl FromStr for ColorPalette {
             "tokyo-night" | "tokyo_night" | "tokyonight" => ColorPalette::TokyoNight,
             // TODO
             // The color palette seems to be the path to an external file
-            // external => {
-            //     let external: PathBuf = external.into();
-            //     if !external.is_file() {
-            //         return Err(format!("Theme source file `{s}` appears to not be a file."));
-            //     };
-            //     let file = File::open(external).map_err(|err| err.to_string())?;
-            //     let file = BufReader::new(file);
-            //     let json = serde_json::from_reader(file)
-            //         .map_err(|err| format!("Error while parsing JSON content of {s}: {err}"))?;
-            //     let Value::Object(map) = json else {
-            //     return Err(format!("Encountered error while parsing JSON theme file: the contents of the file are valid JSON but do not appear to be a JSON object"))
-            // };
-            //     ColorPalette::RawJSON { map }
-            // }
+            external => {
+                let external: PathBuf = external.into();
+                if !external.is_file() {
+                    return Err(format!("Theme source file `{s}` appears to not be a file."));
+                };
+                let file = File::open(external).map_err(|err| err.to_string())?;
+                let file = BufReader::new(file);
+                let json = serde_json::from_reader(file)
+                    .map_err(|err| format!("Error while parsing JSON content of {s}: {err}"))?;
+                let Value::Object(map) = json else {
+                return Err(format!("Encountered error while parsing JSON theme file: the contents of the file are valid JSON but do not appear to be a JSON object"))
+            };
+                ColorPalette::RawJSON { map }
+            }
         };
         Ok(palette)
     }
diff --git a/src/main.rs b/src/main.rs
index 62c4980..203a19a 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -57,7 +57,7 @@ fn main() -> io::Result<()> {
         // writeln!(writer, "Processing {:#?}\nWriting results to {:#?} directory", cli.process, cli.output)?;
         writeln!(writer, "Processing {:#?}", cli.process)?;
     }
-    let palettes = match parse_palette(cli.color_palette.clone().get_json(), &cli.styles) {
+    let mut palettes = match parse_palette(cli.color_palette.clone().get_json(), &cli.styles) {
         Ok(p) => p,
         Err(err) => {
             eprintln!(
@@ -159,7 +159,7 @@ fn main() -> io::Result<()> {
         image.par_chunks_exact_mut(CHUNK).for_each(|bytes| {
             let pixel: [u8; CHUNK] = bytes.try_into().unwrap();
             let lab = Lab::from(pixel);
-            let new_rgb = lab.to_nearest_palette(&palettes).to_rgb();
+            let new_rgb = lab.to_nearest_palette(&palettes_lab).to_rgb();
             bytes[..3].copy_from_slice(&new_rgb);
         });
 
diff --git a/src/palettes.rs b/src/palettes.rs
index fccac09..1950455 100644
--- a/src/palettes.rs
+++ b/src/palettes.rs
@@ -5,7 +5,7 @@ use crate::cli::ColorPalette;
 impl ColorPalette {
     pub fn get_json(self) -> serde_json::Map<String, Value> {
         let colors = match self {
-            // ColorPalette::RawJSON { map } => return map,
+            ColorPalette::RawJSON { map } => return map,
             ColorPalette::Catppuccin => serde_json::from_str(include_str!("./palettes/catppuccin.json")).unwrap(),
             ColorPalette::Edge => serde_json::from_str(include_str!("./palettes/edge.json")).unwrap(),
             ColorPalette::Everforest => serde_json::from_str(include_str!("./palettes/everforest.json")).unwrap(),


Test patch: