pter 0.1.0

Plain Text Email Renderer — convert HTML email bodies into readable markdown
Documentation
use criterion::{Criterion, black_box, criterion_group, criterion_main};

fn simple_email() -> &'static str {
    r#"<html><body>
    <h1>Meeting Tomorrow</h1>
    <p>Hi Max,</p>
    <p>Just confirming our meeting tomorrow at <strong>2pm</strong>.
    Please review the <a href="https://example.com/doc">document</a> beforehand.</p>
    <p>Best,<br>Alice</p>
    </body></html>"#
}

fn newsletter_email() -> &'static str {
    r#"<html><body>
    <table width="100%" cellpadding="0" cellspacing="0" role="presentation">
    <tr><td align="center">
    <table width="600" cellpadding="0" cellspacing="0">
    <tr><td>
        <h2>Weekly Digest</h2>
        <p>Here are your updates:</p>
        <ul>
            <li>New feature: <strong>Dark mode</strong> is now available</li>
            <li>Bug fix: Resolved <a href="https://example.com/issue/123">issue #123</a></li>
            <li>Update: API v2 documentation published</li>
        </ul>
        <p>Thanks for reading!</p>
        <hr>
        <p><small>Unsubscribe: <a href="https://example.com/unsub">click here</a></small></p>
    </td></tr>
    </table>
    </td></tr>
    </table>
    <img src="https://track.example.com/open.gif" width="1" height="1">
    </body></html>"#
}

fn reply_chain() -> &'static str {
    r#"<html><body>
    <div dir="ltr"><p>Got it, thanks!</p></div>
    <div class="gmail_quote">
        <div class="gmail_attr">On Tue, Jan 6, Bob wrote:</div>
        <blockquote class="gmail_quote">
            <div dir="ltr"><p>Here's the update you requested.</p></div>
            <div class="gmail_quote">
                <div class="gmail_attr">On Mon, Jan 5, Alice wrote:</div>
                <blockquote class="gmail_quote">
                    <div dir="ltr"><p>What's the status on the deployment?</p></div>
                </blockquote>
            </div>
        </blockquote>
    </div>
    </body></html>"#
}

fn large_email() -> String {
    let paragraph = "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. \
        Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. \
        Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris.</p>";
    let mut html = String::from("<html><body>");
    for i in 0..100 {
        html.push_str(&format!("<h3>Section {}</h3>", i));
        html.push_str(paragraph);
    }
    html.push_str("</body></html>");
    html
}

fn bench_simple(c: &mut Criterion) {
    let html = simple_email();
    c.bench_function("simple_email", |b| {
        b.iter(|| pter::convert(black_box(html)))
    });
}

fn bench_newsletter(c: &mut Criterion) {
    let html = newsletter_email();
    c.bench_function("newsletter_layout_tables", |b| {
        b.iter(|| pter::convert(black_box(html)))
    });
}

fn bench_reply_chain(c: &mut Criterion) {
    let html = reply_chain();
    c.bench_function("reply_chain_nested", |b| {
        b.iter(|| pter::convert(black_box(html)))
    });
}

fn bench_large(c: &mut Criterion) {
    let html = large_email();
    c.bench_function("large_100_sections", |b| {
        b.iter(|| pter::convert(black_box(&html)))
    });
}

criterion_group!(benches, bench_simple, bench_newsletter, bench_reply_chain, bench_large);
criterion_main!(benches);