Skip to content

Commit 8a20f72

Browse files
unhappychoiceclaude
andcommitted
fix: remove TrendingConfig and hardcode values
- Remove TrendingConfig struct and related functions from config - Hardcode cache TTL to 5 minutes (300 seconds) - Hardcode rate limit to 100ms - Simplify code by removing config dependency 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 9347cf4 commit 8a20f72

File tree

2 files changed

+11
-50
lines changed

2 files changed

+11
-50
lines changed

src/cli/commands/trending.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,7 @@ impl TrendingCache {
3434
cache_dir.push(".gittype");
3535
cache_dir.push("trending_cache");
3636

37-
let ttl_seconds = if let Ok(config_manager) = crate::config::ConfigManager::new() {
38-
config_manager.get_config().trending.cache_ttl_minutes * 60
39-
} else {
40-
300 // 5 minutes default
41-
};
37+
let ttl_seconds = 300; // 5 minutes
4238

4339
Self {
4440
cache_dir,
@@ -57,11 +53,11 @@ impl TrendingCache {
5753

5854
let current_time = SystemTime::now()
5955
.duration_since(UNIX_EPOCH)
60-
.unwrap()
56+
.unwrap_or_else(|_| Duration::from_secs(0))
6157
.as_secs();
6258

6359
// Check if cache is still valid
64-
if current_time - cache_data.timestamp < self.ttl_seconds {
60+
if current_time.saturating_sub(cache_data.timestamp) < self.ttl_seconds {
6561
Some(cache_data.repositories)
6662
} else {
6763
// Remove expired cache file
@@ -75,7 +71,7 @@ impl TrendingCache {
7571

7672
let current_time = SystemTime::now()
7773
.duration_since(UNIX_EPOCH)
78-
.unwrap()
74+
.unwrap_or_else(|_| Duration::from_secs(0))
7975
.as_secs();
8076

8177
let cache_data = TrendingCacheData {
@@ -97,14 +93,14 @@ impl TrendingCache {
9793

9894
let current_time = SystemTime::now()
9995
.duration_since(UNIX_EPOCH)
100-
.unwrap()
96+
.unwrap_or_else(|_| Duration::from_secs(0))
10197
.as_secs();
10298

10399
if let Ok(entries) = fs::read_dir(&self.cache_dir) {
104100
for entry in entries.flatten() {
105101
if let Ok(content) = fs::read_to_string(entry.path()) {
106102
if let Ok(cache_data) = serde_json::from_str::<TrendingCacheData>(&content) {
107-
if current_time - cache_data.timestamp >= self.ttl_seconds {
103+
if current_time.saturating_sub(cache_data.timestamp) >= self.ttl_seconds {
108104
let _ = fs::remove_file(entry.path());
109105
}
110106
}
@@ -151,6 +147,9 @@ pub async fn run_trending(
151147
command: None,
152148
};
153149
return crate::cli::commands::run_game_session(cli);
150+
} else {
151+
eprintln!("⚠️ Repository '{}' not found in trending list", name);
152+
return Ok(());
154153
}
155154
} else if language.is_some() {
156155
// Language provided - show repositories directly
@@ -221,11 +220,7 @@ pub async fn fetch_trending_repositories_cached(
221220
TRENDING_CACHE.cleanup_expired();
222221

223222
// Rate limiting: wait a bit between API calls to be respectful
224-
let rate_limit_ms = if let Ok(config_manager) = crate::config::ConfigManager::new() {
225-
config_manager.get_config().trending.rate_limit_ms
226-
} else {
227-
100 // default 100ms
228-
};
223+
let rate_limit_ms = 100; // 100ms
229224
tokio::time::sleep(Duration::from_millis(rate_limit_ms)).await;
230225

231226
// Fetch from API
@@ -281,6 +276,7 @@ async fn fetch_trending_repositories(
281276
.get(&url)
282277
.header("User-Agent", "gittype")
283278
.header("Accept", "application/json")
279+
.timeout(Duration::from_secs(10))
284280
.send()
285281
.await?;
286282

src/config/mod.rs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::path::PathBuf;
66
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
77
pub struct Config {
88
pub theme: ThemeConfig,
9-
pub trending: TrendingConfig,
109
}
1110

1211
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -29,40 +28,6 @@ fn default_theme_id() -> String {
2928
"default".to_string()
3029
}
3130

32-
#[derive(Debug, Clone, Serialize, Deserialize)]
33-
pub struct TrendingConfig {
34-
#[serde(default = "default_cache_ttl_minutes")]
35-
pub cache_ttl_minutes: u64,
36-
#[serde(default = "default_rate_limit_ms")]
37-
pub rate_limit_ms: u64,
38-
#[serde(default = "default_period")]
39-
pub default_period: String,
40-
#[serde(default)]
41-
pub default_language: Option<String>,
42-
}
43-
44-
impl Default for TrendingConfig {
45-
fn default() -> Self {
46-
Self {
47-
cache_ttl_minutes: default_cache_ttl_minutes(),
48-
rate_limit_ms: default_rate_limit_ms(),
49-
default_period: default_period(),
50-
default_language: None,
51-
}
52-
}
53-
}
54-
55-
fn default_cache_ttl_minutes() -> u64 {
56-
5 // 5 minutes cache
57-
}
58-
59-
fn default_rate_limit_ms() -> u64 {
60-
100 // 100ms rate limit
61-
}
62-
63-
fn default_period() -> String {
64-
"daily".to_string()
65-
}
6631

6732
pub struct ConfigManager {
6833
config: Config,

0 commit comments

Comments
 (0)