diff --git a/css-minifier-web/Cargo.toml b/css-minifier-web/Cargo.toml index a9f86b6..796ac91 100644 --- a/css-minifier-web/Cargo.toml +++ b/css-minifier-web/Cargo.toml @@ -1,15 +1,14 @@ [package] name = "css-minifier-web" -version = "0.3.0" +version = "0.5.0" authors = ["Mnwa "] edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -css-minify = { version = "0.4", path = "../css-minify" } +css-minify = { version = "0.5", path = "../css-minify" } actix-web = { version = "4" } actix-files = "0.6" yarte = "0.15" -serde = { version = "1.0", features = ["derive"] } -parking_lot = "0.12" \ No newline at end of file +serde = { version = "1.0", features = ["derive"] } \ No newline at end of file diff --git a/css-minifier-web/src/main.rs b/css-minifier-web/src/main.rs index 4af862e..5947615 100644 --- a/css-minifier-web/src/main.rs +++ b/css-minifier-web/src/main.rs @@ -1,6 +1,5 @@ use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder}; use css_minify::optimizations::{Level, Minifier}; -use parking_lot::Mutex; use serde::Deserialize; use std::collections::hash_map::DefaultHasher; use std::hash::{Hash, Hasher}; @@ -22,13 +21,10 @@ async fn index() -> impl Responder { #[post("/")] async fn minify_css( request: web::Form, - minifier: web::Data>, + minifier: web::Data, ) -> impl Responder { let level = Level::from_str(&request.level).unwrap_or(Level::One); - let output_css = match minifier.lock().minify(&request.input_css, level) { - Ok(output_css) => output_css, - Err(e) => e.to_string(), - }; + let output_css = minifier.minify(&request.input_css, level).unwrap_or_else(|e| e.to_string()); match Template::call(&IndexTemplate { input_css: Some(request.into_inner().input_css), @@ -55,10 +51,9 @@ async fn main_css(css: web::Data) -> impl Responder { #[actix_web::main] async fn main() -> std::io::Result<()> { let mut hasher = DefaultHasher::default(); - let minifier = web::Data::new(Mutex::new(Minifier::default())); + let minifier = web::Data::new(Minifier::default()); let minified_css = minifier - .lock() .minify(include_str!("../static/main.css"), Level::Three) .expect("invalid css"); @@ -78,9 +73,9 @@ async fn main() -> std::io::Result<()> { .service(minify_css) .service(main_css) }) - .bind(std::env::var("HTTP_HOST").unwrap_or_else(|_| "0.0.0.0:8081".into()))? - .run() - .await + .bind(std::env::var("HTTP_HOST").unwrap_or_else(|_| "0.0.0.0:8081".into()))? + .run() + .await } struct MinifiedCss { diff --git a/css-minifier/Cargo.toml b/css-minifier/Cargo.toml index 8e23a19..cc113ea 100644 --- a/css-minifier/Cargo.toml +++ b/css-minifier/Cargo.toml @@ -12,7 +12,7 @@ license = "Apache-2.0/MIT" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -css-minify = { version = "0.4", path = "../css-minify" } +css-minify = { version = "0.5", path = "../css-minify" } clap = { version = "^4", features = ["derive"] } shellexpand = "3.1" indoc = "2.0" diff --git a/css-minify/Cargo.toml b/css-minify/Cargo.toml index fdedcf2..7780872 100644 --- a/css-minify/Cargo.toml +++ b/css-minify/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "css-minify" -version = "0.4.2" +version = "0.5.0" authors = ["Mnwa "] edition = "2021" description = "CSS parser and minificator based on nom" diff --git a/css-minify/src/optimizations/font.rs b/css-minify/src/optimizations/font.rs index 4387d7a..3ea98fd 100644 --- a/css-minify/src/optimizations/font.rs +++ b/css-minify/src/optimizations/font.rs @@ -5,7 +5,7 @@ use crate::structure::{Parameters, Value}; pub(crate) struct FontTransformer; impl Transform for FontTransformer { - fn transform_parameters(&mut self, mut parameters: Parameters) -> Parameters { + fn transform_parameters(&self, mut parameters: Parameters) -> Parameters { parameters .iter_mut() .filter(|(name, _)| matches!(name.as_str(), "font-weight")) @@ -34,25 +34,25 @@ mod test { Some(Selector::Class("test".into())), vec![], )] - .into(), + .into(), parameters: { let mut tmp = IndexMap::new(); tmp.insert("font-weight".into(), "bold".into()); tmp.into() }, - }),])), + }), ])), CssEntities(vec![CssEntity::Block(Block { selectors: vec![SelectorWithPseudoClasses( Some(Selector::Class("test".into())), vec![], )] - .into(), + .into(), parameters: { let mut tmp = IndexMap::new(); tmp.insert("font-weight".into(), "700".into()); tmp.into() }, - }),]) + }), ]) ) } } diff --git a/css-minify/src/optimizations/merge_blocks.rs b/css-minify/src/optimizations/merge_blocks.rs index a2c4358..ae62c41 100644 --- a/css-minify/src/optimizations/merge_blocks.rs +++ b/css-minify/src/optimizations/merge_blocks.rs @@ -6,15 +6,15 @@ use indexmap::map::IndexMap; pub struct MergeBlocks; impl Transform for MergeBlocks { - fn transform_parameters(&mut self, parameters: Parameters) -> Parameters { + fn transform_parameters(&self, parameters: Parameters) -> Parameters { parameters } - fn transform(&mut self, entity: CssEntity) -> CssEntity { + fn transform(&self, entity: CssEntity) -> CssEntity { entity } - fn transform_many(&mut self, entities: CssEntities) -> CssEntities { + fn transform_many(&self, entities: CssEntities) -> CssEntities { let mut blocks_to_merge = IndexMap::new(); entities .0 @@ -43,15 +43,15 @@ impl Transform for MergeBlocks { screen, entities: self.transform_many(entities), } - .into(), + .into(), CssEntity::Supports(Supports { - conditions, - entities, - }) => Supports { + conditions, + entities, + }) => Supports { conditions, entities: self.transform_many(entities), } - .into(), + .into(), entity => entity, }) .collect(), @@ -79,7 +79,7 @@ mod test { Some(Selector::Class("test".into())), vec![] )] - .into(), + .into(), parameters: { let mut tmp = IndexMap::new(); tmp.insert("background-color".into(), "#f64e60 !important".into()); @@ -91,7 +91,7 @@ mod test { Some(Selector::Class("test".into())), vec![] )] - .into(), + .into(), parameters: { let mut tmp = IndexMap::new(); tmp.insert("color".into(), "#f64e60 !important".into()); @@ -104,7 +104,7 @@ mod test { Some(Selector::Class("test".into())), vec![] )] - .into(), + .into(), parameters: { let mut tmp = IndexMap::new(); tmp.insert("background-color".into(), "#f64e60 !important".into()); diff --git a/css-minify/src/optimizations/merge_m_n_p.rs b/css-minify/src/optimizations/merge_m_n_p.rs index fecde48..43f1840 100644 --- a/css-minify/src/optimizations/merge_m_n_p.rs +++ b/css-minify/src/optimizations/merge_m_n_p.rs @@ -13,7 +13,7 @@ pub struct Margin(Option, Option, Option, Option); pub struct Padding(Option, Option, Option, Option); impl Transform for Merge { - fn transform_parameters(&mut self, mut parameters: Parameters) -> Parameters { + fn transform_parameters(&self, mut parameters: Parameters) -> Parameters { let mut margin = Margin::default(); let mut padding = Padding::default(); parameters.0.iter().for_each(|(name, val)| { @@ -235,7 +235,7 @@ mod test { Parameters(map) }, } - .into() + .into() ), Block { selectors: Selectors::default(), @@ -245,7 +245,7 @@ mod test { Parameters(map) }, } - .into() + .into() ) } @@ -264,7 +264,7 @@ mod test { Parameters(map) }, } - .into() + .into() ), Block { selectors: Selectors::default(), @@ -274,7 +274,7 @@ mod test { Parameters(map) }, } - .into() + .into() ) } @@ -293,7 +293,7 @@ mod test { Parameters(map) }, } - .into() + .into() ), Block { selectors: Selectors::default(), @@ -303,7 +303,7 @@ mod test { Parameters(map) }, } - .into() + .into() ) } @@ -322,7 +322,7 @@ mod test { Parameters(map) }, } - .into() + .into() ), Block { selectors: Selectors::default(), @@ -332,7 +332,7 @@ mod test { Parameters(map) }, } - .into() + .into() ) } } diff --git a/css-minify/src/optimizations/merge_media.rs b/css-minify/src/optimizations/merge_media.rs index d911e87..7b5b4ec 100644 --- a/css-minify/src/optimizations/merge_media.rs +++ b/css-minify/src/optimizations/merge_media.rs @@ -6,11 +6,11 @@ use indexmap::map::IndexMap; pub struct MergeMedia; impl Transform for MergeMedia { - fn transform_parameters(&mut self, parameters: Parameters) -> Parameters { + fn transform_parameters(&self, parameters: Parameters) -> Parameters { parameters } - fn transform(&mut self, entity: CssEntity) -> CssEntity { + fn transform(&self, entity: CssEntity) -> CssEntity { match entity { CssEntity::Media(mut m) => { m.screen = m.screen.replace(": ", ":"); @@ -20,7 +20,7 @@ impl Transform for MergeMedia { } } - fn transform_many(&mut self, entities: CssEntities) -> CssEntities { + fn transform_many(&self, entities: CssEntities) -> CssEntities { let mut media = IndexMap::new(); entities .0 @@ -47,13 +47,13 @@ impl Transform for MergeMedia { .filter(|e| !matches!(e, CssEntity::Media(_))) .map(|e| match e { CssEntity::Supports(Supports { - conditions, - entities, - }) => Supports { + conditions, + entities, + }) => Supports { conditions, entities: self.transform_many(entities), } - .into(), + .into(), entity => entity, }) .collect(), @@ -88,10 +88,10 @@ mod test { Some(Selector::Class("test".into())), vec![] )] - .into(), + .into(), parameters: Default::default() })] - .into() + .into() }), CssEntity::Media(Media { screen: Value::from("only screen and (max-width: 992px)"), @@ -100,10 +100,10 @@ mod test { Some(Selector::Class("test2".into())), vec![] )] - .into(), + .into(), parameters: Default::default() })] - .into() + .into() }) ])), CssEntities(vec![CssEntity::Media(Media { @@ -114,7 +114,7 @@ mod test { Some(Selector::Class("test".into())), vec![] )] - .into(), + .into(), parameters: Default::default() }), CssEntity::Block(Block { @@ -122,11 +122,11 @@ mod test { Some(Selector::Class("test2".into())), vec![] )] - .into(), + .into(), parameters: Default::default() }) ] - .into() + .into() })]) ) } diff --git a/css-minify/src/optimizations/merge_shorthand.rs b/css-minify/src/optimizations/merge_shorthand.rs index 2cd24ec..a673bb5 100644 --- a/css-minify/src/optimizations/merge_shorthand.rs +++ b/css-minify/src/optimizations/merge_shorthand.rs @@ -8,7 +8,7 @@ use std::fmt::Display; pub struct MergeShortHand; impl Transform for MergeShortHand { - fn transform_parameters(&mut self, mut parameters: Parameters) -> Parameters { + fn transform_parameters(&self, mut parameters: Parameters) -> Parameters { let mut font = FontShortHand::default(); let mut list = ListShortHand::default(); let mut background = BackgroundShortHand::default(); @@ -508,7 +508,7 @@ mod test { Parameters(map) }, } - .into() + .into() ), Block { selectors: Selectors::default(), @@ -521,7 +521,7 @@ mod test { Parameters(map) }, } - .into() + .into() ) } @@ -540,7 +540,7 @@ mod test { Parameters(map) }, } - .into() + .into() ), Block { selectors: Selectors::default(), @@ -553,7 +553,7 @@ mod test { Parameters(map) }, } - .into() + .into() ) } @@ -569,7 +569,7 @@ mod test { Parameters(map) }, } - .into() + .into() ), Block { selectors: Selectors::default(), @@ -579,7 +579,7 @@ mod test { Parameters(map) }, } - .into() + .into() ) } @@ -597,7 +597,7 @@ mod test { Parameters(map) }, } - .into() + .into() ), Block { selectors: Selectors::default(), @@ -607,7 +607,7 @@ mod test { Parameters(map) }, } - .into() + .into() ) } @@ -625,7 +625,7 @@ mod test { Parameters(map) }, } - .into() + .into() ), Block { selectors: Selectors::default(), @@ -635,7 +635,7 @@ mod test { Parameters(map) }, } - .into() + .into() ) } } diff --git a/css-minify/src/optimizations/mod.rs b/css-minify/src/optimizations/mod.rs index af4feb2..7bc7f14 100644 --- a/css-minify/src/optimizations/mod.rs +++ b/css-minify/src/optimizations/mod.rs @@ -35,7 +35,7 @@ pub struct Minifier { impl Minifier { /// Minify css input and return result with minified css string - pub fn minify<'a>(&mut self, input: &'a str, level: Level) -> MResult<'a> { + pub fn minify<'a>(&self, input: &'a str, level: Level) -> MResult<'a> { let mut result = parse_css(input) .map(|(_, blocks)| blocks) .map_err(|e| MError(input, e)); diff --git a/css-minify/src/optimizations/transformer.rs b/css-minify/src/optimizations/transformer.rs index 15e2093..e0ad5ef 100644 --- a/css-minify/src/optimizations/transformer.rs +++ b/css-minify/src/optimizations/transformer.rs @@ -7,8 +7,8 @@ pub struct Transformer { } pub enum TransformerParameterFn { - Name(Box Name>), - Value(Box Value>), + Name(Box Name + Send + Sync>), + Value(Box Value + Send + Sync>), } unsafe impl Send for TransformerParameterFn {} @@ -20,12 +20,12 @@ impl Transformer { } impl Transform for Transformer { - fn transform_parameters(&mut self, parameters: Parameters) -> Parameters { + fn transform_parameters(&self, parameters: Parameters) -> Parameters { parameters .0 .into_iter() .map(|(mut name, mut value)| { - for transformer in self.parameters.iter_mut() { + for transformer in self.parameters.iter() { match transformer { TransformerParameterFn::Name(t) => name = t(name), TransformerParameterFn::Value(t) => value = t(value), @@ -39,8 +39,8 @@ impl Transform for Transformer { } pub trait Transform { - fn transform_parameters(&mut self, parameters: Parameters) -> Parameters; - fn transform(&mut self, entity: CssEntity) -> CssEntity { + fn transform_parameters(&self, parameters: Parameters) -> Parameters; + fn transform(&self, entity: CssEntity) -> CssEntity { match entity { CssEntity::Block(mut block) => { block.parameters = self.transform_parameters(block.parameters); @@ -56,9 +56,9 @@ pub trait Transform { .into(), }), CssEntity::Supports(Supports { - conditions, - entities, - }) => CssEntity::Supports(Supports { + conditions, + entities, + }) => CssEntity::Supports(Supports { conditions, entities: entities .0 @@ -98,7 +98,7 @@ pub trait Transform { CssEntity::At(at) => CssEntity::At(at), } } - fn transform_many(&mut self, blocks: CssEntities) -> CssEntities { + fn transform_many(&self, blocks: CssEntities) -> CssEntities { CssEntities(blocks.0.into_iter().map(|b| self.transform(b)).collect()) } }