webpage_quality_analyzer 1.0.2

High-performance webpage quality analyzer with 115 comprehensive metrics - Rust library with WASM, C++, and Python bindings
Documentation
//! Level 1 API - Simple Usage Examples
//!
//! This example demonstrates the simplest way to use the webpage quality analyzer.
//! Perfect for quick analysis and prototyping.

use webpage_quality_analyzer::test_fixtures::SAMPLE_ARTICLE_HTML;
use webpage_quality_analyzer::{analyze, analyze_with_profile};

#[path = "common/macros.rs"]
mod macros;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    print_section!("🚀", "Level 1 API - Simple Usage Examples");

    // Example 1: Basic URL analysis (fetches HTML automatically)
    print_section!("📖", "Example 1: Basic URL Analysis");

    let url = "https://httpbin.org/html";
    match analyze(url, None).await {
        Ok(report) => {
            println!("✅ Analysis completed successfully!");
            println!("   URL: {}", report.url);
            print_score!(report.score, &report.verdict);
            println!(
                "   Word Count: {}",
                report.metrics.html_analysis.content.word_count
            );
            println!(
                "   Headings: {}",
                report.metrics.html_analysis.structure.headings_count
            );

            if !report.notes.is_empty() {
                println!("   Notes:");
                for note in &report.notes[..3.min(report.notes.len())] {
                    println!("{}", note);
                }
            }
        }
        Err(e) => {
            print_error!("Analysis failed", &e.to_string());
        }
    }

    println!();

    // Example 2: Analyze provided HTML content
    print_section!("📝", "Example 2: Analyze Provided HTML");

    let html_content = SAMPLE_ARTICLE_HTML;

    match analyze("https://example.com/article", Some(html_content)).await {
        Ok(report) => {
            println!("✅ HTML analysis completed!");
            println!("   Score: {:.1}/100", report.score);
            println!("   Verdict: {}", report.verdict);
            println!("   Content Quality:");
            println!(
                "     Word Count: {}",
                report.metrics.html_analysis.content.word_count
            );
            println!(
                "     Reading Time: {:.1} minutes",
                report.metrics.html_analysis.content.reading_time_minutes
            );
            println!(
                "     Main Text Ratio: {:.1}%",
                report.metrics.html_analysis.content.main_text_ratio * 100.0
            );
            println!("   SEO Quality:");
            println!(
                "     Title Length: {}",
                report.metrics.html_analysis.seo.title_len
            );
            println!(
                "     Has Description: {}",
                report.metadata.meta_description.is_some()
            );
            println!(
                "     Headings Count: {}",
                report.metrics.html_analysis.structure.headings_count
            );
        }
        Err(e) => {
            println!("❌ HTML analysis failed: {}", e);
        }
    }

    println!();

    // Example 3: Profile-specific analysis
    print_section!("🎯", "Example 3: Profile-Specific Analysis");

    let profiles = ["content_article", "news", "blog", "product"];

    for profile in &profiles {
        println!("Testing profile: {}", profile);

        match analyze_with_profile("https://example.com/test", Some(html_content), profile).await {
            Ok(report) => {
                println!(
                    "{} profile - Score: {:.1}/100 ({})",
                    profile, report.score, report.verdict
                );
            }
            Err(e) => {
                println!("{} profile failed: {}", profile, e);
            }
        }
    }

    println!();

    // Example 4: Error handling demonstration
    print_section!("⚠️", "Example 4: Error Handling");

    // Test with invalid URL
    match analyze("", None).await {
        Ok(_) => println!("  Unexpected success with empty URL"),
        Err(e) => println!("  ✅ Correctly handled empty URL: {}", e),
    }

    // Test with empty HTML
    match analyze("https://example.com", Some("")).await {
        Ok(report) => println!("  ✅ Handled empty HTML - Score: {:.1}", report.score),
        Err(e) => println!("  ⚠️  Empty HTML error: {}", e),
    }

    // Test with invalid profile
    match analyze_with_profile("https://example.com", Some(html_content), "invalid_profile").await {
        Ok(_) => println!("  Unexpected success with invalid profile"),
        Err(e) => println!("  ✅ Correctly handled invalid profile: {}", e),
    }

    print_completion!("Level 1 API examples completed!");

    print_takeaways!(
        "Use analyze() for quick, one-line analysis",
        "Use analyze_with_profile() for content-specific scoring",
        "Both functions handle URL fetching automatically",
        "Provide HTML content to skip network requests",
        "Always handle errors appropriately"
    );

    Ok(())
}