pub trait BrowserFactory: Send + Sync {
// Required method
fn create(&self) -> Result<Browser>;
}Expand description
Trait for browser factory pattern.
Abstracts browser creation to allow different implementations (Chrome, Firefox, mock browsers for testing, etc.)
§Thread Safety
This trait requires Send + Sync because factories are shared
across threads in the browser pool.
§Implementors
ChromeBrowserFactory- Creates Chrome/Chromium browsersmock::MockBrowserFactory- For testing (whentest-utilsfeature enabled)
§Example
ⓘ
use html2pdf_api::{BrowserFactory, ChromeBrowserFactory};
fn use_factory(factory: &dyn BrowserFactory) {
match factory.create() {
Ok(browser) => println!("Browser created!"),
Err(e) => eprintln!("Failed: {}", e),
}
}
let factory = ChromeBrowserFactory::with_defaults();
use_factory(&factory);Required Methods§
Sourcefn create(&self) -> Result<Browser>
fn create(&self) -> Result<Browser>
Create a new browser instance.
§Errors
Returns error if browser creation fails:
BrowserPoolError::Configuration- Invalid launch optionsBrowserPoolError::BrowserCreation- Binary not found, launch fails, etc.
§Example
ⓘ
use html2pdf_api::{BrowserFactory, ChromeBrowserFactory};
let factory = ChromeBrowserFactory::with_defaults();
let browser = factory.create()?;
// Use browser...