Crate statbook

Source
Expand description

§Statbook

A high-performance Rust library for accessing sports statistics and news data with concurrent API calls, comprehensive error handling, and flexible configuration options.

Currently supports NFL player data via MySportsFeeds.com and news data via NewsAPI.org with plans to expand to other sports and data sources.

§Features

  • Concurrent API calls for improved performance
  • Comprehensive error handling with detailed error types
  • Flexible configuration with builder pattern and environment variables
  • Built-in testing utilities with mock providers
  • Multiple fetch strategies (stats-only, news-only, or both)
  • Extensible architecture with trait-based providers

§How-to

§Set up API credentials:

export STATS_API_KEY="your-mysportsfeeds-api-key"
export NEWS_API_KEY="your-newsapi-key"

§Flexible API Functions

use statbook::{
    StatbookClient, NewsQuery, Season,
    api::players::{get_player_stats, get_player_news, get_player_summary}
};

let client = StatbookClient::from_env()?;

// Get only player statistics (faster)
let stats = get_player_stats(&client, "josh-allen", None, &Season::Regular).await?;

// Get only news articles
let query = NewsQuery::for_player("josh-allen").with_page_size(10);
let news = get_player_news(&client, &query).await?;

// Get essential player info with news (always fetches both)
let summary = get_player_summary(&client, "josh-allen", None, &Season::Regular).await?;

§Custom Configuration

use statbook::{StatbookClient, StatbookConfig, NewsConfig, SortBy};

// Custom news configuration
let news_config = NewsConfig::new()
    .with_max_articles(15)
    .with_days_back(30)
    .with_sort_by(SortBy::Relevancy);

// Build configuration
let config = StatbookConfig::builder()
    .stats_api_key("your-stats-key")
    .news_api_key("your-news-key")
    .news_config(news_config)
    .build()?;

let client = StatbookClient::new(config);

§Error Handling

use statbook::{StatbookClient, StatbookError, Season, api::players::get_player_stats};

let client = StatbookClient::from_env()?;

match get_player_stats(&client, "unknown-player", None, &Season::Regular).await {
    Ok(stats) => println!("Found: {} {}", stats.first_name, stats.last_name),
    Err(StatbookError::PlayerNotFound { name }) => {
        println!("No player named '{}'", name);
    }
    Err(StatbookError::Network(e)) => {
        println!("Network error: {}", e);
    }
    Err(e) => println!("Other error: {}", e),
}

§Testing

The library provides built-in testing utilities:

use statbook::{create_mock_client, Season, api::players::get_player_stats};

#[tokio::test]
async fn test_my_app() {
    let client = create_mock_client();  // No real API calls
    let stats = get_player_stats(&client, "josh-allen", None, &Season::Regular).await.unwrap();
    assert_eq!(stats.first_name, "Josh");
}

Re-exports§

pub use client::StatbookClient;
pub use config::NewsConfig;
pub use config::SortBy;
pub use config::StatbookConfig;
pub use error::Result;
pub use error::StatbookError;

Modules§

api
client
config
error

Structs§

Article
A news article about a player or team.
MockNewsProvider
MockStatsProvider
NewsQuery
Query parameters for fetching news articles.
PlayerNews
Collection of news articles for a player.
PlayerStats
Player statistics without news articles.
PlayerSummary
Quick summary of essential player information with news.

Enums§

Season
Season Selector for fetching player data.

Traits§

NewsProvider
Trait for providing news articles from various data sources.
StatsProvider
Trait for providing player statistics from various data sources.

Functions§

create_custom_mock_client
Create a test client with custom mock providers
create_mock_client
Create a test client with mock providers
init_integration_test_client
Initialize a test client with real API credentials if available
skip_if_no_credentials
Skip test if no credentials are available