Skip to content

Commit

Permalink
Merge pull request #149 from sftse/api-surface
Browse files Browse the repository at this point in the history
Trim api visibility
  • Loading branch information
jugglerchris authored May 19, 2024
2 parents ee6aa93 + 8cabef8 commit 59516b7
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 70 deletions.
62 changes: 31 additions & 31 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ const MIN_WIDTH: usize = 3;

/// Size information/estimate
#[derive(Debug, Copy, Clone, Default)]
pub struct SizeEstimate {
struct SizeEstimate {
size: usize, // Rough overall size
min_width: usize, // The narrowest possible

Expand All @@ -184,7 +184,7 @@ pub struct SizeEstimate {
impl SizeEstimate {
/// Combine two estimates into one (add size and take the largest
/// min width)
pub fn add(self, other: SizeEstimate) -> SizeEstimate {
fn add(self, other: SizeEstimate) -> SizeEstimate {
let min_width = max(self.min_width, other.min_width);
SizeEstimate {
size: self.size + other.size,
Expand All @@ -194,7 +194,7 @@ impl SizeEstimate {
}
/// Combine two estimates into one which need to be side by side.
/// The min widths are added.
pub fn add_hor(self, other: SizeEstimate) -> SizeEstimate {
fn add_hor(self, other: SizeEstimate) -> SizeEstimate {
SizeEstimate {
size: self.size + other.size,
min_width: self.min_width + other.min_width,
Expand All @@ -203,7 +203,7 @@ impl SizeEstimate {
}

/// Combine two estimates into one (take max of each)
pub fn max(self, other: SizeEstimate) -> SizeEstimate {
fn max(self, other: SizeEstimate) -> SizeEstimate {
SizeEstimate {
size: max(self.size, other.size),
min_width: max(self.min_width, other.min_width),
Expand All @@ -214,7 +214,7 @@ impl SizeEstimate {

#[derive(Clone, Debug)]
/// Render tree table cell
pub struct RenderTableCell {
struct RenderTableCell {
colspan: usize,
content: Vec<RenderNode>,
size_estimate: Cell<Option<SizeEstimate>>,
Expand All @@ -223,7 +223,7 @@ pub struct RenderTableCell {

impl RenderTableCell {
/// Render this cell to a renderer.
pub fn render<T: Write, D: TextDecorator>(
fn render<T: Write, D: TextDecorator>(
&mut self,
_renderer: &mut TextRenderer<D>,
_err_out: &mut T,
Expand All @@ -233,7 +233,7 @@ impl RenderTableCell {
}

/// Calculate or return the estimate size of the cell
pub fn get_size_estimate(&self) -> SizeEstimate {
fn get_size_estimate(&self) -> SizeEstimate {
if self.size_estimate.get().is_none() {
let size = self
.content
Expand All @@ -248,28 +248,28 @@ impl RenderTableCell {

#[derive(Clone, Debug)]
/// Render tree table row
pub struct RenderTableRow {
struct RenderTableRow {
cells: Vec<RenderTableCell>,
col_sizes: Option<Vec<usize>>,
}

impl RenderTableRow {
/// Return a mutable iterator over the cells.
pub fn cells(&self) -> std::slice::Iter<RenderTableCell> {
fn cells(&self) -> std::slice::Iter<RenderTableCell> {
self.cells.iter()
}
/// Return a mutable iterator over the cells.
pub fn cells_mut(&mut self) -> std::slice::IterMut<RenderTableCell> {
fn cells_mut(&mut self) -> std::slice::IterMut<RenderTableCell> {
self.cells.iter_mut()
}
/// Count the number of cells in the row.
/// Takes into account colspan.
pub fn num_cells(&self) -> usize {
fn num_cells(&self) -> usize {
self.cells.iter().map(|cell| cell.colspan.max(1)).sum()
}
/// Return an iterator over (column, &cell)s, which
/// takes into account colspan.
pub fn cell_columns(&mut self) -> Vec<(usize, &mut RenderTableCell)> {
fn cell_columns(&mut self) -> Vec<(usize, &mut RenderTableCell)> {
let mut result = Vec::new();
let mut colno = 0;
for cell in &mut self.cells {
Expand All @@ -282,7 +282,7 @@ impl RenderTableRow {

/// Return the contained cells as RenderNodes, annotated with their
/// widths if available. Skips cells with no width allocated.
pub fn into_cells(self, vertical: bool) -> Vec<RenderNode> {
fn into_cells(self, vertical: bool) -> Vec<RenderNode> {
let mut result = Vec::new();
let mut colno = 0;
let col_sizes = self.col_sizes.unwrap();
Expand All @@ -306,15 +306,15 @@ impl RenderTableRow {

#[derive(Clone, Debug)]
/// A representation of a table render tree with metadata.
pub struct RenderTable {
struct RenderTable {
rows: Vec<RenderTableRow>,
num_columns: usize,
size_estimate: Cell<Option<SizeEstimate>>,
}

impl RenderTable {
/// Create a new RenderTable with the given rows
pub fn new(rows: Vec<RenderTableRow>) -> RenderTable {
fn new(rows: Vec<RenderTableRow>) -> RenderTable {
let num_columns = rows.iter().map(|r| r.num_cells()).max().unwrap_or(0);
RenderTable {
rows,
Expand All @@ -324,17 +324,17 @@ impl RenderTable {
}

/// Return an iterator over the rows.
pub fn rows(&self) -> std::slice::Iter<RenderTableRow> {
fn rows(&self) -> std::slice::Iter<RenderTableRow> {
self.rows.iter()
}

/// Return an iterator over the rows.
pub fn rows_mut(&mut self) -> std::slice::IterMut<RenderTableRow> {
fn rows_mut(&mut self) -> std::slice::IterMut<RenderTableRow> {
self.rows.iter_mut()
}
/// Consume this and return a `Vec<RenderNode>` containing the children;
/// the children know the column sizes required.
pub fn into_rows(self, col_sizes: Vec<usize>, vert: bool) -> Vec<RenderNode> {
fn into_rows(self, col_sizes: Vec<usize>, vert: bool) -> Vec<RenderNode> {
self.rows
.into_iter()
.map(|mut tr| {
Expand Down Expand Up @@ -383,15 +383,15 @@ impl RenderTable {
}

/// Calculate and store (or return stored value) of estimated size
pub fn get_size_estimate(&self) -> SizeEstimate {
fn get_size_estimate(&self) -> SizeEstimate {
self.size_estimate.get().unwrap()
}
}

/// The node-specific information distilled from the DOM.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum RenderNodeInfo {
enum RenderNodeInfo {
/// Some text.
Text(String),
/// A group of nodes collected together.
Expand Down Expand Up @@ -454,22 +454,22 @@ pub enum RenderNodeInfo {

/// Common fields from a node.
#[derive(Clone, Debug)]
pub struct RenderNode {
struct RenderNode {
size_estimate: Cell<Option<SizeEstimate>>,
info: RenderNodeInfo,
}

impl RenderNode {
/// Create a node from the RenderNodeInfo.
pub fn new(info: RenderNodeInfo) -> RenderNode {
fn new(info: RenderNodeInfo) -> RenderNode {
RenderNode {
size_estimate: Cell::new(None),
info,
}
}

/// Get a size estimate
pub fn get_size_estimate(&self) -> SizeEstimate {
fn get_size_estimate(&self) -> SizeEstimate {
self.size_estimate.get().unwrap()
}

Expand Down Expand Up @@ -602,7 +602,7 @@ impl RenderNode {
/// Return true if this node is definitely empty. This is used to quickly
/// remove e.g. links with no anchor text in most cases, but can't recurse
/// and look more deeply.
pub fn is_shallow_empty(&self) -> bool {
fn is_shallow_empty(&self) -> bool {
use RenderNodeInfo::*;

// Otherwise, make an estimate.
Expand Down Expand Up @@ -1083,7 +1083,7 @@ fn dom_to_render_tree_with_context<T: Write>(
}

/// Convert a DOM tree or subtree into a render tree.
pub fn dom_to_render_tree<T: Write>(handle: Handle, err_out: &mut T) -> Result<Option<RenderNode>> {
fn dom_to_render_tree<T: Write>(handle: Handle, err_out: &mut T) -> Result<Option<RenderNode>> {
dom_to_render_tree_with_context(handle, err_out, &mut Default::default())
}

Expand Down Expand Up @@ -2284,39 +2284,39 @@ impl RenderTree {
}

/// Render this document using the given `decorator` and wrap it to `width` columns.
pub fn render<D: TextDecorator>(self, width: usize, decorator: D) -> Result<RenderedText<D>> {
fn render<D: TextDecorator>(self, width: usize, decorator: D) -> Result<RenderedText<D>> {
self.render_with_context(&mut Default::default(), width, decorator)
}

/// Render this document as plain text using the [`PlainDecorator`][] and wrap it to `width`
/// columns.
///
/// [`PlainDecorator`]: render/text_renderer/struct.PlainDecorator.html
pub fn render_plain(self, width: usize) -> Result<RenderedText<PlainDecorator>> {
fn render_plain(self, width: usize) -> Result<RenderedText<PlainDecorator>> {
self.render(width, PlainDecorator::new())
}

/// Render this document as rich text using the [`RichDecorator`][] and wrap it to `width`
/// columns.
///
/// [`RichDecorator`]: render/text_renderer/struct.RichDecorator.html
pub fn render_rich(self, width: usize) -> Result<RenderedText<RichDecorator>> {
fn render_rich(self, width: usize) -> Result<RenderedText<RichDecorator>> {
self.render(width, RichDecorator::new())
}
}

/// A rendered HTML document.
pub struct RenderedText<D: TextDecorator>(SubRenderer<D>);
struct RenderedText<D: TextDecorator>(SubRenderer<D>);

impl<D: TextDecorator> RenderedText<D> {
/// Convert the rendered HTML document to a string.
pub fn into_string(self) -> Result<String> {
fn into_string(self) -> Result<String> {
self.0.into_string()
}

/// Convert the rendered HTML document to a vector of lines with the annotations created by the
/// decorator.
pub fn into_lines(self) -> Result<Vec<TaggedLine<Vec<D::Annotation>>>> {
fn into_lines(self) -> Result<Vec<TaggedLine<Vec<D::Annotation>>>> {
Ok(self
.0
.into_lines()?
Expand Down
2 changes: 1 addition & 1 deletion src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::Error;
pub mod text_renderer;

/// A type which is a backend for HTML to text rendering.
pub trait Renderer {
pub(crate) trait Renderer {
/// Add an empty line to the output (ie between blocks).
fn add_empty_line(&mut self) -> crate::Result<()>;

Expand Down
Loading

0 comments on commit 59516b7

Please sign in to comment.