use crate::dns::DnsPolicy;
use crate::error::FetchError;
use crate::fetchers::FetcherRegistry;
use crate::types::{FetchRequest, FetchResponse};
#[derive(Debug, Clone, Default)]
pub struct FetchOptions {
pub user_agent: Option<String>,
pub allow_prefixes: Vec<String>,
pub block_prefixes: Vec<String>,
pub enable_markdown: bool,
pub enable_text: bool,
pub dns_policy: DnsPolicy,
}
pub async fn fetch(req: FetchRequest) -> Result<FetchResponse, FetchError> {
let options = FetchOptions {
enable_markdown: true,
enable_text: true,
..Default::default()
};
fetch_with_options(req, options).await
}
pub async fn fetch_with_options(
req: FetchRequest,
options: FetchOptions,
) -> Result<FetchResponse, FetchError> {
if req.url.is_empty() {
return Err(FetchError::MissingUrl);
}
let registry = FetcherRegistry::with_defaults();
registry.fetch(req, options).await
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_fetch_empty_url() {
let req = FetchRequest::new("");
let result = fetch(req).await;
assert!(matches!(result, Err(FetchError::MissingUrl)));
}
#[tokio::test]
async fn test_fetch_invalid_scheme() {
let req = FetchRequest::new("ftp://example.com");
let result = fetch(req).await;
assert!(matches!(result, Err(FetchError::InvalidUrlScheme)));
}
#[tokio::test]
async fn test_fetch_options_default() {
let options = FetchOptions::default();
assert!(options.user_agent.is_none());
assert!(options.allow_prefixes.is_empty());
assert!(options.block_prefixes.is_empty());
assert!(!options.enable_markdown);
assert!(!options.enable_text);
assert!(options.dns_policy.block_private);
}
}