Skip to content

Commit

Permalink
Fix clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnwa committed Aug 17, 2024
1 parent a879812 commit 5279c81
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 73 deletions.
2 changes: 1 addition & 1 deletion css-minifier/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn main() {
let size_rate = ((size_diff as f64) / (input_file.len() as f64) * 100f64) as i64;
let mut prefix = "bytes";
if size_diff > 1024 {
size_diff = size_diff / 1024;
size_diff /= 1024;
prefix = "kilobytes"
}
println!("You saved: {}% ({} {})", size_rate, size_diff, prefix);
Expand Down
2 changes: 1 addition & 1 deletion css-minify/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "css-minify"
version = "0.4.1"
version = "0.4.2"
authors = ["Mnwa <[email protected]>"]
edition = "2018"
description = "CSS parser and minificator based on nom"
Expand Down
15 changes: 7 additions & 8 deletions css-minify/src/optimizations/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,20 @@ use nom::IResult;
pub struct Color(String);

pub fn optimize_color(input: &str) -> Color {
let color = alt((parse_hex, parse_rgb))(input)
alt((parse_hex, parse_rgb))(input)
.map(|(postfix, mut color)| {
if postfix.trim() == "!important" {
color.0 += "!important"
}
color
})
.unwrap_or_else(|_| Color(input.into()));
color
.unwrap_or_else(|_| Color(input.into()))
}

pub fn parse_hex(input: &str) -> IResult<&str, Color> {
map(recognize(preceded(tag("#"), hex_digit1)), |color: &str| {
if color.len() > 3 && color[1..4] == color[4..] {
return Color((&color[..4]).to_lowercase());
return Color(color[..4].to_lowercase());
}
Color(color.to_lowercase())
})(input)
Expand All @@ -44,13 +43,13 @@ pub fn parse_rgb(input: &str) -> IResult<&str, Color> {
|(red, green, blue): (&str, &str, &str)| {
let color = format!(
"#{:02X}{:02X}{:02X}",
u8::from_str_radix(red, 10).unwrap(),
u8::from_str_radix(green, 10).unwrap(),
u8::from_str_radix(blue, 10).unwrap(),
str::parse::<u8>(red).unwrap(),
str::parse::<u8>(green).unwrap(),
str::parse::<u8>(blue).unwrap(),
);

if color[1..4] == color[4..] {
return Color((&color[..4]).to_lowercase());
return Color(color[..4].to_lowercase());
}
Color(color.to_lowercase())
},
Expand Down
55 changes: 24 additions & 31 deletions css-minify/src/optimizations/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,30 @@ mod test {
#[test]
fn test_blocks() {
assert_eq!(
FontTransformer::default().transform_many(CssEntities(vec![
CssEntity::Block(Block {
selectors: vec![SelectorWithPseudoClasses(
Some(Selector::Class("test".into())),
vec![],
)]
.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(),
parameters: {
let mut tmp = IndexMap::new();
tmp.insert("font-weight".into(), "700".into());
tmp.into()
},
}),
])
FontTransformer::default().transform_many(CssEntities(vec![CssEntity::Block(Block {
selectors: vec![SelectorWithPseudoClasses(
Some(Selector::Class("test".into())),
vec![],
)]
.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(),
parameters: {
let mut tmp = IndexMap::new();
tmp.insert("font-weight".into(), "700".into());
tmp.into()
},
}),])
)
}
}
10 changes: 5 additions & 5 deletions css-minify/src/optimizations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ impl Default for Minifier {
name.to_lowercase()
})));

let merge_m_n_p = Merge::default();
let merge_shorthand = MergeShortHand::default();
let media = MergeMedia::default();
let blocks = MergeBlocks::default();
let font = FontTransformer::default();
let merge_m_n_p = Merge;
let merge_shorthand = MergeShortHand;
let media = MergeMedia;
let blocks = MergeBlocks;
let font = FontTransformer;

Minifier {
merge_m_n_p,
Expand Down
14 changes: 5 additions & 9 deletions css-minify/src/parsers/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::parsers::utils::{between, is_not_block_ending, non_useless, space};
use crate::structure::{Name, Parameters, Value};
use indexmap::map::IndexMap;
use nom::branch::alt;
use nom::bytes::complete::{is_not};
use nom::character::complete::{char};
use nom::bytes::complete::is_not;
use nom::character::complete::char;
use nom::combinator::{map, peek, recognize};
use nom::multi::{many0, many1};
use nom::sequence::{separated_pair, terminated};
Expand All @@ -19,11 +19,7 @@ pub fn parse_parameters(input: &str) -> IResult<&str, Parameters> {
pub fn parse_parameter(input: &str) -> IResult<&str, (Name, Value)> {
map(
terminated(
separated_pair(
parse_key,
char(':'),
parse_value,
),
separated_pair(parse_key, char(':'), parse_value),
alt((char(';'), peek(char('}')))),
),
|(name, value)| (name.trim().into(), value.trim().into()),
Expand All @@ -40,7 +36,7 @@ fn parse_value(input: &str) -> IResult<&str, &str> {
between("'", "'"),
between("(", ")"),
space,
is_not("\"'();}")
is_not("\"'();}"),
)))))(input)
}

Expand Down Expand Up @@ -106,7 +102,7 @@ mod test {
font-size: 16px;
"
)
.is_err(),
.is_err(),
true
)
}
Expand Down
33 changes: 19 additions & 14 deletions css-minify/src/parsers/selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ pub fn parse_pseudo_class(input: &str) -> IResult<&str, PseudoClass> {
i.map(|i| i.to_string())
}),
),
|(((prefix, name), params), next)| PseudoClass { prefix, name, params, next },
|(((prefix, name), params), next)| PseudoClass {
prefix,
name,
params,
next,
},
)(input)
}

