Skip to main content

BlockBuilder

Struct BlockBuilder 

Source
pub struct BlockBuilder { /* private fields */ }
Expand description

Block Builder

A builder for constructing content blocks of various types.

§Example

use lib_epub::{builder::content::BlockBuilder, types::{BlockType, Footnote}};

let mut builder = BlockBuilder::new(BlockType::Text);
builder.set_content("Hello, world!").add_footnote(Footnote {
    content: "This is a footnote.".to_string(),
    locate: 13,               
});

builder.build()?;

§Notes

  • Not all fields are required for all block types. Required fields vary by block type.
  • The build() method will validate that required fields are set for the specified block type.

Implementations§

Source§

impl BlockBuilder

Source

pub fn new(block_type: BlockType) -> Self

Creates a new BlockBuilder instance

Initializes a BlockBuilder with the specified block type.

§Parameters
  • block_type: The type of block to construct
Source

pub fn set_content(&mut self, content: &str) -> &mut Self

Sets the text content of the block

Used for Text, Quote, and Title block types.

§Parameters
  • content: The text content to set
Source

pub fn set_title_level(&mut self, level: usize) -> &mut Self

Sets the heading level for a Title block

Only applicable to Title block types. Valid range is 1 to 6. If the level is outside the valid range, this method silently ignores the setting and returns self unchanged.

§Parameters
  • level: The heading level (1-6), corresponding to h1-h6 HTML tags
Source

pub fn set_url(&mut self, url: &PathBuf) -> Result<&mut Self, EpubError>

Sets the media file path

Used for Image, Audio, and Video block types. This method validates that the file is a recognized image, audio, or video type.

§Parameters
  • url: The path to the media file
§Return
  • Ok(&mut self): If the file type is valid
  • Err(EpubError): The file does not exist or the file format is not image, audio, or video
Source

pub fn set_alt(&mut self, alt: &str) -> &mut Self

Sets the alternative text for an image

Only applicable to Image block types. Alternative text is displayed when the image cannot be loaded.

§Parameters
  • alt: The alternative text for the image
Source

pub fn set_caption(&mut self, caption: &str) -> &mut Self

Sets the caption for the block

Used for Image, Audio, Video, and MathML block types. The caption is displayed below the media or element.

§Parameters
  • caption: The caption text to display
Source

pub fn set_fallback(&mut self, fallback: &str) -> &mut Self

Sets the fallback text for audio or video content

Used for Audio and Video block types. The fallback text is displayed when the media file cannot be played.

§Parameters
  • fallback: The fallback text content
Source

pub fn set_mathml_element(&mut self, element_str: &str) -> &mut Self

Sets the raw MathML element string

Only applicable to MathML block types. This method accepts the raw MathML markup data without validation. The user is responsible for ensuring the MathML is well-formed.

§Parameters
  • element_str: The raw MathML markup string
Source

pub fn set_fallback_image( &mut self, fallback_image: PathBuf, ) -> Result<&mut Self, EpubError>

Sets the fallback image for MathML content

Only applicable to MathML block types. The fallback image is displayed when the MathML markup cannot be rendered. This method validates that the file is a recognized image type.

§Parameters
  • fallback_image: The path to the fallback image file
§Return
  • Ok(self): If the file type is valid
  • Err(EpubError): If validation fails
Source

pub fn add_footnote(&mut self, footnote: Footnote) -> &mut Self

Adds a footnote to the block

Adds a single footnote to the block’s footnotes collection. The footnote must reference a valid position within the content.

§Parameters
  • footnote: The footnote to add
Source

pub fn set_footnotes(&mut self, footnotes: Vec<Footnote>) -> &mut Self

Sets all footnotes for the block

Replaces the current footnotes collection with the provided one. All footnotes must reference valid positions within the content.

§Parameters
  • footnotes: The vector of footnotes to set
Source

pub fn remove_last_footnote(&mut self) -> &mut Self

Removes the last footnote

Removes and discards the last footnote from the footnotes collection. If the collection is empty, this method has no effect.

Source

pub fn clear_footnotes(&mut self) -> &mut Self

Clears all footnotes

Removes all footnotes from the block’s footnotes collection.

Source

pub fn build(self) -> Result<Block, EpubError>

Builds the block

Constructs a Block instance based on the configured parameters and block type. This method validates that all required fields are set for the specified block type and validates the footnotes to ensure they reference valid content positions.

§Return
  • Ok(Block): Build successful
  • Err(EpubError): Error occurred during the build process

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.