From da796a58396fba672ea206dacb225f4b761aa1bd Mon Sep 17 00:00:00 2001 From: Wilson Lin Date: Thu, 26 Dec 2019 13:47:18 +1100 Subject: [PATCH] Refactoring; fix whitespace minification in content --- src/err.rs | 6 +++-- src/proc.rs | 20 +++++++-------- src/unit/attr/mod.rs | 4 +-- src/unit/attr/value.rs | 4 +-- src/unit/bang.rs | 4 +-- src/unit/comment.rs | 4 +-- src/unit/content.rs | 57 +++++++++++++++++++++++++++--------------- src/unit/entity.rs | 52 +++++++++++++++++++++++++------------- src/unit/script.rs | 14 +++++------ src/unit/style.rs | 11 ++++---- src/unit/tag.rs | 4 +-- 11 files changed, 108 insertions(+), 72 deletions(-) diff --git a/src/err.rs b/src/err.rs index 121d86f..aab5e6c 100644 --- a/src/err.rs +++ b/src/err.rs @@ -1,11 +1,13 @@ #[derive(Debug)] pub enum ErrorType { + NoSpaceBeforeAttr, + UnterminatedCssString, + UnterminatedJsString, CharNotFound { need: u8, got: u8 }, MatchNotFound(&'static [u8]), NotFound(&'static str), - NoSpaceBeforeAttr, UnexpectedChar(u8), UnexpectedEnd, } -pub type InternalResult = Result; +pub type ProcessingResult = Result; diff --git a/src/proc.rs b/src/proc.rs index 1ac29a6..347eda5 100644 --- a/src/proc.rs +++ b/src/proc.rs @@ -2,7 +2,7 @@ use std::ops::Index; use phf::Set; -use crate::err::{ErrorType, InternalResult}; +use crate::err::{ErrorType, ProcessingResult}; macro_rules! chain { ($proc:ident $($tail:tt)+) => ({ @@ -158,7 +158,7 @@ impl<'d> Processor<'d> { self._new_match(count, None, RequireReason::Custom) } // Ensure that match is nonempty or return error. - fn _match_require(&self, custom_reason: Option<&'static str>) -> InternalResult<()> { + fn _match_require(&self, custom_reason: Option<&'static str>) -> ProcessingResult<()> { if self.match_len > 0 { Ok(()) } else { @@ -207,10 +207,10 @@ impl<'d> Processor<'d> { } // Assert match. - pub fn require(&self) -> InternalResult<()> { + pub fn require(&self) -> ProcessingResult<()> { self._match_require(None) } - pub fn require_with_reason(&self, reason: &'static str) -> InternalResult<()> { + pub fn require_with_reason(&self, reason: &'static str) -> ProcessingResult<()> { self._match_require(Some(reason)) } // TODO Document @@ -361,20 +361,20 @@ impl<'d> Processor<'d> { pub fn peek_offset_eof(&self, offset: usize) -> Option { self._maybe_read_offset(offset) } - pub fn peek_offset(&self, offset: usize) -> InternalResult { + pub fn peek_offset(&self, offset: usize) -> ProcessingResult { self._maybe_read_offset(offset).ok_or(ErrorType::UnexpectedEnd) } pub fn peek_eof(&self) -> Option { self._maybe_read_offset(0) } - pub fn peek(&self) -> InternalResult { + pub fn peek(&self) -> ProcessingResult { self._maybe_read_offset(0).ok_or(ErrorType::UnexpectedEnd) } // Consuming source characters. /// Skip the next `count` characters (can be zero). /// Will result in an error if exceeds bounds. - pub fn skip_amount(&mut self, count: usize) -> InternalResult<()> { + pub fn skip_amount(&mut self, count: usize) -> ProcessingResult<()> { // Check for zero to prevent underflow as type is usize. if count == 0 || self._in_bounds(count - 1) { self.read_next += count; @@ -385,7 +385,7 @@ impl<'d> Processor<'d> { } /// Skip and return the next character. /// Will result in an error if exceeds bounds. - pub fn skip(&mut self) -> InternalResult { + pub fn skip(&mut self) -> ProcessingResult { if !self.at_end() { let c = self._read_offset(0); self.read_next += 1; @@ -435,7 +435,7 @@ impl<'d> Processor<'d> { } // Shifting characters. - pub fn accept(&mut self) -> InternalResult { + pub fn accept(&mut self) -> ProcessingResult { if !self.at_end() { let c = self._read_offset(0); self._shift(1); @@ -444,7 +444,7 @@ impl<'d> Processor<'d> { Err(ErrorType::UnexpectedEnd) } } - pub fn accept_amount(&mut self, count: usize) -> InternalResult<()> { + pub fn accept_amount(&mut self, count: usize) -> ProcessingResult<()> { // Check for zero to prevent underflow as type is usize. if count == 0 || self._in_bounds(count - 1) { self._shift(count); diff --git a/src/unit/attr/mod.rs b/src/unit/attr/mod.rs index 8f66e38..f8bae90 100644 --- a/src/unit/attr/mod.rs +++ b/src/unit/attr/mod.rs @@ -1,5 +1,5 @@ use crate::proc::Processor; -use crate::err::InternalResult; +use crate::err::ProcessingResult; use crate::spec::codepoint::is_control; use phf::{Set, phf_set}; use crate::unit::attr::value::process_attr_value; @@ -30,7 +30,7 @@ fn is_name_char(c: u8) -> bool { } } -pub fn process_attr<'d, 'p>(proc: &'p mut Processor<'d>) -> InternalResult { +pub fn process_attr(proc: &mut Processor) -> ProcessingResult { // Expect `process_attr` to be called at an attribute. let name = chain!(proc.match_while_pred(is_name_char).expect().keep().slice()); diff --git a/src/unit/attr/value.rs b/src/unit/attr/value.rs index 50f285e..8226cb1 100644 --- a/src/unit/attr/value.rs +++ b/src/unit/attr/value.rs @@ -1,6 +1,6 @@ use phf::{Map, phf_map}; -use crate::err::InternalResult; +use crate::err::ProcessingResult; use crate::proc::Processor; use crate::spec::codepoint::is_whitespace; use crate::unit::attr::AttrType; @@ -207,7 +207,7 @@ macro_rules! consume_attr_value_chars { }; } -pub fn process_attr_value<'d, 'p>(proc: &'p mut Processor<'d>, should_collapse_and_trim_ws: bool) -> InternalResult { +pub fn process_attr_value(proc: &mut Processor, should_collapse_and_trim_ws: bool) -> ProcessingResult { // Processing a quoted attribute value is tricky, due to the fact that // it's not possible to know whether or not to unquote the value until // the value has been processed. For example, decoding an entity could diff --git a/src/unit/bang.rs b/src/unit/bang.rs index b4256dc..dc98eff 100644 --- a/src/unit/bang.rs +++ b/src/unit/bang.rs @@ -1,7 +1,7 @@ use crate::proc::Processor; -use crate::err::InternalResult; +use crate::err::ProcessingResult; -pub fn process_bang<'d, 'p>(proc: &'p mut Processor<'d>) -> InternalResult<()> { +pub fn process_bang(proc: &mut Processor) -> ProcessingResult<()> { chain!(proc.match_seq(b"').keep()); diff --git a/src/unit/comment.rs b/src/unit/comment.rs index 04df4f5..daa9cb4 100644 --- a/src/unit/comment.rs +++ b/src/unit/comment.rs @@ -1,7 +1,7 @@ use crate::proc::Processor; -use crate::err::InternalResult; +use crate::err::ProcessingResult; -pub fn process_comment<'d, 'p>(proc: &'p mut Processor<'d>) -> InternalResult<()> { +pub fn process_comment(proc: &mut Processor) -> ProcessingResult<()> { chain!(proc.match_seq(b"