Skip to content

Commit c108329

Browse files
authored
Merge pull request #8 from gcavelier/new-CacheArrets
Merge PR #8: Simplification CacheArrets avec ArretNaolib
2 parents ec5de9a + 2622a00 commit c108329

1 file changed

Lines changed: 15 additions & 13 deletions

File tree

‎src/main.rs‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ fn http_get_json<T: serde::de::DeserializeOwned>(
6262
// ============================================================================
6363

6464
struct CacheArrets {
65-
liste: Vec<(Box<str>, Box<str>)>, // (code, libelle)
65+
liste: Vec<ArretNaolib>,
6666
derniere_maj: Option<Instant>,
6767
}
6868

@@ -77,14 +77,9 @@ fn rafraichir_cache() -> Result<(), Box<dyn std::error::Error>> {
7777
let url = format!("{API_URL}/arrets.json");
7878
let arrets: Vec<ArretNaolib> = http_get_json(&url)?;
7979

80-
let liste: Vec<(Box<str>, Box<str>)> = arrets
81-
.into_iter()
82-
.map(|a| (a.code_lieu.into_boxed_str(), a.libelle.into_boxed_str()))
83-
.collect();
84-
8580
if let Ok(mut cache) = CACHE_ARRETS.write() {
86-
eprintln!("[INFO] Cache rafraîchi : {} arrêts", liste.len());
87-
cache.liste = liste;
81+
eprintln!("[INFO] Cache rafraîchi : {} arrêts", arrets.len());
82+
cache.liste = arrets;
8883
cache.derniere_maj = Some(Instant::now());
8984
}
9085

@@ -114,7 +109,12 @@ fn code_arret_valide(code: &str) -> bool {
114109
CACHE_ARRETS
115110
.read()
116111
.ok()
117-
.map(|c| c.liste.is_empty() || c.liste.iter().any(|a| a.0.eq_ignore_ascii_case(code)))
112+
.map(|c| {
113+
c.liste.is_empty()
114+
|| c.liste
115+
.iter()
116+
.any(|a| a.code_lieu.eq_ignore_ascii_case(code))
117+
})
118118
.unwrap_or(true)
119119
}
120120

@@ -323,18 +323,20 @@ fn handle_stops(params: &Params) -> (u16, String) {
323323
result.push('[');
324324

325325
let mut count = 0;
326-
for (code, libelle) in &cache.liste {
327-
if let Some(ref search) = params.search
326+
for ArretNaolib { code_lieu, libelle } in &cache.liste {
327+
if let Some(search) = &params.search
328328
&& !libelle.to_lowercase().contains(search)
329-
&& !code.to_lowercase().contains(search)
329+
&& !code_lieu.to_lowercase().contains(search)
330330
{
331331
continue;
332332
}
333333

334334
if count > 0 {
335335
result.push(',');
336336
}
337-
result.push_str(&format!(r#"{{"codeLieu":"{code}","libelle":"{libelle}"}}"#,));
337+
result.push_str(&format!(
338+
r#"{{"codeLieu":"{code_lieu}","libelle":"{libelle}"}}"#,
339+
));
338340
count += 1;
339341

340342
if count >= limit {

0 commit comments

Comments
 (0)