use cache by default, add clear-cache argument
Some checks failed
CI/CD Pipeline / test (push) Failing after 1m30s
Some checks failed
CI/CD Pipeline / test (push) Failing after 1m30s
This commit is contained in:
@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user