Skip to content

Commit

Permalink
More configs for minifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
szeweq committed May 14, 2024
1 parent 844cfb1 commit a78906e
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 42 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion lib-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,5 @@ oxipng = {optional = true, version = "^9.1.0", default-features = false, feature
flate2 = "^1.0.26"
json_comments = "0.2"
toml = {optional = true, version = "^0.8.0", features = ["preserve_order"]}
lazy_static = "^1.4.0"
zopfli = {version = "^0.8.0", optional = true}
state = "0.6"
2 changes: 1 addition & 1 deletion lib-core/src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<AC: AcceptsConfig> std::ops::Deref for ConfigHolder<AC> {
macro_rules! acfg {
($ac:ident : $cfg:ty) => {
pub enum $ac {}
impl AcceptsConfig for $ac {
impl crate::cfg::AcceptsConfig for $ac {
type Cfg = $cfg;
}
};
Expand Down
34 changes: 25 additions & 9 deletions lib-core/src/min/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,34 @@ use std::io::Cursor;
use json_comments::StripComments;
use serde_json::Value;

use crate::cfg::{acfg, ConfigHolder};

use super::{find_brackets, BracketsError, Result_};

pub(super) fn minify_json(v: &[u8], vout: &mut Vec<u8>) -> Result_ {
let (i, j) = find_brackets(v).ok_or(BracketsError)?;
let fv = &v[i..=j];
let strip_comments = StripComments::new(Cursor::new(fv));
let mut sv: Value = serde_json::from_reader(strip_comments)?;
if let Value::Object(xm) = &mut sv {
uncomment_json_recursive(xm);
acfg!(MinifierJSON: JSONConfig);
impl ConfigHolder<MinifierJSON> {
pub(super) fn minify(&self, b: &[u8], vout: &mut Vec<u8>) -> Result_ {
let (i, j) = find_brackets(b).ok_or(BracketsError)?;
let fv = &b[i..=j];
let strip_comments = StripComments::new(Cursor::new(fv));
let mut sv: Value = serde_json::from_reader(strip_comments)?;
if self.remove_underscored {
if let Value::Object(xm) = &mut sv {
uncomment_json_recursive(xm);
}
}
serde_json::to_writer(vout, &sv)?;
Ok(())
}
}

pub struct JSONConfig {
remove_underscored: bool
}
impl Default for JSONConfig {
fn default() -> Self {
Self { remove_underscored: true }
}
serde_json::to_writer(vout, &sv)?;
Ok(())
}

fn uncomment_json_recursive(m: &mut serde_json::Map<String, Value>) {
Expand Down
6 changes: 3 additions & 3 deletions lib-core/src/min/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ impl Minifier {
/// Returns an error if minifying fails, depending on file type
pub fn minify(&self, cfgmap: &cfg::ConfigMap, v: &[u8], vout: &mut Vec<u8>) -> Result_ {
match self {
#[cfg(feature = "png")] Self::PNG => png::minify_png(v, vout),
Self::JSON => json::minify_json(strip_bom(v), vout),
#[cfg(feature = "toml")] Self::TOML => toml::minify_toml(strip_bom(v), vout),
#[cfg(feature = "png")] Self::PNG => cfgmap.fetch::<png::MinifierPNG>().minify(v, vout),
Self::JSON => cfgmap.fetch::<json::MinifierJSON>().minify(strip_bom(v), vout),
#[cfg(feature = "toml")] Self::TOML => cfgmap.fetch::<toml::MinifierTOML>().minify(strip_bom(v), vout),
#[cfg(feature = "nbt")] Self::NBT => cfgmap.fetch::<nbt::MinifierNBT>().minify(v, vout),
Self::Hash => remove_line_comments(b"#", v, vout),
Self::Slash => remove_line_comments(b"//", v, vout)
Expand Down
2 changes: 1 addition & 1 deletion lib-core/src/min/nbt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{error::Error, io::{copy, Cursor, Write}};

use crate::cfg::{AcceptsConfig, ConfigHolder, acfg};
use crate::cfg::{ConfigHolder, acfg};

use super::Result_;

Expand Down
32 changes: 21 additions & 11 deletions lib-core/src/min/png.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
#![cfg(feature = "png")]

use crate::cfg::{acfg, ConfigHolder};

use super::Result_;
use lazy_static::lazy_static;

#[cfg(feature = "png-zopfli")]
const BEST_DEFLATE: oxipng::Deflaters = oxipng::Deflaters::Zopfli { iterations: 15 };
#[cfg(not(feature = "png-zopfli"))]
const BEST_DEFLATE: oxipng::Deflaters = oxipng::Deflaters::Libdeflater { compression: 12 };

lazy_static! {
static ref PNG_OPTS: oxipng::Options = {
acfg!(MinifierPNG: PNGConfig);
impl ConfigHolder<MinifierPNG> {
pub(super) fn minify(&self, b: &[u8], vout: &mut Vec<u8>) -> Result_ {
let v = oxipng::optimize_from_memory(b, &self.oxipng_opts)?;
let _ = std::mem::replace(vout, v);
Ok(())
}
}

pub struct PNGConfig {
oxipng_opts: oxipng::Options,
#[cfg(feature = "png-zopfli")]
use_zopfli: bool
}
impl Default for PNGConfig {
fn default() -> Self {
let mut popts = oxipng::Options {
fix_errors: true,
strip: oxipng::StripChunks::Safe,
Expand All @@ -19,12 +34,7 @@ lazy_static! {
};
popts.filter.insert(oxipng::RowFilter::Up);
popts.filter.insert(oxipng::RowFilter::Paeth);
popts
};

Self { oxipng_opts: popts, #[cfg(feature = "png-zopfli")] use_zopfli: false }
}
}

pub(super) fn minify_png(v: &[u8], vout: &mut Vec<u8>) -> Result_ {
let v = oxipng::optimize_from_memory(v, &PNG_OPTS)?;
let _ = std::mem::replace(vout, v);
Ok(())
}
46 changes: 31 additions & 15 deletions lib-core/src/min/toml.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
#![cfg(feature = "toml")]

use crate::cfg::{acfg, ConfigHolder};

use super::Result_;

pub(super) fn minify_toml(v: &[u8], vout: &mut Vec<u8>) -> Result_ {
let fv = std::str::from_utf8(v)?;
let mut table: toml::Table = toml::from_str(fv)?;
strip_toml_table(&mut table);
toml::to_string(&table)?.lines().for_each(|l| {
match l.split_once(" = ") {
Some((k, v)) => {
vout.extend_from_slice(k.as_bytes());
vout.push(b'=');
vout.extend_from_slice(v.as_bytes());
}
None => vout.extend_from_slice(l.as_bytes()),
acfg!(MinifierTOML: TOMLConfig);
impl ConfigHolder<MinifierTOML> {
pub(super) fn minify(&self, b: &[u8], vout: &mut Vec<u8>) -> Result_ {
let fv = std::str::from_utf8(b)?;
let mut table: toml::Table = toml::from_str(fv)?;
if self.strip_strings {
strip_toml_table(&mut table);
}
vout.push(b'\n');
});
Ok(())
toml::to_string(&table)?.lines().for_each(|l| {
match l.split_once(" = ") {
Some((k, v)) => {
vout.extend_from_slice(k.as_bytes());
vout.push(b'=');
vout.extend_from_slice(v.as_bytes());
}
None => vout.extend_from_slice(l.as_bytes()),
}
vout.push(b'\n');
});
Ok(())
}
}

pub struct TOMLConfig {
strip_strings: bool
}
impl Default for TOMLConfig {
fn default() -> Self {
Self { strip_strings: true }
}
}

fn strip_toml_table(t: &mut toml::Table) {
Expand Down

0 comments on commit a78906e

Please sign in to comment.