Expand Down Expand Up @@ -112,7 +117,7 @@ mod test {
SelectorWithPseudoClasses(Some(Selector::Class("some_class".into())), vec![]),
SelectorWithPseudoClasses(Some(Selector::Tag("input".into())), vec![]),
]
.into()
.into()
))
);
}
Expand All @@ -131,8 +136,8 @@ mod test {
params: None,
next: None,
}],
), ]
.into()
),]
.into()
))
);
}
Expand All @@ -151,8 +156,8 @@ mod test {
params: Some("4n".to_string()),
next: None,
}],
), ]
.into()
),]
.into()
))
);
}
Expand All @@ -171,8 +176,8 @@ mod test {
params: Some("nav, .posts".to_string()),
next: None,
}],
), ]
.into()
),]
.into()
))
);
}
Expand All @@ -191,8 +196,8 @@ mod test {
params: Some(".test".to_string()),
next: Some("a".to_string()),
}],
), ]
.into()
),]
.into()
))
);
}
Expand All @@ -211,8 +216,8 @@ mod test {
params: Some(".test".to_string()),
next: Some("a".to_string()),
}],
), ]
.into()
),]
.into()
))
);
}
Expand All @@ -239,8 +244,8 @@ mod test {
next: None,
},
],
), ]
.into()
),]
.into()
))
);
}
Expand Down
5 changes: 4 additions & 1 deletion css-minify/src/parsers/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ pub fn space(s: &str) -> IResult<&str, &str> {
is_a(" \t\r\n")(s)
}

pub fn between<'a>(opener: &'a str, closer: &'a str) -> impl FnMut(&'a str) -> IResult<&'a str, &'a str> + 'a {
pub fn between<'a>(
opener: &'a str,
closer: &'a str,
) -> impl FnMut(&'a str) -> IResult<&'a str, &'a str> + 'a {
delimited(tag(opener), take_until(closer), tag(closer))
}

Expand Down
6 changes: 3 additions & 3 deletions css-minify/src/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ mod test {
SelectorWithPseudoClasses(Some(Selector::Id("some_id".into())), vec![]),
SelectorWithPseudoClasses(Some(Selector::Tag("input".into())), vec![]),
]
.into(),
.into(),
parameters: {
let mut tmp = IndexMap::new();
tmp.insert("padding".into(), "5px 3px".into());
Expand All @@ -369,7 +369,7 @@ mod test {
SelectorWithPseudoClasses(Some(Selector::Id("some_id_2".into())), vec![]),
SelectorWithPseudoClasses(Some(Selector::Class("class".into())), vec![]),
]
.into(),
.into(),
parameters: {
let mut tmp = IndexMap::new();
tmp.insert("padding".into(), "5px 4px".into());
Expand All @@ -378,7 +378,7 @@ mod test {
},
},
]
.into();
.into();
assert_eq!(format!("{}", blocks), "#some_id,input{padding:5px 3px;color:white}#some_id_2,.class{padding:5px 4px;color:black}")
}
}

0 comments on commit 5279c81

Please sign in to comment.