Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit visibility where possible #234

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/base/align.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub trait Align {
pub(crate) trait Align {
fn align(&mut self, offset: usize);
}

Expand Down
1 change: 1 addition & 0 deletions src/base/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AtomicUsize>,
Expand Down
6 changes: 3 additions & 3 deletions src/base/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion src/base/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
4 changes: 3 additions & 1 deletion src/html/local_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64>);

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
1 change: 1 addition & 0 deletions src/html/namespace.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Pub only for integration tests
#[derive(Default, Copy, Clone, Eq, PartialEq, Debug)]
pub enum Namespace {
#[default]
Expand Down
1 change: 1 addition & 0 deletions src/html/tag.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]

Expand Down
2 changes: 1 addition & 1 deletion src/memory/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>,
}
Expand Down
2 changes: 1 addition & 1 deletion src/memory/limited_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::vec::Drain;
use super::{MemoryLimitExceededError, SharedMemoryLimiter};

#[derive(Debug)]
pub struct LimitedVec<T> {
pub(crate) struct LimitedVec<T> {
limiter: SharedMemoryLimiter,
vec: Vec<T>,
}
Expand Down
1 change: 1 addition & 0 deletions src/memory/limiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AtomicUsize>,
Expand Down
4 changes: 2 additions & 2 deletions src/memory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
12 changes: 6 additions & 6 deletions src/parser/lexer/lexeme/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -15,7 +15,7 @@ pub type TagLexeme<'i> = Lexeme<'i, TagTokenOutline>;
pub type NonTagContentLexeme<'i> = Lexeme<'i, Option<NonTagContentTokenOutline>>;

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,
Expand All @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions src/parser/lexer/lexeme/token_outline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -19,7 +19,7 @@ impl Align for AttributeOutline {
}

#[derive(Debug)]
pub enum TagTokenOutline {
pub(crate) enum TagTokenOutline {
StartTag {
name: Range,
name_hash: LocalNameHash,
Expand All @@ -35,7 +35,7 @@ pub enum TagTokenOutline {
}

#[derive(Debug)]
pub enum NonTagContentTokenOutline {
pub(crate) enum NonTagContentTokenOutline {
Text(TextType),
Comment(Range),

Expand Down
10 changes: 5 additions & 5 deletions src/parser/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -13,19 +13,19 @@ 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<ParserDirective, RewritingError>;
fn handle_non_tag_content(
&mut self,
lexeme: &NonTagContentLexeme<'_>,
) -> Result<(), RewritingError>;
}

pub type State<S> = fn(&mut Lexer<S>, context: &mut ParserContext<S>, &[u8]) -> StateResult;
pub(crate) type State<S> = fn(&mut Lexer<S>, context: &mut ParserContext<S>, &[u8]) -> StateResult;

pub type AttributeBuffer = Vec<AttributeOutline>;
pub(crate) type AttributeBuffer = Vec<AttributeOutline>;

pub struct Lexer<S: LexemeSink> {
pub(crate) struct Lexer<S> {
next_pos: usize,
is_last_input: bool,
lexeme_start: usize,
Expand Down
16 changes: 10 additions & 6 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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<S> {
pub(crate) struct ParserContext<S> {
output_sink: S,
tree_builder_simulator: TreeBuilderSimulator,
}

pub trait ParserOutputSink: LexemeSink + TagHintSink {}
pub(crate) trait ParserOutputSink: LexemeSink + TagHintSink {}

pub struct Parser<S: ParserOutputSink> {
// Pub only for integration tests
pub struct Parser<S> {
lexer: Lexer<S>,
tag_scanner: TagScanner<S>,
current_directive: ParserDirective,
context: ParserContext<S>,
}

// public only for integration tests
#[allow(private_bounds, private_interfaces)]
impl<S: ParserOutputSink> Parser<S> {
#[inline]
#[must_use]
Expand Down Expand Up @@ -112,6 +115,7 @@ cfg_if! {
if #[cfg(feature = "integration_test")] {
use crate::html::{LocalNameHash, TextType};

#[allow(private_bounds)]
impl<S: ParserOutputSink> Parser<S> {
pub fn switch_text_type(&mut self, text_type: TextType) {
match self.current_directive {
Expand Down
12 changes: 6 additions & 6 deletions src/parser/state_machine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -48,7 +48,7 @@ pub struct StateMachineBookmark {
feedback_directive: FeedbackDirective,
}

pub enum ActionError {
pub(crate) enum ActionError {
RewritingError(RewritingError),
ParserDirectiveChangeRequired(ParserDirective, StateMachineBookmark),
}
Expand All @@ -72,7 +72,7 @@ pub type ActionResult = Result<(), ActionError>;
pub type StateResult = Result<(), ParsingTermination>;
pub type ParseResult = Result<Never, ParsingTermination>;

pub trait StateMachineActions {
pub(crate) trait StateMachineActions {
type Context;

fn emit_eof(&mut self, context: &mut Self::Context, input: &[u8]) -> ActionResult;
Expand Down Expand Up @@ -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!();
Expand Down
7 changes: 4 additions & 3 deletions src/parser/tag_scanner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<'_>,
Expand All @@ -21,7 +21,8 @@ pub trait TagHintSink {
) -> Result<ParserDirective, RewritingError>;
}

pub type State<S> = fn(&mut TagScanner<S>, context: &mut ParserContext<S>, &[u8]) -> StateResult;
pub(crate) type State<S> =
fn(&mut TagScanner<S>, context: &mut ParserContext<S>, &[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
Expand All @@ -34,7 +35,7 @@ pub type State<S> = fn(&mut TagScanner<S>, context: &mut ParserContext<S>, &[u8]
/// of the input (e.g. `<div` will produce a tag preview, but not tag token). However,
/// it's not a concern for our use case as no content will be erroneously captured
/// in this case.
pub struct TagScanner<S: TagHintSink> {
pub(crate) struct TagScanner<S> {
next_pos: usize,
is_last_input: bool,
tag_start: Option<usize>,
Expand Down
2 changes: 1 addition & 1 deletion src/parser/tree_builder_simulator/ambiguity_guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ enum State {
InOrAfterFrameset,
}

pub struct AmbiguityGuard {
pub(crate) struct AmbiguityGuard {
state: State,
}

Expand Down
6 changes: 3 additions & 3 deletions src/parser/tree_builder_simulator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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<Namespace>,
current_ns: Namespace,
ambiguity_guard: AmbiguityGuard,
Expand Down Expand Up @@ -173,7 +173,7 @@ impl TreeBuilderSimulator {
}

#[inline]
pub fn current_ns(&self) -> Namespace {
pub const fn current_ns(&self) -> Namespace {
self.current_ns
}

Expand Down
Loading
Loading