From aae0c504f6b8176e740d75b1f2cc04acbd65fb8c Mon Sep 17 00:00:00 2001 From: Kornel Date: Tue, 29 Oct 2024 00:40:08 +0000 Subject: [PATCH] Limit visibility where possible --- src/base/align.rs | 2 +- src/base/encoding.rs | 1 + src/base/mod.rs | 6 +++--- src/base/range.rs | 2 +- src/html/local_name.rs | 4 +++- src/html/mod.rs | 2 +- src/html/namespace.rs | 1 + src/html/tag.rs | 1 + src/lib.rs | 1 + src/memory/arena.rs | 2 +- src/memory/limited_vec.rs | 2 +- src/memory/limiter.rs | 1 + src/memory/mod.rs | 4 ++-- src/parser/lexer/lexeme/mod.rs | 12 +++++------ src/parser/lexer/lexeme/token_outline.rs | 6 +++--- src/parser/lexer/mod.rs | 10 +++++----- src/parser/mod.rs | 16 +++++++++------ src/parser/state_machine/mod.rs | 12 +++++------ src/parser/tag_scanner/mod.rs | 7 ++++--- .../tree_builder_simulator/ambiguity_guard.rs | 2 +- src/parser/tree_builder_simulator/mod.rs | 6 +++--- src/rewritable_units/mod.rs | 4 ++-- src/rewritable_units/tokens/attributes.rs | 11 +++++++--- src/rewritable_units/tokens/capturer/mod.rs | 4 ++-- .../tokens/capturer/text_decoder.rs | 2 +- .../tokens/capturer/to_token.rs | 4 ++-- src/rewritable_units/tokens/mod.rs | 2 ++ src/rewritable_units/tokens/start_tag.rs | 4 +++- src/rewriter/handlers_dispatcher.rs | 8 ++++---- src/rewriter/mod.rs | 2 +- src/rewriter/rewrite_controller.rs | 6 +++--- src/selectors_vm/ast.rs | 16 +++++++-------- src/selectors_vm/attribute_matcher.rs | 2 +- src/selectors_vm/compiler.rs | 4 ++-- src/selectors_vm/mod.rs | 20 +++++++++---------- src/selectors_vm/parser.rs | 6 +++--- src/selectors_vm/program.rs | 10 +++++----- src/selectors_vm/stack.rs | 12 +++++------ src/transform_stream/dispatcher.rs | 7 ++++++- src/transform_stream/mod.rs | 9 ++++++--- 40 files changed, 131 insertions(+), 102 deletions(-) diff --git a/src/base/align.rs b/src/base/align.rs index dc5802e2..abe831fa 100644 --- a/src/base/align.rs +++ b/src/base/align.rs @@ -1,4 +1,4 @@ -pub trait Align { +pub(crate) trait Align { fn align(&mut self, offset: usize); } diff --git a/src/base/encoding.rs b/src/base/encoding.rs index 2f9504d2..03ba25b1 100644 --- a/src/base/encoding.rs +++ b/src/base/encoding.rs @@ -250,6 +250,7 @@ fn encoding_to_index(encoding: AsciiCompatibleEncoding) -> usize { /// This is, for instance, used to adapt the charset dynamically in a [`crate::HtmlRewriter`] if it /// encounters a `meta` tag that specifies the charset (that behavior is dependent on /// [`crate::Settings::adjust_charset_on_meta_tag`]). +// Pub only for integration tests #[derive(Clone)] pub struct SharedEncoding { encoding: Arc, diff --git a/src/base/mod.rs b/src/base/mod.rs index e9f4b238..edf8efcd 100644 --- a/src/base/mod.rs +++ b/src/base/mod.rs @@ -6,7 +6,7 @@ mod bytes; mod encoding; mod range; -pub use self::align::Align; -pub use self::bytes::{Bytes, HasReplacementsError}; +pub(crate) use self::align::Align; +pub(crate) use self::bytes::{Bytes, HasReplacementsError}; pub use self::encoding::SharedEncoding; -pub use self::range::Range; +pub(crate) use self::range::Range; diff --git a/src/base/range.rs b/src/base/range.rs index a85585fd..5bec4462 100644 --- a/src/base/range.rs +++ b/src/base/range.rs @@ -3,7 +3,7 @@ use super::Align; // NOTE: std::ops::Range implements iterator and, thus, doesn't implement Copy. // See: https://github.com/rust-lang/rust/pull/27186 #[derive(Clone, Copy, Default, Debug)] -pub struct Range { +pub(crate) struct Range { pub start: usize, pub end: usize, } diff --git a/src/html/local_name.rs b/src/html/local_name.rs index 80d9320e..a334e000 100644 --- a/src/html/local_name.rs +++ b/src/html/local_name.rs @@ -25,6 +25,8 @@ use encoding_rs::Encoding; // for digits, but considering that tag name can't start with a digit // we are safe here, since we'll just get first character shifted left // by zeroes as repetitave 1 digits get added to the hash. +// +// Pub only for integration tests #[derive(Debug, PartialEq, Eq, Copy, Clone, Default, Hash)] pub struct LocalNameHash(Option); @@ -109,7 +111,7 @@ pub enum LocalName<'i> { impl<'i> LocalName<'i> { #[inline] #[must_use] - pub fn new(input: &'i Bytes<'i>, range: Range, hash: LocalNameHash) -> Self { + pub(crate) fn new(input: &'i Bytes<'i>, range: Range, hash: LocalNameHash) -> Self { if hash.is_empty() { LocalName::Bytes(input.slice(range)) } else { diff --git a/src/html/mod.rs b/src/html/mod.rs index 0257c9cb..2e23a89b 100644 --- a/src/html/mod.rs +++ b/src/html/mod.rs @@ -7,5 +7,5 @@ mod text_type; pub use self::local_name::{LocalName, LocalNameHash}; pub use self::namespace::Namespace; -pub use self::tag::*; +pub use self::tag::Tag; pub use self::text_type::TextType; diff --git a/src/html/namespace.rs b/src/html/namespace.rs index 19711310..fb9c03a1 100644 --- a/src/html/namespace.rs +++ b/src/html/namespace.rs @@ -1,3 +1,4 @@ +// Pub only for integration tests #[derive(Default, Copy, Clone, Eq, PartialEq, Debug)] pub enum Namespace { #[default] diff --git a/src/html/tag.rs b/src/html/tag.rs index c63f0dc8..20ad90db 100644 --- a/src/html/tag.rs +++ b/src/html/tag.rs @@ -1,5 +1,6 @@ macro_rules! declare_tags { ($($name:ident = $val:expr),+) => { + // Pub only for integration tests #[repr(u64)] #[derive(Debug, Copy, Clone)] pub enum Tag { diff --git a/src/lib.rs b/src/lib.rs index b1f0ab44..f78fa3b3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,6 +18,7 @@ //! [`rewrite_str`]: fn.rewrite_str.html #![allow(clippy::default_trait_access)] #![allow(clippy::module_name_repetitions)] +#![allow(clippy::redundant_pub_crate)] #![cfg_attr(not(any(feature = "integration_test", test)), warn(missing_docs))] #![cfg_attr(any(feature = "integration_test", test), allow(unnameable_types))] diff --git a/src/memory/arena.rs b/src/memory/arena.rs index 6ff81572..ff49f9f8 100644 --- a/src/memory/arena.rs +++ b/src/memory/arena.rs @@ -3,7 +3,7 @@ use super::{MemoryLimitExceededError, SharedMemoryLimiter}; /// Preallocated region of memory that can grow and never deallocates during the lifetime of /// the limiter. #[derive(Debug)] -pub struct Arena { +pub(crate) struct Arena { limiter: SharedMemoryLimiter, data: Vec, } diff --git a/src/memory/limited_vec.rs b/src/memory/limited_vec.rs index 07060679..6db9badc 100644 --- a/src/memory/limited_vec.rs +++ b/src/memory/limited_vec.rs @@ -7,7 +7,7 @@ use std::vec::Drain; use super::{MemoryLimitExceededError, SharedMemoryLimiter}; #[derive(Debug)] -pub struct LimitedVec { +pub(crate) struct LimitedVec { limiter: SharedMemoryLimiter, vec: Vec, } diff --git a/src/memory/limiter.rs b/src/memory/limiter.rs index 56934654..27e5a7fb 100644 --- a/src/memory/limiter.rs +++ b/src/memory/limiter.rs @@ -10,6 +10,7 @@ use thiserror::Error; #[error("The memory limit has been exceeded.")] pub struct MemoryLimitExceededError; +// Pub only for integration tests #[derive(Debug, Clone)] pub struct SharedMemoryLimiter { current_usage: Arc, diff --git a/src/memory/mod.rs b/src/memory/mod.rs index 637f9e2d..8f0039ef 100644 --- a/src/memory/mod.rs +++ b/src/memory/mod.rs @@ -2,6 +2,6 @@ mod arena; mod limited_vec; mod limiter; -pub use arena::Arena; -pub use limited_vec::LimitedVec; +pub(crate) use arena::Arena; +pub(crate) use limited_vec::LimitedVec; pub use limiter::{MemoryLimitExceededError, SharedMemoryLimiter}; diff --git a/src/parser/lexer/lexeme/mod.rs b/src/parser/lexer/lexeme/mod.rs index fa07c740..06f1cde1 100644 --- a/src/parser/lexer/lexeme/mod.rs +++ b/src/parser/lexer/lexeme/mod.rs @@ -3,9 +3,9 @@ mod token_outline; use crate::base::{Bytes, Range}; use std::fmt::{self, Debug}; -pub use self::token_outline::*; +pub(crate) use self::token_outline::*; -pub struct Lexeme<'i, T> { +pub(crate) struct Lexeme<'i, T> { input: Bytes<'i>, raw_range: Range, pub(super) token_outline: T, @@ -15,7 +15,7 @@ pub type TagLexeme<'i> = Lexeme<'i, TagTokenOutline>; pub type NonTagContentLexeme<'i> = Lexeme<'i, Option>; impl<'i, T> Lexeme<'i, T> { - pub fn new(input: Bytes<'i>, token_outline: T, raw_range: Range) -> Self { + pub const fn new(input: Bytes<'i>, token_outline: T, raw_range: Range) -> Self { Lexeme { input, raw_range, @@ -24,17 +24,17 @@ impl<'i, T> Lexeme<'i, T> { } #[inline] - pub fn input(&self) -> &Bytes<'i> { + pub const fn input(&self) -> &Bytes<'i> { &self.input } #[inline] - pub fn token_outline(&self) -> &T { + pub const fn token_outline(&self) -> &T { &self.token_outline } #[inline] - pub fn raw_range(&self) -> Range { + pub const fn raw_range(&self) -> Range { self.raw_range } diff --git a/src/parser/lexer/lexeme/token_outline.rs b/src/parser/lexer/lexeme/token_outline.rs index 96a49f03..f70002c1 100644 --- a/src/parser/lexer/lexeme/token_outline.rs +++ b/src/parser/lexer/lexeme/token_outline.rs @@ -3,7 +3,7 @@ use crate::html::{LocalNameHash, Namespace, TextType}; use crate::parser::AttributeBuffer; #[derive(Debug, Default, Copy, Clone)] -pub struct AttributeOutline { +pub(crate) struct AttributeOutline { pub name: Range, pub value: Range, pub raw_range: Range, @@ -19,7 +19,7 @@ impl Align for AttributeOutline { } #[derive(Debug)] -pub enum TagTokenOutline { +pub(crate) enum TagTokenOutline { StartTag { name: Range, name_hash: LocalNameHash, @@ -35,7 +35,7 @@ pub enum TagTokenOutline { } #[derive(Debug)] -pub enum NonTagContentTokenOutline { +pub(crate) enum NonTagContentTokenOutline { Text(TextType), Comment(Range), diff --git a/src/parser/lexer/mod.rs b/src/parser/lexer/mod.rs index a4036ea4..4317443d 100644 --- a/src/parser/lexer/mod.rs +++ b/src/parser/lexer/mod.rs @@ -4,7 +4,7 @@ mod actions; mod conditions; mod lexeme; -pub use self::lexeme::*; +pub(crate) use self::lexeme::*; use crate::base::{Align, Range}; use crate::html::{LocalNameHash, Namespace, TextType}; use crate::parser::state_machine::{ @@ -13,7 +13,7 @@ use crate::parser::state_machine::{ use crate::parser::{ParserContext, ParserDirective, ParsingAmbiguityError, TreeBuilderFeedback}; use crate::rewriter::RewritingError; -pub trait LexemeSink { +pub(crate) trait LexemeSink { fn handle_tag(&mut self, lexeme: &TagLexeme<'_>) -> Result; fn handle_non_tag_content( &mut self, @@ -21,11 +21,11 @@ pub trait LexemeSink { ) -> Result<(), RewritingError>; } -pub type State = fn(&mut Lexer, context: &mut ParserContext, &[u8]) -> StateResult; +pub(crate) type State = fn(&mut Lexer, context: &mut ParserContext, &[u8]) -> StateResult; -pub type AttributeBuffer = Vec; +pub(crate) type AttributeBuffer = Vec; -pub struct Lexer { +pub(crate) struct Lexer { next_pos: usize, is_last_input: bool, lexeme_start: usize, diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 6483c512..1f128f06 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -6,12 +6,12 @@ mod tag_scanner; mod tree_builder_simulator; use self::lexer::Lexer; -pub use self::lexer::{ +pub(crate) use self::lexer::{ AttributeBuffer, AttributeOutline, Lexeme, LexemeSink, NonTagContentLexeme, NonTagContentTokenOutline, TagLexeme, TagTokenOutline, }; use self::state_machine::{ActionError, ParsingTermination, StateMachine}; -pub use self::tag_scanner::TagHintSink; +pub(crate) use self::tag_scanner::TagHintSink; use self::tag_scanner::TagScanner; pub use self::tree_builder_simulator::ParsingAmbiguityError; use self::tree_builder_simulator::{TreeBuilderFeedback, TreeBuilderSimulator}; @@ -23,25 +23,28 @@ use cfg_if::cfg_if; // to consumer to switch the parser back to the tag scan mode in // the tag handler. #[derive(Clone, Copy, Debug)] -pub enum ParserDirective { +pub(crate) enum ParserDirective { WherePossibleScanForTagsOnly, Lex, } -pub struct ParserContext { +pub(crate) struct ParserContext { output_sink: S, tree_builder_simulator: TreeBuilderSimulator, } -pub trait ParserOutputSink: LexemeSink + TagHintSink {} +pub(crate) trait ParserOutputSink: LexemeSink + TagHintSink {} -pub struct Parser { +// Pub only for integration tests +pub struct Parser { lexer: Lexer, tag_scanner: TagScanner, current_directive: ParserDirective, context: ParserContext, } +// public only for integration tests +#[allow(private_bounds, private_interfaces)] impl Parser { #[inline] #[must_use] @@ -112,6 +115,7 @@ cfg_if! { if #[cfg(feature = "integration_test")] { use crate::html::{LocalNameHash, TextType}; + #[allow(private_bounds)] impl Parser { pub fn switch_text_type(&mut self, text_type: TextType) { match self.current_directive { diff --git a/src/parser/state_machine/mod.rs b/src/parser/state_machine/mod.rs index 8fbb9ce5..dee177ff 100644 --- a/src/parser/state_machine/mod.rs +++ b/src/parser/state_machine/mod.rs @@ -10,7 +10,7 @@ use crate::rewriter::RewritingError; use std::fmt::{self, Debug}; use std::mem; -pub enum FeedbackDirective { +pub(crate) enum FeedbackDirective { ApplyUnhandledFeedback(TreeBuilderFeedback), Skip, None, @@ -39,7 +39,7 @@ impl Debug for FeedbackDirective { } #[derive(Debug)] -pub struct StateMachineBookmark { +pub(crate) struct StateMachineBookmark { cdata_allowed: bool, text_type: TextType, last_start_tag_name_hash: LocalNameHash, @@ -48,7 +48,7 @@ pub struct StateMachineBookmark { feedback_directive: FeedbackDirective, } -pub enum ActionError { +pub(crate) enum ActionError { RewritingError(RewritingError), ParserDirectiveChangeRequired(ParserDirective, StateMachineBookmark), } @@ -72,7 +72,7 @@ pub type ActionResult = Result<(), ActionError>; pub type StateResult = Result<(), ParsingTermination>; pub type ParseResult = Result; -pub trait StateMachineActions { +pub(crate) trait StateMachineActions { type Context; fn emit_eof(&mut self, context: &mut Self::Context, input: &[u8]) -> ActionResult; @@ -131,12 +131,12 @@ pub trait StateMachineActions { fn leave_cdata(&mut self, context: &mut Self::Context, input: &[u8]); } -pub trait StateMachineConditions { +pub(crate) trait StateMachineConditions { fn is_appropriate_end_tag(&self) -> bool; fn cdata_allowed(&self) -> bool; } -pub trait StateMachine: StateMachineActions + StateMachineConditions { +pub(crate) trait StateMachine: StateMachineActions + StateMachineConditions { cdata_section_states_group!(); data_states_group!(); plaintext_states_group!(); diff --git a/src/parser/tag_scanner/mod.rs b/src/parser/tag_scanner/mod.rs index ee013fba..93982e0a 100644 --- a/src/parser/tag_scanner/mod.rs +++ b/src/parser/tag_scanner/mod.rs @@ -9,7 +9,7 @@ use crate::parser::{ParserContext, ParserDirective, ParsingAmbiguityError, TreeB use crate::rewriter::RewritingError; use std::cmp::min; -pub trait TagHintSink { +pub(crate) trait TagHintSink { fn handle_start_tag_hint( &mut self, name: LocalName<'_>, @@ -21,7 +21,8 @@ pub trait TagHintSink { ) -> Result; } -pub type State = fn(&mut TagScanner, context: &mut ParserContext, &[u8]) -> StateResult; +pub(crate) type State = + fn(&mut TagScanner, context: &mut ParserContext, &[u8]) -> StateResult; /// Tag scanner skips the majority of lexer operations and, thus, /// is faster. It also has much less requirements for buffering which makes it more @@ -34,7 +35,7 @@ pub type State = fn(&mut TagScanner, context: &mut ParserContext, &[u8] /// of the input (e.g. ` { +pub(crate) struct TagScanner { next_pos: usize, is_last_input: bool, tag_start: Option, diff --git a/src/parser/tree_builder_simulator/ambiguity_guard.rs b/src/parser/tree_builder_simulator/ambiguity_guard.rs index 2f457cfd..556996fb 100644 --- a/src/parser/tree_builder_simulator/ambiguity_guard.rs +++ b/src/parser/tree_builder_simulator/ambiguity_guard.rs @@ -132,7 +132,7 @@ enum State { InOrAfterFrameset, } -pub struct AmbiguityGuard { +pub(crate) struct AmbiguityGuard { state: State, } diff --git a/src/parser/tree_builder_simulator/mod.rs b/src/parser/tree_builder_simulator/mod.rs index 1c9078d5..c3ebb4ac 100644 --- a/src/parser/tree_builder_simulator/mod.rs +++ b/src/parser/tree_builder_simulator/mod.rs @@ -24,7 +24,7 @@ pub use self::ambiguity_guard::ParsingAmbiguityError; const DEFAULT_NS_STACK_CAPACITY: usize = 256; #[must_use] -pub enum TreeBuilderFeedback { +pub(crate) enum TreeBuilderFeedback { SwitchTextType(TextType), SetAllowCdata(bool), #[allow(clippy::type_complexity)] @@ -114,7 +114,7 @@ fn is_html_integration_point_in_svg(tag_name: LocalNameHash) -> bool { } // TODO limit ns stack -pub struct TreeBuilderSimulator { +pub(crate) struct TreeBuilderSimulator { ns_stack: Vec, current_ns: Namespace, ambiguity_guard: AmbiguityGuard, @@ -173,7 +173,7 @@ impl TreeBuilderSimulator { } #[inline] - pub fn current_ns(&self) -> Namespace { + pub const fn current_ns(&self) -> Namespace { self.current_ns } diff --git a/src/rewritable_units/mod.rs b/src/rewritable_units/mod.rs index 922e964f..da5521d7 100644 --- a/src/rewritable_units/mod.rs +++ b/src/rewritable_units/mod.rs @@ -93,7 +93,7 @@ mod test_utils { use encoding_rs::Encoding; use std::borrow::Cow; - pub fn encoded(input: &str) -> Vec<(Vec, &'static Encoding)> { + pub(crate) fn encoded(input: &str) -> Vec<(Vec, &'static Encoding)> { ASCII_COMPATIBLE_ENCODINGS .iter() .filter_map(|enc| { @@ -115,7 +115,7 @@ mod test_utils { .collect() } - pub fn rewrite_html<'h>( + pub(crate) fn rewrite_html<'h>( html: &[u8], encoding: &'static Encoding, element_content_handlers: Vec<(Cow<'_, Selector>, ElementContentHandlers<'h>)>, diff --git a/src/rewritable_units/tokens/attributes.rs b/src/rewritable_units/tokens/attributes.rs index fed2e06d..c55e18b9 100644 --- a/src/rewritable_units/tokens/attributes.rs +++ b/src/rewritable_units/tokens/attributes.rs @@ -43,7 +43,12 @@ pub struct Attribute<'i> { impl<'i> Attribute<'i> { #[inline] #[must_use] - fn new(name: Bytes<'i>, value: Bytes<'i>, raw: Bytes<'i>, encoding: &'static Encoding) -> Self { + const fn new( + name: Bytes<'i>, + value: Bytes<'i>, + raw: Bytes<'i>, + encoding: &'static Encoding, + ) -> Self { Attribute { name, value, @@ -144,7 +149,7 @@ impl Debug for Attribute<'_> { } } -pub struct Attributes<'i> { +pub(crate) struct Attributes<'i> { input: &'i Bytes<'i>, attribute_buffer: &'i AttributeBuffer, items: LazyCell>>, @@ -235,7 +240,7 @@ impl<'i> Attributes<'i> { } #[cfg(test)] - pub fn raw_attributes(&self) -> (&'i Bytes<'i>, &'i AttributeBuffer) { + pub(crate) const fn raw_attributes(&self) -> (&'i Bytes<'i>, &'i AttributeBuffer) { (self.input, self.attribute_buffer) } } diff --git a/src/rewritable_units/tokens/capturer/mod.rs b/src/rewritable_units/tokens/capturer/mod.rs index f46b3abd..4a9957ad 100644 --- a/src/rewritable_units/tokens/capturer/mod.rs +++ b/src/rewritable_units/tokens/capturer/mod.rs @@ -8,7 +8,7 @@ use crate::parser::Lexeme; use crate::rewriter::RewritingError; use bitflags::bitflags; -pub use self::to_token::{ToToken, ToTokenResult}; +pub(crate) use self::to_token::{ToToken, ToTokenResult}; bitflags! { #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -22,7 +22,7 @@ bitflags! { } #[derive(Debug)] -pub enum TokenCapturerEvent<'i> { +pub(crate) enum TokenCapturerEvent<'i> { LexemeConsumed, TokenProduced(Box>), } diff --git a/src/rewritable_units/tokens/capturer/text_decoder.rs b/src/rewritable_units/tokens/capturer/text_decoder.rs index 171b71a7..1a81e3e3 100644 --- a/src/rewritable_units/tokens/capturer/text_decoder.rs +++ b/src/rewritable_units/tokens/capturer/text_decoder.rs @@ -14,7 +14,7 @@ macro_rules! emit { }}; } -pub struct TextDecoder { +pub(crate) struct TextDecoder { encoding: SharedEncoding, pending_text_streaming_decoder: Option, text_buffer: String, diff --git a/src/rewritable_units/tokens/capturer/to_token.rs b/src/rewritable_units/tokens/capturer/to_token.rs index dce8c45b..2d042847 100644 --- a/src/rewritable_units/tokens/capturer/to_token.rs +++ b/src/rewritable_units/tokens/capturer/to_token.rs @@ -3,7 +3,7 @@ use crate::html::TextType; use crate::parser::{NonTagContentLexeme, NonTagContentTokenOutline, TagLexeme, TagTokenOutline}; use encoding_rs::Encoding; -pub enum ToTokenResult<'i> { +pub(crate) enum ToTokenResult<'i> { Token(Box>), Text(TextType), None, @@ -16,7 +16,7 @@ impl<'i> From> for ToTokenResult<'i> { } } -pub trait ToToken { +pub(crate) trait ToToken { fn to_token( &self, capture_flags: &mut TokenCaptureFlags, diff --git a/src/rewritable_units/tokens/mod.rs b/src/rewritable_units/tokens/mod.rs index ce08f8d9..e2af3f8f 100644 --- a/src/rewritable_units/tokens/mod.rs +++ b/src/rewritable_units/tokens/mod.rs @@ -7,6 +7,7 @@ pub(super) use self::attributes::Attributes; pub use self::attributes::{Attribute, AttributeNameError}; pub use self::capturer::*; +// Pub only for integration tests pub trait Serialize { fn to_bytes(&self, output_handler: &mut dyn FnMut(&[u8])); } @@ -57,6 +58,7 @@ pub use self::end_tag::EndTag; pub use self::start_tag::StartTag; pub use self::text_chunk::TextChunk; +// Pub only for integration tests #[derive(Debug)] pub enum Token<'i> { TextChunk(TextChunk<'i>), diff --git a/src/rewritable_units/tokens/start_tag.rs b/src/rewritable_units/tokens/start_tag.rs index 3a689e3c..0f1fae6e 100644 --- a/src/rewritable_units/tokens/start_tag.rs +++ b/src/rewritable_units/tokens/start_tag.rs @@ -169,7 +169,9 @@ impl<'i> StartTag<'i> { } #[cfg(test)] - pub(crate) fn raw_attributes(&self) -> (&'i Bytes<'i>, &'i crate::parser::AttributeBuffer) { + pub(crate) const fn raw_attributes( + &self, + ) -> (&'i Bytes<'i>, &'i crate::parser::AttributeBuffer) { self.attributes.raw_attributes() } } diff --git a/src/rewriter/handlers_dispatcher.rs b/src/rewriter/handlers_dispatcher.rs index db03f2af..9eb49869 100644 --- a/src/rewriter/handlers_dispatcher.rs +++ b/src/rewriter/handlers_dispatcher.rs @@ -4,7 +4,7 @@ use crate::rewritable_units::{DocumentEnd, Element, StartTag, Token, TokenCaptur use crate::selectors_vm::MatchInfo; #[derive(Copy, Clone, Default, Debug, PartialEq, Eq, Hash)] -pub struct SelectorHandlersLocator { +pub(crate) struct SelectorHandlersLocator { pub element_handler_idx: Option, pub comment_handler_idx: Option, pub text_handler_idx: Option, @@ -59,7 +59,7 @@ impl HandlerVec { } #[inline] - pub fn has_active(&self) -> bool { + pub const fn has_active(&self) -> bool { self.user_count > 0 } @@ -112,7 +112,7 @@ impl HandlerVec { } } -pub struct ContentHandlersDispatcher<'h, H: HandlerTypes> { +pub(crate) struct ContentHandlersDispatcher<'h, H: HandlerTypes> { doctype_handlers: HandlerVec>, comment_handlers: HandlerVec>, text_handlers: HandlerVec>, @@ -180,7 +180,7 @@ impl<'h, H: HandlerTypes> ContentHandlersDispatcher<'h, H> { } #[inline] - pub fn has_matched_elements_with_removed_content(&self) -> bool { + pub const fn has_matched_elements_with_removed_content(&self) -> bool { self.matched_elements_with_removed_content > 0 } diff --git a/src/rewriter/mod.rs b/src/rewriter/mod.rs index d9135fc5..aae83b74 100644 --- a/src/rewriter/mod.rs +++ b/src/rewriter/mod.rs @@ -780,8 +780,8 @@ mod tests { mod fatal_errors { use super::*; - use crate::errors::MemoryLimitExceededError; use crate::html_content::Comment; + use crate::memory::MemoryLimitExceededError; use crate::rewritable_units::{Element, TextChunk}; fn create_rewriter( diff --git a/src/rewriter/rewrite_controller.rs b/src/rewriter/rewrite_controller.rs index 65c743ee..6b931ab5 100644 --- a/src/rewriter/rewrite_controller.rs +++ b/src/rewriter/rewrite_controller.rs @@ -7,7 +7,7 @@ use crate::transform_stream::*; use hashbrown::HashSet; #[derive(Default)] -pub struct ElementDescriptor { +pub(crate) struct ElementDescriptor { pub matched_content_handlers: HashSet, pub end_tag_handler_idx: Option, pub remove_content: bool, @@ -22,14 +22,14 @@ impl ElementData for ElementDescriptor { } } -pub struct HtmlRewriteController<'h, H: HandlerTypes> { +pub(crate) struct HtmlRewriteController<'h, H: HandlerTypes> { handlers_dispatcher: ContentHandlersDispatcher<'h, H>, selector_matching_vm: Option>, } impl<'h, H: HandlerTypes> HtmlRewriteController<'h, H> { #[inline] - pub fn new( + pub(crate) const fn new( handlers_dispatcher: ContentHandlersDispatcher<'h, H>, selector_matching_vm: Option>, ) -> Self { diff --git a/src/selectors_vm/ast.rs b/src/selectors_vm/ast.rs index 685954b6..6300647d 100644 --- a/src/selectors_vm/ast.rs +++ b/src/selectors_vm/ast.rs @@ -6,7 +6,7 @@ use std::fmt::{self, Debug, Formatter}; use std::hash::Hash; #[derive(PartialEq, Eq, Debug, Copy, Clone)] -pub struct NthChild { +pub(crate) struct NthChild { step: i32, offset: i32, } @@ -46,7 +46,7 @@ impl NthChild { } #[derive(PartialEq, Eq, Debug)] -pub enum OnTagNameExpr { +pub(crate) enum OnTagNameExpr { ExplicitAny, Unmatchable, LocalName(String), @@ -55,7 +55,7 @@ pub enum OnTagNameExpr { } #[derive(Eq, PartialEq)] -pub struct AttributeComparisonExpr { +pub(crate) struct AttributeComparisonExpr { pub name: String, pub value: String, pub case_sensitivity: ParsedCaseSensitivity, @@ -104,7 +104,7 @@ impl Debug for AttributeComparisonExpr { /// An attribute check when attributes are received and parsed. #[derive(PartialEq, Eq, Debug)] -pub enum OnAttributesExpr { +pub(crate) enum OnAttributesExpr { Id(String), Class(String), AttributeExists(String), @@ -173,7 +173,7 @@ impl From<&Component> for Condition { } #[derive(PartialEq, Eq, Debug)] -pub struct Expr +pub(crate) struct Expr where E: PartialEq + Eq + Debug, { @@ -195,7 +195,7 @@ where } #[derive(PartialEq, Eq, Debug, Default)] -pub struct Predicate { +pub(crate) struct Predicate { pub on_tag_name_exprs: Vec>, pub on_attr_exprs: Vec>, } @@ -219,7 +219,7 @@ impl Predicate { } #[derive(PartialEq, Eq, Debug)] -pub struct AstNode

+pub(crate) struct AstNode

where P: Hash + Eq, { @@ -244,7 +244,7 @@ where } #[derive(Default, PartialEq, Eq, Debug)] -pub struct Ast

+pub(crate) struct Ast

where P: PartialEq + Eq + Copy + Debug + Hash, { diff --git a/src/selectors_vm/attribute_matcher.rs b/src/selectors_vm/attribute_matcher.rs index ade4146d..21b11944 100644 --- a/src/selectors_vm/attribute_matcher.rs +++ b/src/selectors_vm/attribute_matcher.rs @@ -20,7 +20,7 @@ const fn is_attr_whitespace(b: u8) -> bool { type MemoizedAttrValue<'i> = LazyCell>>; -pub struct AttributeMatcher<'i> { +pub(crate) struct AttributeMatcher<'i> { input: &'i Bytes<'i>, attributes: &'i AttributeBuffer, id: MemoizedAttrValue<'i>, diff --git a/src/selectors_vm/compiler.rs b/src/selectors_vm/compiler.rs index 345b9432..7e124df7 100644 --- a/src/selectors_vm/compiler.rs +++ b/src/selectors_vm/compiler.rs @@ -24,7 +24,7 @@ struct ExprSet { pub attribute_exprs: Vec, } -pub struct AttrExprOperands { +pub(crate) struct AttrExprOperands { pub name: Bytes<'static>, pub value: Bytes<'static>, pub case_sensitivity: ParsedCaseSensitivity, @@ -187,7 +187,7 @@ impl Compilable for Expr { } } -pub struct Compiler

+pub(crate) struct Compiler

where P: PartialEq + Eq + Copy + Debug + Hash, { diff --git a/src/selectors_vm/mod.rs b/src/selectors_vm/mod.rs index c0c5dc3b..20332df4 100644 --- a/src/selectors_vm/mod.rs +++ b/src/selectors_vm/mod.rs @@ -15,20 +15,20 @@ use crate::memory::{MemoryLimitExceededError, SharedMemoryLimiter}; use crate::transform_stream::AuxStartTagInfo; use encoding_rs::Encoding; -pub use self::ast::*; -pub use self::attribute_matcher::AttributeMatcher; -pub use self::compiler::Compiler; +pub(crate) use self::ast::*; +pub(crate) use self::attribute_matcher::AttributeMatcher; +pub(crate) use self::compiler::Compiler; pub use self::error::SelectorError; pub use self::parser::Selector; -pub use self::program::{ExecutionBranch, Program, TryExecResult}; -pub use self::stack::{ChildCounter, ElementData, Stack, StackItem}; +pub(crate) use self::program::{ExecutionBranch, Program, TryExecResult}; +pub(crate) use self::stack::{ChildCounter, ElementData, Stack, StackItem}; -pub struct MatchInfo

{ +pub(crate) struct MatchInfo

{ pub payload: P, pub with_content: bool, } -pub type AuxStartTagInfoRequest = Box< +pub(crate) type AuxStartTagInfoRequest = Box< dyn FnOnce( &mut SelectorMatchingVm, AuxStartTagInfo<'_>, @@ -37,7 +37,7 @@ pub type AuxStartTagInfoRequest = Box< + Send, >; -pub enum VmError { +pub(crate) enum VmError { InfoRequest(AuxStartTagInfoRequest), MemoryLimitExceeded(MemoryLimitExceededError), } @@ -69,7 +69,7 @@ struct Bailout { } /// A container for tracking state from various places on the stack. -pub struct SelectorState<'i> { +pub(crate) struct SelectorState<'i> { pub cumulative: &'i ChildCounter, pub typed: Option<&'i ChildCounter>, } @@ -140,7 +140,7 @@ macro_rules! aux_info_request { }; } -pub struct SelectorMatchingVm { +pub(crate) struct SelectorMatchingVm { program: Program, stack: Stack, enable_esi_tags: bool, diff --git a/src/selectors_vm/parser.rs b/src/selectors_vm/parser.rs index bba71feb..0ae36708 100644 --- a/src/selectors_vm/parser.rs +++ b/src/selectors_vm/parser.rs @@ -9,7 +9,7 @@ use std::fmt; use std::str::FromStr; #[derive(Debug, Clone, Eq, PartialEq)] -pub struct SelectorImplDescriptor; +pub(crate) struct SelectorImplDescriptor; impl SelectorImpl for SelectorImplDescriptor { type AttrValue = String; @@ -29,7 +29,7 @@ impl SelectorImpl for SelectorImplDescriptor { } #[derive(PartialEq, Eq, Clone, Debug, Hash)] -pub enum PseudoElementStub {} +pub(crate) enum PseudoElementStub {} impl ToCss for PseudoElementStub { fn to_css(&self, _dest: &mut W) -> fmt::Result { @@ -43,7 +43,7 @@ impl PseudoElement for PseudoElementStub { } #[derive(PartialEq, Eq, Clone, Debug, Hash)] -pub enum NonTSPseudoClassStub {} +pub(crate) enum NonTSPseudoClassStub {} impl NonTSPseudoClass for NonTSPseudoClassStub { type Impl = SelectorImplDescriptor; diff --git a/src/selectors_vm/program.rs b/src/selectors_vm/program.rs index 289f732d..a9316f1f 100644 --- a/src/selectors_vm/program.rs +++ b/src/selectors_vm/program.rs @@ -6,10 +6,10 @@ use hashbrown::HashSet; use std::hash::Hash; use std::ops::Range; -pub type AddressRange = Range; +pub(crate) type AddressRange = Range; #[derive(Debug, PartialEq, Eq)] -pub struct ExecutionBranch

+pub(crate) struct ExecutionBranch

where P: Hash + Eq, { @@ -19,7 +19,7 @@ where } /// The result of trying to execute an instruction without having parsed all attributes -pub enum TryExecResult<'i, P> +pub(crate) enum TryExecResult<'i, P> where P: Hash + Eq, { @@ -31,7 +31,7 @@ where Fail, } -pub struct Instruction

+pub(crate) struct Instruction

where P: Hash + Eq, { @@ -89,7 +89,7 @@ where } } -pub struct Program

+pub(crate) struct Program

where P: Hash + Eq, { diff --git a/src/selectors_vm/stack.rs b/src/selectors_vm/stack.rs index 6552aa8b..313b177d 100644 --- a/src/selectors_vm/stack.rs +++ b/src/selectors_vm/stack.rs @@ -37,20 +37,20 @@ fn is_void_element(local_name: &LocalName<'_>, enable_esi_tags: bool) -> bool { false } -pub trait ElementData: Default + 'static { +pub(crate) trait ElementData: Default + 'static { type MatchPayload: PartialEq + Eq + Copy + Debug + Hash + 'static; fn matched_payload_mut(&mut self) -> &mut HashSet; } -pub enum StackDirective { +pub(crate) enum StackDirective { Push, PushIfNotSelfClosing, PopImmediately, } #[derive(Default)] -pub struct ChildCounter { +pub(crate) struct ChildCounter { cumulative: i32, } @@ -100,7 +100,7 @@ impl CounterList { #[derive(Default)] /// A more efficient counter that only requires one owned local name to track counters across multiple stack frames -pub struct TypedChildCounterMap(HashMap, CounterList>); +pub(crate) struct TypedChildCounterMap(HashMap, CounterList>); impl TypedChildCounterMap { fn hash_name(&self, name: &LocalName<'_>) -> u64 { @@ -166,7 +166,7 @@ impl TypedChildCounterMap { } } -pub struct StackItem<'i, E: ElementData> { +pub(crate) struct StackItem<'i, E: ElementData> { pub local_name: LocalName<'i>, pub element_data: E, pub jumps: Vec, @@ -205,7 +205,7 @@ impl<'i, E: ElementData> StackItem<'i, E> { } } -pub struct Stack { +pub(crate) struct Stack { /// A counter for root elements root_child_counter: ChildCounter, /// A typed counter for all elements on all frames. This is optional to indicate if types are actually being counted. diff --git a/src/transform_stream/dispatcher.rs b/src/transform_stream/dispatcher.rs index 571ec1db..a337b496 100644 --- a/src/transform_stream/dispatcher.rs +++ b/src/transform_stream/dispatcher.rs @@ -11,7 +11,7 @@ use crate::rewriter::RewritingError; use TagTokenOutline::{EndTag, StartTag}; -pub struct AuxStartTagInfo<'i> { +pub(crate) struct AuxStartTagInfo<'i> { pub input: &'i Bytes<'i>, pub attr_buffer: &'i AttributeBuffer, pub self_closing: bool, @@ -21,13 +21,17 @@ type AuxStartTagInfoRequest = Box< dyn FnOnce(&mut C, AuxStartTagInfo<'_>) -> Result + Send, >; +// Pub only for integration tests +#[allow(private_interfaces)] pub enum DispatcherError { InfoRequest(AuxStartTagInfoRequest), RewritingError(RewritingError), } +// Pub only for integration tests pub type StartTagHandlingResult = Result>; +// Pub only for integration tests pub trait TransformController: Sized { fn initial_capture_flags(&self) -> TokenCaptureFlags; fn handle_start_tag( @@ -62,6 +66,7 @@ impl OutputSink for F { } } +// Pub only for integration tests pub struct Dispatcher where C: TransformController, diff --git a/src/transform_stream/mod.rs b/src/transform_stream/mod.rs index 1ba68309..84f4fbe3 100644 --- a/src/transform_stream/mod.rs +++ b/src/transform_stream/mod.rs @@ -1,14 +1,15 @@ mod dispatcher; use self::dispatcher::Dispatcher; -pub use self::dispatcher::{ - AuxStartTagInfo, DispatcherError, OutputSink, StartTagHandlingResult, TransformController, -}; +pub use self::dispatcher::OutputSink; +pub(crate) use self::dispatcher::{AuxStartTagInfo, DispatcherError}; +pub use self::dispatcher::{StartTagHandlingResult, TransformController}; use crate::base::SharedEncoding; use crate::memory::{Arena, SharedMemoryLimiter}; use crate::parser::{Parser, ParserDirective}; use crate::rewriter::RewritingError; +// Pub only for integration tests pub struct TransformStreamSettings where C: TransformController, @@ -22,6 +23,7 @@ where pub strict: bool, } +// Pub only for integration tests pub struct TransformStream where C: TransformController, @@ -134,6 +136,7 @@ where } #[cfg(feature = "integration_test")] + #[allow(private_interfaces)] pub fn parser(&mut self) -> &mut Parser> { &mut self.parser }