pub struct CalendarClient { /* private fields */ }Expand description
Client responsible for fetching economic calendar releases over HTTP and managing disk caching.
Implementations§
Source§impl CalendarClient
impl CalendarClient
Sourcepub fn default_cache_dir() -> PathBuf
pub fn default_cache_dir() -> PathBuf
Returns the default platform cache directory for redfolder.
- On Windows: Respects
%LOCALAPPDATA%\redfolder\cache,%APPDATA%\redfolder\cache, or%USERPROFILE%\.cache\redfolder. - On Linux/Unix: Respects
$XDG_CACHE_HOME/redfolderor$HOME/.cache/redfolder. - Fallback: System temporary directory (
redfolder_cache).
Sourcepub fn try_new(cache_dir: Option<PathBuf>) -> Result<Self>
pub fn try_new(cache_dir: Option<PathBuf>) -> Result<Self>
Create a new CalendarClient with an optional cache directory fallibly.
Sourcepub fn new(cache_dir: Option<PathBuf>) -> Self
pub fn new(cache_dir: Option<PathBuf>) -> Self
Create a new CalendarClient with an optional cache directory.
If None, defaults to CalendarClient::default_cache_dir().
Sourcepub fn try_without_cache() -> Result<Self>
pub fn try_without_cache() -> Result<Self>
Create a new CalendarClient without any local disk caching fallibly.
Sourcepub fn without_cache() -> Self
pub fn without_cache() -> Self
Create a new CalendarClient without any local disk caching.
Sourcepub fn with_user_agent(
cache_dir: Option<PathBuf>,
user_agent: &str,
) -> Result<Self>
pub fn with_user_agent( cache_dir: Option<PathBuf>, user_agent: &str, ) -> Result<Self>
Create a new CalendarClient with a custom User-Agent string.
Sourcepub fn with_options(
client: Client,
calendar_url: impl Into<String>,
cache_dir: Option<PathBuf>,
request_timeout: Duration,
) -> Self
pub fn with_options( client: Client, calendar_url: impl Into<String>, cache_dir: Option<PathBuf>, request_timeout: Duration, ) -> Self
Create with custom options.
Sourcepub fn with_fallback_url(self, url: impl Into<String>) -> Self
pub fn with_fallback_url(self, url: impl Into<String>) -> Self
Add a fallback calendar endpoint URL used if the primary URL fails.
Sourcepub fn with_fallback_urls<I, S>(self, urls: I) -> Self
pub fn with_fallback_urls<I, S>(self, urls: I) -> Self
Add multiple fallback calendar endpoint URLs.
Sourcepub fn fallback_urls(&self) -> &[String]
pub fn fallback_urls(&self) -> &[String]
Configured fallback calendar URLs.
Sourcepub fn calendar_url(&self) -> &str
pub fn calendar_url(&self) -> &str
Primary calendar URL.
Sourcepub fn set_calendar_url(&mut self, url: impl Into<String>)
pub fn set_calendar_url(&mut self, url: impl Into<String>)
Set the primary calendar URL.
Sourcepub fn add_fallback_url(&mut self, url: impl Into<String>)
pub fn add_fallback_url(&mut self, url: impl Into<String>)
Add a fallback calendar URL.
Sourcepub fn with_max_response_bytes(self, max_bytes: usize) -> Self
pub fn with_max_response_bytes(self, max_bytes: usize) -> Self
Configure the maximum response body size in bytes to prevent unbounded allocations.
Sourcepub fn set_max_response_bytes(&mut self, max_bytes: usize)
pub fn set_max_response_bytes(&mut self, max_bytes: usize)
Set maximum allowable response body size in bytes.
Sourcepub fn max_response_bytes(&self) -> usize
pub fn max_response_bytes(&self) -> usize
Maximum allowable response body size in bytes.
Sourcepub fn with_max_event_count(self, max_events: usize) -> Self
pub fn with_max_event_count(self, max_events: usize) -> Self
Configure the maximum number of raw events permitted from upstream.
Sourcepub fn set_max_event_count(&mut self, max_events: usize)
pub fn set_max_event_count(&mut self, max_events: usize)
Set maximum allowable raw event count.
Sourcepub fn max_event_count(&self) -> usize
pub fn max_event_count(&self) -> usize
Maximum allowable raw event count.
Sourcepub fn with_max_retries(self, max_retries: usize) -> Self
pub fn with_max_retries(self, max_retries: usize) -> Self
Configure maximum HTTP retry attempts for transient errors (429, 408, 5xx).
Sourcepub fn max_retries(&self) -> usize
pub fn max_retries(&self) -> usize
Configured maximum retry attempts.
Sourcepub fn with_backoff(
self,
initial_delay: Duration,
max_retry_after: Duration,
) -> Self
pub fn with_backoff( self, initial_delay: Duration, max_retry_after: Duration, ) -> Self
Configure exponential backoff parameters.
Sourcepub fn backoff_initial_delay(&self) -> Duration
pub fn backoff_initial_delay(&self) -> Duration
Initial exponential backoff delay duration.
Sourcepub fn max_retry_after(&self) -> Duration
pub fn max_retry_after(&self) -> Duration
Maximum duration allowed for server-specified Retry-After delays.
Sourcepub fn with_overall_timeout(self, timeout: Duration) -> Self
pub fn with_overall_timeout(self, timeout: Duration) -> Self
Configure an overall wall-clock timeout ceiling spanning all retry attempts and backoffs.
Sourcepub fn overall_timeout(&self) -> Option<Duration>
pub fn overall_timeout(&self) -> Option<Duration>
Overall wall-clock timeout ceiling if set.
Sourcepub fn with_integrity_validator(
self,
validator: impl Fn(&[RawCalendarEvent]) -> Result<()> + Send + Sync + 'static,
) -> Self
pub fn with_integrity_validator( self, validator: impl Fn(&[RawCalendarEvent]) -> Result<()> + Send + Sync + 'static, ) -> Self
Configure a custom pluggable integrity validator callback.
Sourcepub fn with_timezone(self, tz: Tz) -> Self
pub fn with_timezone(self, tz: Tz) -> Self
Configure the default source timezone for naive calendar timestamps.
Sourcepub fn set_calendar_timezone(&mut self, tz: Option<Tz>)
pub fn set_calendar_timezone(&mut self, tz: Option<Tz>)
Set the default source timezone for naive calendar timestamps.
Sourcepub fn calendar_timezone(&self) -> Option<Tz>
pub fn calendar_timezone(&self) -> Option<Tz>
Default source timezone if configured.
Sourcepub fn with_max_stale_age(self, max_age: Option<Duration>) -> Self
pub fn with_max_stale_age(self, max_age: Option<Duration>) -> Self
Configure the maximum allowable age for stale cache fallback on network failures.
If None, stale cache fallback is disabled.
Sourcepub fn set_max_stale_cache_age(&mut self, max_age: Option<Duration>)
pub fn set_max_stale_cache_age(&mut self, max_age: Option<Duration>)
Set maximum allowable age for stale cache fallback.
Sourcepub fn max_stale_cache_age(&self) -> Option<Duration>
pub fn max_stale_cache_age(&self) -> Option<Duration>
Configured maximum stale cache age.
Sourcepub fn with_ttl(self, ttl: Duration) -> Self
pub fn with_ttl(self, ttl: Duration) -> Self
Configure an optional Time-To-Live (TTL) for the local disk cache.
Sourcepub fn set_cache_ttl(&mut self, ttl: Option<Duration>)
pub fn set_cache_ttl(&mut self, ttl: Option<Duration>)
Set an optional Time-To-Live (TTL) for the local disk cache.
Sourcepub fn set_cache_path(&mut self, path: impl AsRef<Path>)
pub fn set_cache_path(&mut self, path: impl AsRef<Path>)
Set an explicit cache file path.
Sourcepub fn cache_path(&self) -> Option<&Path>
pub fn cache_path(&self) -> Option<&Path>
Cache file path if configured.
Sourcepub async fn fetch_remote(&self) -> Result<Vec<RawCalendarEvent>>
pub async fn fetch_remote(&self) -> Result<Vec<RawCalendarEvent>>
Fetch fresh calendar events directly from the remote API with bounded retry, response size checks, and fallback URL failover.
§Latency Ceiling Guarantee
Worst-case wall-clock latency per URL candidate is strictly bounded:
with default settings (30s request timeout, 2 retries, 10s max backoff), worst-case latency under
total network timeouts is (1 + 2) * 30s + 20s = 110s. Under immediate 429/5xx status responses,
worst-case latency is <= 21s.
If an overall_timeout is set, the entire operation across all candidates
is terminated when the deadline expires.
Sourcepub fn save_cache(&self, events: &[RawCalendarEvent]) -> Result<()>
pub fn save_cache(&self, events: &[RawCalendarEvent]) -> Result<()>
Save raw calendar events to the local disk cache atomically (if cache path is set).
On Unix systems, applies restrictive file permissions (0600) and directory permissions (0700) to prevent unauthorized access or tampering on shared multi-user hosts.
Sourcepub fn load_cache_data(&self) -> Option<CachedCalendarData>
pub fn load_cache_data(&self) -> Option<CachedCalendarData>
Load full cached calendar data including metadata, with recovery from legacy and corrupted caches.
Sourcepub fn load_cache(&self) -> Option<Vec<RawCalendarEvent>>
pub fn load_cache(&self) -> Option<Vec<RawCalendarEvent>>
Load raw calendar events from the local disk cache (if exists and readable).
Sourcepub fn is_cache_valid(&self) -> bool
pub fn is_cache_valid(&self) -> bool
Checks whether the disk cache exists and is within its configured TTL.
Legacy caches (version 0) lacking fetch timestamps are rejected.
Sourcepub async fn force_fetch(&self) -> Result<Vec<RawCalendarEvent>>
pub async fn force_fetch(&self) -> Result<Vec<RawCalendarEvent>>
Fetch fresh calendar events directly from the remote API, bypassing cache, protecting against empty responses overwriting good data, and saving to cache on success.
Sourcepub async fn fetch_or_cached(&self) -> Result<Vec<RawCalendarEvent>>
pub async fn fetch_or_cached(&self) -> Result<Vec<RawCalendarEvent>>
Fetch fresh events from the remote API, automatically saving to cache on success, or falling back to local cache if the remote request fails.
Respects configured cache TTL and protects against empty responses overwriting good data.