use cache by default, add clear-cache argument
Some checks failed
CI/CD Pipeline / test (push) Failing after 1m30s

This commit is contained in:
2024-02-15 09:24:30 +01:00
parent c511e5a4d8
commit ddb2ebe5b7
6 changed files with 134 additions and 41 deletions

View File

@ -1,9 +1,18 @@
//! Deals with getting all paragraphs for a given law text
mod parser;
use std::{
fs,
hash::{DefaultHasher, Hash, Hasher},
path::Path,
};
use log::info;
use crate::{law::LawBuilder, misc::Error};
use crate::{
law::LawBuilder,
misc::{get_cache_dir, Error},
};
use self::parser::Risdok;
@ -57,30 +66,23 @@ impl Parser {
}
}
#[cfg(not(test))]
fn fetch(url: &str) -> Result<String, Error> {
Ok(ureq::get(url).call()?.into_string()?)
}
#[cfg(test)]
fn fetch(url: &str) -> Result<String, Error> {
use std::{
collections::hash_map::DefaultHasher,
fs,
hash::{Hash, Hasher},
};
let mut hasher = DefaultHasher::new();
url.hash(&mut hasher);
let hash = format!("{:x}", hasher.finish());
let expected_filename = format!("./data/cache/par-{hash}");
let expected_filename = format!("{}par-{hash}", get_cache_dir()?);
match fs::read_to_string(&expected_filename) {
Ok(data) => Ok(data),
Err(_) => {
info!("Not finding url {url} in the cache, downloading...");
let data = ureq::get(url).call()?.into_string()?;
let path = Path::new(&expected_filename);
if let Some(parent) = path.parent() {
// Try to create the directory (and any necessary parent directories)
fs::create_dir_all(parent).expect("Unable to create directory");
}
fs::write(expected_filename, &data).expect("Unable to write file");
Ok(data)
}