This documentation is part of the "Projects with Books" initiative at zenOSmosis.
The source code for this project is available on GitHub.
Utility Functions & Accessors
Loading…
Utility Functions & Accessors
Relevant source files
- src/network/fetch_cik_submissions.rs
- src/utils.rs
- src/utils/is_interactive_mode.rs
- tests/sec_client_tests.rs
Purpose and Scope
This document covers the utility functions and helper modules provided by the utils module in the Rust sec-fetcher application. These utilities provide cross-cutting functionality used throughout the codebase, including data structure transformations, runtime mode detection, and collection extensions. Additionally, it documents the internal data extraction patterns used by the network layer to process complex SEC JSON responses.
Sources: src/utils.rs:1-9
Module Overview
The utils module is organized as a collection of focused sub-modules. The module uses Rust’s re-export pattern to provide a clean public API for environment detection and collection manipulation.
| Sub-module | Primary Export | Purpose |
|---|---|---|
is_development_mode | is_development_mode() | Runtime environment detection for configuration and logging. |
is_interactive_mode | is_interactive_mode(), set_interactive_mode_override() | Detects if the app is running in a TTY or should simulate one. |
vec_extensions | VecExtensions trait | Extension methods for Vec<T> used in data processing. |
Sources: src/utils.rs:1-9
Interactive Mode Management
The interactive mode utilities manage application state related to user interaction, controlling whether the application should prompt for user input or run in automated mode (e.g., CI/CD or piped output).
Implementation Details
The is_interactive_mode function checks the environment and standard streams to determine the session type:
- Override Check : It first looks for the
INTERACTIVE_MODE_OVERRIDEenvironment variable. Values"1"/"true"force interactive mode;"0"/"false"force non-interactive src/utils/is_interactive_mode.rs:8-28 - Terminal Detection : If no override exists, it uses
std::io::IsTerminalto check if bothstdinandstdoutare connected to a terminal src/utils/is_interactive_mode.rs:30-32
Function Signatures
src/utils/is_interactive_mode.rs21 src/utils/is_interactive_mode.rs62
Usage Scenarios
| Scenario | is_interactive_mode() is true | is_interactive_mode() is false |
|---|---|---|
| Missing Config | Prompt user for email/API keys | Exit with error message |
| Progress | Render dynamic progress bars | Log static status updates |
| Piping | Warning if output is not redirected | Clean data output for grep/jq |
Sources: src/utils/is_interactive_mode.rs:1-76
Data Accessors and Parsing Patterns
While not a standalone “accessor” module, the codebase implements standardized internal functions to extract data from SEC JSON structures (DataFrames-like objects in Python, but raw serde_json::Value in Rust).
Submission Parsing Logic
The fetch_cik_submissions module contains specialized logic to flatten the SEC’s columnar JSON format into a vector of strongly-typed models.
graph TD
subgraph "SEC JSON Structure"
JSON["Root JSON Object"]
Filings["'filings' Object"]
Recent["'recent' Block (Columnar)"]
Files["'files' Array (Pagination)"]
end
subgraph "Logic: extract_filings_from_block"
Zip["itertools::izip!"]
Acc["accessionNumber[]"]
Form["form[]"]
Doc["primaryDocument[]"]
Date["filingDate[]"]
end
subgraph "Output Space"
Model["Vec<CikSubmission>"]
end
JSON --> Filings
Filings --> Recent
Filings --> Files
Recent --> Acc & Form & Doc & Date
Acc & Form & Doc &
Date --> Zip
Zip --> Model
Files -.->|Recursively Fetch| JSON
Key Accessor Functions
extract_filings_from_block: Zips multiple arrays (accession numbers, forms, dates) from aserde_json::Valueinto aVec<CikSubmission>src/network/fetch_cik_submissions.rs:10-63parse_cik_submissions_json: The public entry point for converting a raw SEC JSON response into models, specifically targeting thefilings.recentblock src/network/fetch_cik_submissions.rs:75-87
Sources: src/network/fetch_cik_submissions.rs:10-177
Utility Function Relationships
This diagram maps the relationship between utility functions and the higher-level system components that consume them.
Sources: src/utils.rs:1-9 src/network/fetch_cik_submissions.rs:1-178
graph LR
subgraph "Utility Layer (src/utils/)"
IsDev["is_development_mode()"]
IsInt["is_interactive_mode()"]
VecExt["VecExtensions"]
end
subgraph "Network Layer (src/network/)"
Client["SecClient"]
SubParser["parse_cik_submissions_json()"]
end
subgraph "App Logic"
Config["ConfigManager"]
Main["main.rs / CLI"]
end
IsDev --> Config
IsInt --> Main
VecExt --> SubParser
SubParser --> Client
Development Mode Detection
The is_development_mode utility is a simple toggle used to gate features that should not be active in production, such as test fixture generation or bypasses for rate limiting in local mock environments.
Usage in Configuration
The ConfigManager and SecClient utilize these flags to determine if they should load production SEC endpoints or local mock servers during integration testing tests/sec_client_tests.rs:7-23
Sources: src/utils.rs:1-2 tests/sec_client_tests.rs:1-23
Vector Extensions Trait
The VecExtensions trait provides ergonomic helpers for common operations performed on lists of SEC data, such as deduplication or specialized filtering before rendering.
Trait Definition Pattern
src/utils/vec_extensions.rs:1-10 (referenced via src/utils.rs:7-8)
Sources: src/utils.rs:7-8
Dismiss
Refresh this wiki
Enter email to refresh