2020-07-21 03:57:39 -04:00
|
|
|
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;
|
2020-07-10 11:15:56 -04:00
|
|
|
pub use crate::cfg::Cfg;
|
2019-12-25 04:44:51 -05:00
|
|
|
|
2020-07-10 11:15:56 -04:00
|
|
|
mod cfg;
|
2019-12-29 19:52:45 -05:00
|
|
|
mod err;
|
2020-06-19 03:58:16 -04:00
|
|
|
mod gen;
|
2019-12-29 19:52:45 -05:00
|
|
|
mod pattern;
|
2019-12-25 04:44:51 -05:00
|
|
|
#[macro_use]
|
2019-12-23 06:48:41 -05:00
|
|
|
mod proc;
|
|
|
|
mod spec;
|
2020-07-10 06:40:33 -04:00
|
|
|
mod tests;
|
2019-12-25 04:44:51 -05:00
|
|
|
mod unit;
|
2019-12-23 06:48:41 -05:00
|
|
|
|
2020-07-21 03:57:39 -04:00
|
|
|
pub fn in_place(code: &mut [u8], cfg: &Cfg) -> Result<usize, Error> {
|
2019-12-26 00:17:57 -05:00
|
|
|
let mut proc = Processor::new(code);
|
2020-07-21 09:27:56 -04:00
|
|
|
process_content(&mut proc, cfg, Namespace::Html, None)
|
|
|
|
.map_err(|error_type| Error {
|
|
|
|
error_type,
|
2020-07-21 03:57:39 -04:00
|
|
|
position: proc.read_len(),
|
2020-07-21 09:27:56 -04:00
|
|
|
})?;
|
|
|
|
proc.finish()
|
2019-12-23 06:48:41 -05:00
|
|
|
}
|
2020-01-25 09:25:07 -05:00
|
|
|
|
2020-07-21 03:57:39 -04:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 03:57:39 -04:00
|
|
|
pub fn truncate(code: &mut Vec<u8>, cfg: &Cfg) -> Result<(), Error> {
|
2020-07-11 11:29:34 -04:00
|
|
|
match in_place(code, cfg) {
|
2020-05-12 03:12:29 -04:00
|
|
|
Ok(written_len) => {
|
2020-05-12 03:26:13 -04:00
|
|
|
code.truncate(written_len);
|
2020-05-12 03:12:29 -04:00
|
|
|
Ok(())
|
2020-07-10 11:15:56 -04:00
|
|
|
}
|
2020-05-12 03:12:29 -04:00
|
|
|
Err(e) => Err(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 03:57:39 -04:00
|
|
|
pub fn copy(code: &[u8], cfg: &Cfg) -> Result<Vec<u8>, Error> {
|
2020-05-12 03:12:29 -04:00
|
|
|
let mut copy = code.to_vec();
|
2020-07-11 11:29:34 -04:00
|
|
|
match truncate(&mut copy, cfg) {
|
2020-05-12 03:12:29 -04:00
|
|
|
Ok(()) => Ok(copy),
|
|
|
|
Err(e) => Err(e),
|
|
|
|
}
|
|
|
|
}
|