Skip to main content

feedparser_rs/
options.rs

1//! Parser configuration options
2//!
3//! This module provides configuration options for customizing feed parsing behavior.
4//! Options control features like URL resolution, HTML sanitization, and resource limits.
5
6use crate::limits::ParserLimits;
7
8/// Parser configuration options
9///
10/// Controls various aspects of feed parsing behavior including URL resolution,
11/// HTML sanitization, and resource limits for `DoS` protection.
12///
13/// # Examples
14///
15/// ```
16/// use feedparser_rs::ParseOptions;
17///
18/// // Default options (recommended for most use cases)
19/// let options = ParseOptions::default();
20/// assert!(options.resolve_relative_uris);
21/// assert!(options.sanitize_html);
22///
23/// // Custom options for restricted environment
24/// let custom = ParseOptions::default()
25///     .with_resolve_relative_uris(true)
26///     .with_sanitize_html(false)
27///     .with_limits(feedparser_rs::ParserLimits::strict());
28/// ```
29#[derive(Debug, Clone)]
30#[non_exhaustive]
31pub struct ParseOptions {
32    /// Whether to resolve relative URLs to absolute URLs
33    ///
34    /// When `true`, relative URLs in links, images, and other resources
35    /// are converted to absolute URLs using the feed's base URL.
36    ///
37    /// Default: `true`
38    ///
39    /// # Examples
40    ///
41    /// ```
42    /// use feedparser_rs::ParseOptions;
43    ///
44    /// let mut options = ParseOptions::default();
45    /// options.resolve_relative_uris = false; // Keep relative URLs
46    /// ```
47    pub resolve_relative_uris: bool,
48
49    /// Whether to sanitize HTML content in feed entries
50    ///
51    /// When `true`, HTML content in titles, summaries, and content blocks
52    /// is sanitized to remove potentially dangerous elements and attributes
53    /// (scripts, iframes, etc.) while preserving safe formatting.
54    ///
55    /// Default: `true`
56    ///
57    /// # Security
58    ///
59    /// Disabling HTML sanitization is **not recommended** unless you fully
60    /// trust the feed source and have other security measures in place.
61    ///
62    /// # Examples
63    ///
64    /// ```
65    /// use feedparser_rs::ParseOptions;
66    ///
67    /// let mut options = ParseOptions::default();
68    /// options.sanitize_html = false; // Disable for trusted feeds
69    /// ```
70    pub sanitize_html: bool,
71
72    /// Parser limits for `DoS` protection
73    ///
74    /// Controls maximum allowed sizes for collections, text fields,
75    /// and overall feed size to prevent resource exhaustion attacks.
76    ///
77    /// Default: `ParserLimits::default()`
78    ///
79    /// # Examples
80    ///
81    /// ```
82    /// use feedparser_rs::{ParseOptions, ParserLimits};
83    ///
84    /// let options = ParseOptions::default().with_limits(ParserLimits::strict());
85    /// ```
86    pub limits: ParserLimits,
87}
88
89impl Default for ParseOptions {
90    /// Creates default parse options
91    ///
92    /// Default configuration:
93    /// - `resolve_relative_uris`: `true`
94    /// - `sanitize_html`: `true`
95    /// - `limits`: `ParserLimits::default()`
96    ///
97    /// These defaults are suitable for most use cases and provide
98    /// good security and compatibility.
99    fn default() -> Self {
100        Self {
101            resolve_relative_uris: true,
102            sanitize_html: true,
103            limits: ParserLimits::default(),
104        }
105    }
106}
107
108impl ParseOptions {
109    /// Returns a copy with the parser limits replaced.
110    #[must_use]
111    pub const fn with_limits(mut self, limits: ParserLimits) -> Self {
112        self.limits = limits;
113        self
114    }
115
116    /// Returns a copy with HTML sanitization switched on or off.
117    #[must_use]
118    pub const fn with_sanitize_html(mut self, sanitize_html: bool) -> Self {
119        self.sanitize_html = sanitize_html;
120        self
121    }
122
123    /// Returns a copy with relative-URI resolution switched on or off.
124    #[must_use]
125    pub const fn with_resolve_relative_uris(mut self, resolve_relative_uris: bool) -> Self {
126        self.resolve_relative_uris = resolve_relative_uris;
127        self
128    }
129
130    /// Creates permissive parse options
131    ///
132    /// Suitable for trusted feeds where you want maximum compatibility
133    /// and performance:
134    /// - `resolve_relative_uris`: `true`
135    /// - `sanitize_html`: `false`
136    /// - `limits`: `ParserLimits::permissive()`
137    ///
138    /// # Security Warning
139    ///
140    /// Use only with trusted feed sources!
141    ///
142    /// # Examples
143    ///
144    /// ```
145    /// use feedparser_rs::ParseOptions;
146    ///
147    /// let options = ParseOptions::permissive();
148    /// assert!(!options.sanitize_html);
149    /// ```
150    #[must_use]
151    pub const fn permissive() -> Self {
152        Self {
153            resolve_relative_uris: true,
154            sanitize_html: false,
155            limits: ParserLimits::permissive(),
156        }
157    }
158
159    /// Creates strict parse options
160    ///
161    /// Suitable for untrusted feeds in resource-constrained environments:
162    /// - `resolve_relative_uris`: `false` (preserve original URLs)
163    /// - `sanitize_html`: `true` (remove dangerous content)
164    /// - `limits`: `ParserLimits::strict()` (tight resource limits)
165    ///
166    /// # Examples
167    ///
168    /// ```
169    /// use feedparser_rs::ParseOptions;
170    ///
171    /// let options = ParseOptions::strict();
172    /// assert!(options.sanitize_html);
173    /// assert!(!options.resolve_relative_uris);
174    /// ```
175    #[must_use]
176    pub const fn strict() -> Self {
177        Self {
178            resolve_relative_uris: false,
179            sanitize_html: true,
180            limits: ParserLimits::strict(),
181        }
182    }
183}
184
185#[cfg(test)]
186mod tests {
187    use super::*;
188
189    #[test]
190    fn test_default_options() {
191        let options = ParseOptions::default();
192        assert!(options.resolve_relative_uris);
193        assert!(options.sanitize_html);
194        assert_eq!(options.limits.max_entries, 10_000);
195    }
196
197    #[test]
198    fn test_permissive_options() {
199        let options = ParseOptions::permissive();
200        assert!(options.resolve_relative_uris);
201        assert!(!options.sanitize_html);
202        assert_eq!(options.limits.max_entries, 100_000);
203    }
204
205    #[test]
206    fn test_strict_options() {
207        let options = ParseOptions::strict();
208        assert!(!options.resolve_relative_uris);
209        assert!(options.sanitize_html);
210        assert_eq!(options.limits.max_entries, 1_000);
211    }
212
213    #[test]
214    fn test_custom_options() {
215        let options = ParseOptions {
216            resolve_relative_uris: false,
217            sanitize_html: false,
218            limits: ParserLimits::permissive(),
219        };
220        assert!(!options.resolve_relative_uris);
221        assert!(!options.sanitize_html);
222        assert_eq!(options.limits.max_entries, 100_000);
223    }
224
225    #[test]
226    fn test_options_clone() {
227        let options1 = ParseOptions::default();
228        let options2 = options1.clone();
229        assert_eq!(
230            options1.resolve_relative_uris,
231            options2.resolve_relative_uris
232        );
233        assert_eq!(options1.sanitize_html, options2.sanitize_html);
234    }
235
236    #[test]
237    fn test_options_debug() {
238        let options = ParseOptions::default();
239        let debug_str = format!("{options:?}");
240        assert!(debug_str.contains("ParseOptions"));
241        assert!(debug_str.contains("resolve_relative_uris"));
242        assert!(debug_str.contains("sanitize_html"));
243    }
244}