Skip to content

Commit

Permalink
fix(ppuccinier): crash on file extension + cleanup (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stonks3141 authored Jul 29, 2023
1 parent cb32b6b commit bb2acd5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 58 deletions.
2 changes: 0 additions & 2 deletions puccinier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ homepage = "https://github.com/catppuccin/toolbox/tree/main/puccinier"
description = "Generate the other Catppuccin flavours off a template file written in one of them"
categories = ["command-line-utilities", "template-engine"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "3.2.15", features = ["cargo"] }
serde = { version = "1.0.140", features = ["serde_derive"] }
Expand Down
93 changes: 37 additions & 56 deletions puccinier/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
mod palette;

use clap::{
arg, builder::PossibleValuesParser, command, parser::ValuesRef, value_parser, App, ArgMatches,
ErrorKind,
};
use clap::{arg, builder::PossibleValuesParser, value_parser, ErrorKind};
use regex::{Match, Regex};
use std::{
fs::File,
Expand All @@ -14,60 +11,50 @@ use std::{
use palette::{Color, Palette, COLOR_FROM_VARIANT, VARIANT_FROM_COLOR};

fn main() {
let mut cmd: App = command!()
.arg(
arg!(-s --source <FILE> "Set the source file to convert")
.value_parser(value_parser!(PathBuf))
.required(true)
)
.arg(
arg!(-o --output <TYPES> "Set the themes (space-separated) to generate from the source file")
.value_parser(PossibleValuesParser::new(["latte", "frappe", "macchiato", "mocha"]))
.takes_value(true)
.multiple_values(true)
.required(true)
);

let matches: ArgMatches = cmd.clone().get_matches();

let source_path: &PathBuf = match matches.get_one::<PathBuf>("source") {
let mut cmd = clap::command!()
.arg( arg!(-s --source <FILE> "Set the source file to convert")
.value_parser(value_parser!(PathBuf))
.required(true))
.arg( arg!(-o --output <TYPES> "Set the themes (space-separated) to generate from the source file")
.value_parser(PossibleValuesParser::new(["latte", "frappe", "macchiato", "mocha"]))
.takes_value(true)
.multiple_values(true)
.required(true));

let matches = cmd.clone().get_matches();

let source_path: &PathBuf = match matches.get_one("source") {
Some(path) => path,
None => cmd
.error(ErrorKind::ValueValidation, "Failed to read source argument")
.exit(),
};

let source_file: File = match File::options().read(true).write(true).open(source_path) {
let source_file = match File::options().read(true).write(true).open(source_path) {
Ok(file) => file,
Err(e) => cmd
.error(ErrorKind::Io, format!("Failed to open source file: {e}"))
.exit(),
};

let output_themes: ValuesRef<String> = match matches.get_many("output") {
Some(output_theme) => output_theme,
let output_themes: Vec<&String> = match matches.get_many::<String>("output") {
Some(output_themes) => output_themes.collect(),
None => cmd
.error(ErrorKind::ValueValidation, "Failed to read output argument")
.exit(),
};

let mut writers: Vec<BufWriter<File>> = output_themes
.clone()
.into_iter()
.map(|theme: &String| -> BufWriter<File> {
let extension = match source_path.extension() {
Some(extension) => extension,
None => cmd
.clone()
.error(ErrorKind::Io, "Failed to read source file extension")
.exit(),
};

.iter()
.map(|theme| {
let mut path = source_path.with_file_name(theme);
if let Some(extension) = source_path.extension() {
path = path.with_extension(extension);
}
BufWriter::new(
match File::create(source_path.with_file_name(theme).with_extension(extension)) {
match File::create(path) {
Ok(file) => file,
Err(e) => cmd
.clone()
.error(ErrorKind::Io, format!("Failed to create output file: {e}"))
.exit(),
},
Expand All @@ -82,19 +69,16 @@ fn main() {
}

for line in BufReader::new(&source_file).lines() {
let line: String = match line {
let line = match line {
Ok(line) => line,
Err(e) => {
cmd.clone()
.error(ErrorKind::Io, format!("Failed to read line: {e}, skipping"))
.print()
.unwrap();
continue;
cmd.error(ErrorKind::Io, format!("Failed to read line: {e}"))
.exit();
}
};

for (i, theme) in output_themes.clone().enumerate() {
let mut copy: String = line.clone();
for (theme, writer) in output_themes.iter().zip(&mut writers) {
let mut copy = line.clone();

HEX.find_iter(&line)
.chain(RGB.find_iter(&line))
Expand All @@ -108,16 +92,15 @@ fn main() {
.zip(Some(replacement))
})
.for_each(|(lookup, replacement)| {
let label: &Palette = COLOR_FROM_VARIANT.get(theme).unwrap();
let label: &Palette = COLOR_FROM_VARIANT.get(theme.as_str()).unwrap();
let color_format: &Color = label.get(lookup[1]).unwrap();
let color_value: &String = color_format.get(lookup[2]).unwrap();

copy = copy.replace(replacement, color_value);
});

if let Err(e) = writeln!(writers[i], "{}", copy) {
cmd.clone()
.error(ErrorKind::Io, format!("Failed to write line: {e}"))
if let Err(e) = writeln!(writer, "{}", copy) {
cmd.error(ErrorKind::Io, format!("Failed to write line: {e}"))
.print()
.unwrap();
}
Expand All @@ -126,13 +109,11 @@ fn main() {

for mut writer in writers {
if let Err(e) = writer.flush() {
cmd.clone()
.error(
ErrorKind::Io,
format!("Failed to flush writer: {e}. Changes will be dropped."),
)
.print()
.unwrap();
cmd.error(
ErrorKind::Io,
format!("Failed to flush writer: {e}. Changes will be dropped."),
)
.exit();
}
}
}

0 comments on commit bb2acd5

Please sign in to comment.