feedr 0.7.0

Feedr is a feature-rich terminal-based RSS/Atom feed reader written in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
use anyhow::{bail, Context, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;
use std::fs;
use std::path::{Path, PathBuf};

/// Main configuration structure for Feedr
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
pub struct Config {
    #[serde(default)]
    pub general: GeneralConfig,
    #[serde(default)]
    pub network: NetworkConfig,
    #[serde(default)]
    pub ui: UiConfig,
    #[serde(default)]
    pub default_feeds: Vec<DefaultFeed>,
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub keybindings: HashMap<String, toml::Value>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct GeneralConfig {
    /// Maximum number of items to show on the dashboard
    #[serde(default = "default_max_dashboard_items")]
    pub max_dashboard_items: usize,
    /// Auto-refresh interval in seconds (0 = disabled)
    #[serde(default)]
    pub auto_refresh_interval: u64,
    /// Enable automatic background refresh
    #[serde(default)]
    pub refresh_enabled: bool,
    /// Delay in milliseconds between requests to the same domain (for rate limiting)
    #[serde(default = "default_refresh_rate_limit_delay")]
    pub refresh_rate_limit_delay: u64,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct NetworkConfig {
    /// HTTP request timeout in seconds
    #[serde(default = "default_http_timeout")]
    pub http_timeout: u64,
    /// User agent string for HTTP requests
    #[serde(default = "default_user_agent")]
    pub user_agent: String,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct UiConfig {
    /// Tick rate for UI updates in milliseconds
    #[serde(default = "default_tick_rate")]
    pub tick_rate: u64,
    /// Error message display timeout in milliseconds
    #[serde(default = "default_error_timeout")]
    pub error_display_timeout: u64,
    /// Color theme (light or dark)
    #[serde(default)]
    pub theme: Theme,
    /// Compact mode for small terminals (auto, always, never)
    #[serde(default)]
    pub compact_mode: CompactMode,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum Theme {
    Light,
    #[default]
    Dark,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum CompactMode {
    #[default]
    Auto,
    Always,
    Never,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct DefaultFeed {
    pub url: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub category: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub headers: Option<HashMap<String, String>>,
    /// Per-feed refresh interval in seconds; None = use global interval
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub refresh_interval: Option<u64>,
}

// Default value functions
fn default_max_dashboard_items() -> usize {
    100
}

fn default_refresh_rate_limit_delay() -> u64 {
    2000 // 2 seconds for Reddit safety
}

fn default_http_timeout() -> u64 {
    15
}

fn default_user_agent() -> String {
    "Mozilla/5.0 (compatible; Feedr/1.0; +https://github.com/bahdotsh/feedr)".to_string()
}

fn default_tick_rate() -> u64 {
    100
}

fn default_error_timeout() -> u64 {
    3000
}

impl Default for GeneralConfig {
    fn default() -> Self {
        Self {
            max_dashboard_items: default_max_dashboard_items(),
            auto_refresh_interval: 0,
            refresh_enabled: false,
            refresh_rate_limit_delay: default_refresh_rate_limit_delay(),
        }
    }
}

impl Default for NetworkConfig {
    fn default() -> Self {
        Self {
            http_timeout: default_http_timeout(),
            user_agent: default_user_agent(),
        }
    }
}

impl Default for UiConfig {
    fn default() -> Self {
        Self {
            tick_rate: default_tick_rate(),
            error_display_timeout: default_error_timeout(),
            theme: Theme::default(),
            compact_mode: CompactMode::default(),
        }
    }
}

impl fmt::Display for Theme {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Theme::Light => write!(f, "light"),
            Theme::Dark => write!(f, "dark"),
        }
    }
}

impl fmt::Display for CompactMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CompactMode::Auto => write!(f, "auto"),
            CompactMode::Always => write!(f, "always"),
            CompactMode::Never => write!(f, "never"),
        }
    }
}

impl Config {
    /// Get a config value by dot-notation key
    pub fn get_value(&self, key: &str) -> Result<String> {
        match key {
            "general.max_dashboard_items" => Ok(self.general.max_dashboard_items.to_string()),
            "general.auto_refresh_interval" => Ok(self.general.auto_refresh_interval.to_string()),
            "general.refresh_enabled" => Ok(self.general.refresh_enabled.to_string()),
            "general.refresh_rate_limit_delay" => {
                Ok(self.general.refresh_rate_limit_delay.to_string())
            }
            "network.http_timeout" => Ok(self.network.http_timeout.to_string()),
            "network.user_agent" => Ok(self.network.user_agent.clone()),
            "ui.tick_rate" => Ok(self.ui.tick_rate.to_string()),
            "ui.error_display_timeout" => Ok(self.ui.error_display_timeout.to_string()),
            "ui.theme" => Ok(self.ui.theme.to_string()),
            "ui.compact_mode" => Ok(self.ui.compact_mode.to_string()),
            k if k.starts_with("default_feeds") => {
                bail!("Feed management is not supported via CLI. Use 'feedr config --tui' instead.")
            }
            _ => bail!("Unknown config key: {}", key),
        }
    }

    /// Validate and set a config value by dot-notation key
    pub fn validate_and_set(&mut self, key: &str, value: &str) -> Result<()> {
        match key {
            "general.max_dashboard_items" => {
                let v: usize = value.parse().context("Expected a positive integer")?;
                if !(1..=10000).contains(&v) {
                    bail!("Value must be between 1 and 10000");
                }
                self.general.max_dashboard_items = v;
            }
            "general.auto_refresh_interval" => {
                let v: u64 = value.parse().context("Expected a non-negative integer")?;
                if v > 86400 {
                    bail!("Value must be between 0 and 86400");
                }
                self.general.auto_refresh_interval = v;
            }
            "general.refresh_enabled" => {
                let v: bool = value.parse().context("Expected 'true' or 'false'")?;
                self.general.refresh_enabled = v;
            }
            "general.refresh_rate_limit_delay" => {
                let v: u64 = value.parse().context("Expected a non-negative integer")?;
                if v > 60000 {
                    bail!("Value must be between 0 and 60000");
                }
                self.general.refresh_rate_limit_delay = v;
            }
            "network.http_timeout" => {
                let v: u64 = value.parse().context("Expected a positive integer")?;
                if !(1..=300).contains(&v) {
                    bail!("Value must be between 1 and 300");
                }
                self.network.http_timeout = v;
            }
            "network.user_agent" => {
                if value.is_empty() {
                    bail!("User agent cannot be empty");
                }
                self.network.user_agent = value.to_string();
            }
            "ui.tick_rate" => {
                let v: u64 = value.parse().context("Expected a positive integer")?;
                if !(10..=1000).contains(&v) {
                    bail!("Value must be between 10 and 1000");
                }
                self.ui.tick_rate = v;
            }
            "ui.error_display_timeout" => {
                let v: u64 = value.parse().context("Expected a positive integer")?;
                if !(500..=30000).contains(&v) {
                    bail!("Value must be between 500 and 30000");
                }
                self.ui.error_display_timeout = v;
            }
            "ui.theme" => match value {
                "light" => self.ui.theme = Theme::Light,
                "dark" => self.ui.theme = Theme::Dark,
                _ => bail!("Invalid theme '{}'. Valid values: light, dark", value),
            },
            "ui.compact_mode" => match value {
                "auto" => self.ui.compact_mode = CompactMode::Auto,
                "always" => self.ui.compact_mode = CompactMode::Always,
                "never" => self.ui.compact_mode = CompactMode::Never,
                _ => bail!(
                    "Invalid compact_mode '{}'. Valid values: auto, always, never",
                    value
                ),
            },
            k if k.starts_with("default_feeds") => {
                bail!("Feed management is not supported via CLI. Use 'feedr config --tui' instead.")
            }
            _ => bail!("Unknown config key: {}", key),
        }
        Ok(())
    }

    /// Load configuration from the XDG config directory
    /// Falls back to default configuration if file doesn't exist
    pub fn load() -> Result<Self> {
        let config_path = Self::config_path();

        if config_path.exists() {
            let contents =
                fs::read_to_string(&config_path).context("Failed to read config file")?;

            let config: Config =
                toml::from_str(&contents).context("Failed to parse config file")?;

            Ok(config)
        } else {
            // Config doesn't exist, create it with defaults
            let config = Config::default();

            // Try to save the default config for future use
            if let Err(e) = config.save() {
                // Don't fail if we can't save, just use defaults
                eprintln!("Warning: Could not create default config file: {}", e);
            }

            Ok(config)
        }
    }

    /// Save configuration to the XDG config directory
    pub fn save(&self) -> Result<()> {
        let config_path = Self::config_path();

        // Ensure the parent directory exists
        if let Some(parent) = config_path.parent() {
            fs::create_dir_all(parent).context("Failed to create config directory")?;
        }

        let toml_string = toml::to_string_pretty(self).context("Failed to serialize config")?;

        // Add helpful comments to the config file
        let commented_config = Self::add_comments(&toml_string);

        fs::write(&config_path, commented_config).context("Failed to write config file")?;

        Ok(())
    }

    /// Get the path to the config file following XDG specifications
    pub fn config_path() -> PathBuf {
        let mut path = dirs::config_dir().unwrap_or_else(|| Path::new(".").to_path_buf());
        path.push("feedr");
        path.push("config.toml");
        path
    }

    /// Add helpful comments to the generated TOML config
    fn add_comments(toml: &str) -> String {
        format!(
            "# Feedr Configuration File\n\
             # This file is automatically generated with default values.\n\
             # You can modify any settings below to customize Feedr's behavior.\n\
             #\n\
             # For more information, visit: https://github.com/bahdotsh/feedr\n\
             \n\
             {}\n\
             \n\
             # Background Refresh Settings:\n\
             # - refresh_enabled: Enable automatic background refresh (default: false)\n\
             # - auto_refresh_interval: Time in seconds between auto-refreshes (default: 0/disabled)\n\
             # - refresh_rate_limit_delay: Delay in milliseconds between requests to same domain (default: 2000ms)\n\
             #   This prevents \"too many requests\" errors, especially for Reddit feeds\n\
             #\n\
             # UI Theme Settings:\n\
             # - theme: Choose between \"light\" or \"dark\" theme (default: dark)\n\
             #   You can also toggle the theme in the app by pressing 't'\n\
             #\n\
             # Example configuration for auto-refresh every 5 minutes:\n\
             # [general]\n\
             # refresh_enabled = true\n\
             # auto_refresh_interval = 300\n\
             # refresh_rate_limit_delay = 2000\n\
             #\n\
             # [ui]\n\
             # theme = \"light\"\n\
             # compact_mode = \"auto\"  # auto (default), always, or never\n\
             #\n\
             # Example default feeds configuration:\n\
             # [[default_feeds]]\n\
             # url = \"https://example.com/feed.xml\"\n\
             # category = \"News\"\n\
             #\n\
             # [[default_feeds]]\n\
             # url = \"https://another-example.com/rss\"\n\
             # category = \"Tech\"\n\
             #\n\
             # Authenticated feed example (custom HTTP headers):\n\
             # [[default_feeds]]\n\
             # url = \"https://private.example.com/feed.xml\"\n\
             # [default_feeds.headers]\n\
             # Authorization = \"Bearer your_token_here\"\n",
            toml
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_config() {
        let config = Config::default();
        assert_eq!(config.general.max_dashboard_items, 100);
        assert_eq!(config.network.http_timeout, 15);
        assert_eq!(config.ui.tick_rate, 100);
        assert_eq!(config.ui.error_display_timeout, 3000);
    }

    #[test]
    fn test_default_feed_with_headers() {
        let toml_str = r#"
            [[default_feeds]]
            url = "https://example.com/feed.xml"

            [[default_feeds]]
            url = "https://private.example.com/feed.xml"
            [default_feeds.headers]
            Authorization = "Bearer token123"
            X-Custom = "value"
        "#;
        let config: Config = toml::from_str(toml_str).unwrap();
        assert_eq!(config.default_feeds.len(), 2);
        assert!(config.default_feeds[0].headers.is_none());
        let headers = config.default_feeds[1].headers.as_ref().unwrap();
        assert_eq!(headers.get("Authorization").unwrap(), "Bearer token123");
        assert_eq!(headers.get("X-Custom").unwrap(), "value");
    }

    #[test]
    fn test_config_serialization() {
        let config = Config::default();
        let toml_str = toml::to_string(&config).unwrap();
        let deserialized: Config = toml::from_str(&toml_str).unwrap();

        assert_eq!(
            config.general.max_dashboard_items,
            deserialized.general.max_dashboard_items
        );
        assert_eq!(
            config.network.http_timeout,
            deserialized.network.http_timeout
        );
    }
}