This documentation is part of the "Projects with Books" initiative at zenOSmosis.
The source code for this project is available on GitHub.
Running Examples
Relevant source files
This page demonstrates how to run the example programs included in the repository. Each example illustrates specific functionality of the sec-fetcher system, from basic configuration validation to fetching company data from the SEC EDGAR API. These examples serve as practical starting points for understanding how to integrate the library into your own applications.
For detailed information about configuring the application before running these examples, see Configuration System. For understanding the underlying network layer and data fetching mechanisms, see Network Layer & SecClient and Data Fetching Functions.
Example Programs Overview
The repository includes four example programs located in the examples/ directory. Each demonstrates different aspects of the system:
| Example Program | Primary Purpose | Key Functions Demonstrated | Command-Line Args |
|---|---|---|---|
config.rs | Configuration validation and inspection | ConfigManager::load(), AppConfig::pretty_print() | None |
funds.rs | Fetch investment company datasets | fetch_investment_company_series_and_class_dataset() | None |
lookup_cik.rs | CIK lookup and submission retrieval | fetch_cik_by_ticker_symbol(), fetch_cik_submissions() | Ticker symbol (e.g., AAPL) |
nport_filing.rs | Parse NPORT filing holdings | fetch_nport_filing(), XML parsing | Accession number |
us_gaap_human_readable.rs | Fetch and display US GAAP fundamentals | fetch_us_gaap_fundamentals(), distill_us_gaap_fundamental_concepts() | Ticker symbol |
Sources: examples/config.rs:1-18 examples/funds.rs:1-19 examples/lookup_cik.rs:1-75
Example Program Architecture
Diagram: Example Programs and System Integration
Sources: examples/config.rs:1-18 examples/funds.rs:1-19 examples/lookup_cik.rs:1-75
config.rs - Configuration Validation
Purpose : Load and display the current application configuration to verify that configuration files are properly formatted and credentials are correctly set.
How to Run :
Expected Output : The example displays the merged configuration in JSON format, showing values from the configuration file merged with defaults.
Code Flow :
graph TB
Start["main()
entry point\nexamples/config.rs:4"]
Load["ConfigManager::load()\nLoad config from file\nexamples/config.rs:13"]
GetConfig["config_manager.get_config()\nRetrieve AppConfig reference\nexamples/config.rs:15"]
Pretty["config.pretty_print()\nSerialize to JSON\nexamples/config.rs:16"]
Print["print! macro\nDisplay formatted config\nexamples/config.rs:16"]
End["Program exit"]
Start --> Load
Load --> GetConfig
GetConfig --> Pretty
Pretty --> Print
Print --> End
Key Functions Used :
| Function | File Location | Purpose |
|---|---|---|
ConfigManager::load() | examples/config.rs13 | Loads configuration from default paths |
get_config() | examples/config.rs15 | Returns reference to AppConfig struct |
pretty_print() | examples/config.rs16 | Serializes configuration to formatted JSON |
What This Demonstrates :
- Basic configuration loading workflow
- Configuration file resolution and merging
- Validation of configuration structure
Sources: examples/config.rs:1-18 src/config/app_config.rs:1-159
funds.rs - Investment Company Dataset
Purpose : Fetch the complete investment company series and class dataset from the SEC EDGAR API, demonstrating network client initialization and basic data fetching.
How to Run :
Expected Output : A list of investment company records, each containing series information, fund names, CIKs, and classification details. The output format is the Debug representation of the Ticker structs.
Code Flow :
graph TB
Start["#[tokio::main] async fn main()\nexamples/funds.rs:6-7"]
LoadCfg["ConfigManager::load()\nexamples/funds.rs:8"]
CreateClient["SecClient::from_config_manager()\nInitialize HTTP client\nexamples/funds.rs:9"]
FetchData["fetch_investment_company_series_and_class_dataset()\nNetwork request to SEC API\nexamples/funds.rs:11"]
LoopStart["for fund in funds\nIterate results\nexamples/funds.rs:13"]
PrintFund["print! macro\nDisplay fund Debug output\nexamples/funds.rs:14"]
End["Return Ok(())\nexamples/funds.rs:17"]
Start --> LoadCfg
LoadCfg --> CreateClient
CreateClient --> FetchData
FetchData --> LoopStart
LoopStart --> PrintFund
PrintFund -->|More funds| LoopStart
LoopStart -->|Done| End
Key Functions and Structures :
| Entity | Type | Purpose |
|---|---|---|
fetch_investment_company_series_and_class_dataset() | Function | Fetches investment company data from SEC |
SecClient | Struct | HTTP client with throttling and caching |
Ticker | Struct | Represents company ticker information |
tokio::main | Macro | Enables async runtime |
What This Demonstrates :
- Async/await pattern usage with
tokio SecClientinitialization from configuration- Network function invocation
- Iteration over fetched data structures
Sources: examples/funds.rs:1-19
lookup_cik.rs - CIK Lookup and Submission Retrieval
Purpose : Demonstrate CIK (Central Index Key) lookup by ticker symbol and retrieval of company submissions, with special handling for NPORT-P filings.
How to Run :
Replace AAPL with any valid ticker symbol.
Expected Output :
- The CIK number for the ticker
- URL to the SEC submissions JSON
- Most recent NPORT-P submission (if the company files NPORT-P forms)
- Or a list of other submission types (e.g., 10-K)
- EDGAR archive URLs for each submission
Execution Flow with Code Entities :
Command-Line Argument Handling :
The example requires exactly one argument (the ticker symbol). The argument parsing logic is implemented at examples/lookup_cik.rs:10-14:
args.len() check → if not 2, print usage and exit
ticker_symbol = args[1]
Key Code Entities :
| Entity | Type | Location | Purpose |
|---|---|---|---|
fetch_cik_by_ticker_symbol() | Function | examples/lookup_cik.rs:21-23 | Retrieves CIK for ticker |
fetch_cik_submissions() | Function | examples/lookup_cik.rs43 | Fetches all submissions for CIK |
CikSubmission::most_recent_nport_p_submission() | Method | examples/lookup_cik.rs:45-46 | Filters for latest NPORT-P |
as_edgar_archive_url() | Method | examples/lookup_cik.rs54 | Generates SEC EDGAR URL |
Cik | Struct | examples/lookup_cik.rs28 | 10-digit CIK identifier |
CikSubmission | Struct | examples/lookup_cik.rs43 | Represents a single SEC filing |
What This Demonstrates :
- Command-line argument parsing using
std::env::args() - Error handling with
ResultandOptiontypes - Conditional logic based on filing types
- Method chaining for data transformation
- URL generation from structured data
Sources: examples/lookup_cik.rs:1-75
Prerequisites and Common Patterns
Configuration Requirement : All examples (except config.rs) require a valid configuration file with at least an email address set. See Configuration System for setup instructions.
Common Code Pattern Across Examples :
All examples follow this pattern:
- Load configuration using
ConfigManager::load() - Initialize
SecClientfrom the configuration - Call network functions to fetch data
- Process and display results
Error Handling : Examples use the ? operator extensively to propagate errors and return Result<(), Box<dyn Error>> from main(). This allows errors to bubble up with descriptive messages.
Async Runtime : Examples that perform network operations use #[tokio::main] to enable the async runtime, allowing await syntax for asynchronous operations.
Sources: examples/config.rs:1-18 examples/funds.rs:1-19 examples/lookup_cik.rs:1-75
Running Examples in Development Mode
When running examples, the application may detect development mode based on environment variables or the presence of certain files. Development mode affects:
- Cache behavior : May use more aggressive caching strategies
- Logging verbosity : May produce more detailed output
- Rate limiting : May use relaxed throttling policies for testing
Refer to Utility Functions for details on development mode detection.
Next Steps
After running these examples:
- Explore the Network Layer & SecClient to understand HTTP client configuration
- Learn about Data Fetching Functions for all available network operations
- Review Data Models & Enumerations to understand returned data structures
- See Main Application Flow for production usage patterns
Sources: examples/config.rs:1-18 examples/funds.rs:1-19 examples/lookup_cik.rs:1-75