minify-html/src/lib.rs

52 lines
1.3 KiB
Rust
Raw Normal View History

pub use crate::err::{Error, ErrorType, FriendlyError};
2019-12-25 04:44:51 -05:00
use crate::proc::Processor;
use crate::unit::content::process_content;
2020-06-19 03:58:16 -04:00
use crate::spec::tag::ns::Namespace;
pub use crate::cfg::Cfg;
2019-12-25 04:44:51 -05:00
mod cfg;
mod err;
2020-06-19 03:58:16 -04:00
mod gen;
mod pattern;
2019-12-25 04:44:51 -05:00
#[macro_use]
mod proc;
mod spec;
2020-07-10 06:40:33 -04:00
mod tests;
2019-12-25 04:44:51 -05:00
mod unit;
pub fn in_place(code: &mut [u8], cfg: &Cfg) -> Result<usize, Error> {
let mut proc = Processor::new(code);
process_content(&mut proc, cfg, Namespace::Html, None)
.map_err(|error_type| Error {
error_type,
position: proc.read_len(),
})?;
proc.finish()
}
pub fn in_place_str<'s>(code: &'s mut str, cfg: &Cfg) -> Result<&'s str, Error> {
2020-07-12 02:05:49 -04:00
let bytes = unsafe { code.as_bytes_mut() };
match in_place(bytes, cfg) {
Ok(min_len) => Ok(unsafe { std::str::from_utf8_unchecked(&bytes[..min_len]) }),
Err(e) => Err(e),
}
}
pub fn truncate(code: &mut Vec<u8>, cfg: &Cfg) -> Result<(), Error> {
2020-07-11 11:29:34 -04:00
match in_place(code, cfg) {
Ok(written_len) => {
2020-05-12 03:26:13 -04:00
code.truncate(written_len);
Ok(())
}
Err(e) => Err(e),
}
}
pub fn copy(code: &[u8], cfg: &Cfg) -> Result<Vec<u8>, Error> {
let mut copy = code.to_vec();
2020-07-11 11:29:34 -04:00
match truncate(&mut copy, cfg) {
Ok(()) => Ok(copy),
Err(e) => Err(e),
}
}