From 3e5905b4a27c2378a474b880928cdb258af32676 Mon Sep 17 00:00:00 2001 From: Wilson Lin Date: Sat, 7 Aug 2021 19:57:35 +1000 Subject: [PATCH] Update Ruby library --- ruby/src/lib.rs | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/ruby/src/lib.rs b/ruby/src/lib.rs index 915200b..56b7613 100644 --- a/ruby/src/lib.rs +++ b/ruby/src/lib.rs @@ -1,6 +1,15 @@ -use minify_html::{Cfg, Error, in_place as minify_html_native}; -use rutie::{Boolean, Class, class, Hash, methods, Object, RString, Symbol, VM}; -use std::str::from_utf8_unchecked; +use minify_html::{minify as minify_html_native, Cfg}; +use rutie::{class, methods, Boolean, Class, Hash, Object, RString, Symbol, VM}; +use std::str::from_utf8; + +macro_rules! get_cfg_hash_prop { + ($cfg_hash:ident, $prop:literal) => { + $cfg_hash + .at(&Symbol::new($prop)) + .try_convert_to::() + .map_or(false, |v| v.to_bool()) + }; +} class!(MinifyHtml); @@ -9,7 +18,7 @@ methods! { _itself, fn minify(source: RString, cfg_hash: Hash) -> RString { - let mut code = source + let code = source .map_err(|e| VM::raise_ex(e) ) .unwrap() .to_string() @@ -20,20 +29,19 @@ methods! { .unwrap(); let cfg = &Cfg { - minify_js: cfg_hash - .at(&Symbol::new("minify_js")) - .try_convert_to::() - .map_or(false, |v| v.to_bool()), - minify_css: cfg_hash - .at(&Symbol::new("minify_css")) - .try_convert_to::() - .map_or(false, |v| v.to_bool()), + keep_closing_tags: get_cfg_hash_prop(cfg_hash, "keep_closing_tags"), + keep_comments: get_cfg_hash_prop(cfg_hash, "keep_comments"), + keep_html_and_head_opening_tags: get_cfg_hash_prop(cfg_hash, "keep_html_and_head_opening_tags"), + keep_spaces_between_attributes: get_cfg_hash_prop(cfg_hash, "keep_spaces_between_attributes"), + minify_css: get_cfg_hash_prop(cfg_hash, "minify_css"), + minify_js: get_cfg_hash_prop(cfg_hash, "minify_js"), + remove_bangs: get_cfg_hash_prop(cfg_hash, "remove_bangs"), + remove_processing_instructions: get_cfg_hash_prop(cfg_hash, "remove_processing_instructions"), }; - minify_html_native(&mut code, cfg) - .map_err(|Error { error_type, position }| VM::raise(Class::from_existing("SyntaxError"), format!("{} [Character {}]", error_type.message(), position).as_str())) - .map(|out_len| RString::new_utf8(unsafe { from_utf8_unchecked(&code[0..out_len]) })) - .unwrap() + let out_code = minify_html_native(&code, cfg); + let out_str = from_utf8(&out_code).unwrap(); + RString::new_utf8(out_str).unwrap() } }