Skip to main content

webfetch/
types.rs

1use serde::{Deserialize, Serialize};
2
3pub use crate::tls::TlsConfig;
4
5/// Result of fetching and converting a web page.
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct FetchResult {
8    pub title: String,
9    pub final_url: String,
10    pub content: String,
11    pub content_type: ContentType,
12    /// The detected source media kind: "html", "json", "text", or a raw
13    /// content-type for anything not rendered.
14    pub media: String,
15    pub token_estimate: usize,
16    pub references: Vec<UrlReference>,
17    #[serde(default)]
18    pub metadata: Metadata,
19    pub source: String,
20}
21
22/// Citation-oriented page metadata, all best-effort.
23#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
24pub struct Metadata {
25    #[serde(skip_serializing_if = "Option::is_none")]
26    pub description: Option<String>,
27    #[serde(skip_serializing_if = "Option::is_none")]
28    pub author: Option<String>,
29    #[serde(skip_serializing_if = "Option::is_none")]
30    pub published: Option<String>,
31    #[serde(skip_serializing_if = "Option::is_none")]
32    pub lang: Option<String>,
33    #[serde(skip_serializing_if = "Option::is_none")]
34    pub site_name: Option<String>,
35}
36
37/// A single preserved URL, recoverable by its `index`.
38#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
39pub struct UrlReference {
40    pub index: usize,
41    pub url: String,
42    /// The anchor text the link was attached to (best-effort).
43    pub text: String,
44}
45
46impl crate::refs::Referable for UrlReference {
47    fn index(&self) -> usize {
48        self.index
49    }
50    fn url(&self) -> &str {
51        &self.url
52    }
53}
54
55#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
56#[serde(rename_all = "lowercase")]
57pub enum ContentType {
58    Text,
59    Markdown,
60    Structured,
61}
62
63impl ContentType {
64    pub fn parse(s: &str) -> Self {
65        match s.to_ascii_lowercase().as_str() {
66            "markdown" | "md" => ContentType::Markdown,
67            "structured" | "json" => ContentType::Structured,
68            _ => ContentType::Text,
69        }
70    }
71}
72
73#[derive(Debug, Clone, Deserialize)]
74pub struct FetchOptions {
75    pub url: String,
76    pub content_type: ContentType,
77    pub max_tokens: Option<usize>,
78    pub timeout_secs: u64,
79    /// TLS trust configuration (OS store is honoured by default; this carries
80    /// the explicit `--ca-cert` / `--insecure` overrides).
81    #[serde(default)]
82    pub tls: TlsConfig,
83}
84
85impl Default for FetchOptions {
86    fn default() -> Self {
87        Self {
88            url: String::new(),
89            content_type: ContentType::Text,
90            max_tokens: None,
91            timeout_secs: 10,
92            tls: TlsConfig::default(),
93        }
94    }
95}