diff options
author | Qrius <[email protected]> | 2024-09-26 00:11:06 +0200 |
---|---|---|
committer | Qrius <[email protected]> | 2024-09-26 00:11:06 +0200 |
commit | e59a0f60e0cb5c38d666156b7558410a9dc7d725 (patch) | |
tree | 2ab48783a4babe8a85fd098253ff173d6c5c1c67 /src/macro_processor/deadlinks.rs | |
parent | 33413e77ddaaca00d6b93b3aa4c917e4e3e2b4f0 (diff) | |
download | skaldpress-e59a0f60e0cb5c38d666156b7558410a9dc7d725.tar.gz skaldpress-e59a0f60e0cb5c38d666156b7558410a9dc7d725.zip |
Cache links, so we don't have to wait for the same check on multiple runs
Diffstat (limited to 'src/macro_processor/deadlinks.rs')
-rw-r--r-- | src/macro_processor/deadlinks.rs | 43 |
1 files changed, 31 insertions, 12 deletions
diff --git a/src/macro_processor/deadlinks.rs b/src/macro_processor/deadlinks.rs index 061f729..f8bf352 100644 --- a/src/macro_processor/deadlinks.rs +++ b/src/macro_processor/deadlinks.rs @@ -1,6 +1,10 @@ use crate::macro_processor::error::SMPError; use crate::macro_processor::macro_processor::MacroProcessorWarning; use crate::macro_processor::MacroProcessor; +use core::cell::OnceCell; +use std::collections::HashMap; + +static mut LINK_CACHE: OnceCell<HashMap<String, (bool, i32, String)>> = OnceCell::new(); /// Warn on dead link pub fn smp_builtin_wodl( @@ -18,26 +22,41 @@ pub fn smp_builtin_wodl( return Ok(macro_name.to_string()); } - let response = match minreq::get(&args[0]).send() { - Ok(s) => s, - Err(e) => { - smp.warnings - .push(MacroProcessorWarning::from_macro_invocation( - macro_name, - args, - format!("Dead link {:#?} ({:#?})!", args[0], e), - )); - return Ok(args[0].to_string()); + let link_cache: &mut HashMap<String, (bool, i32, String)>; + unsafe { + let _ = LINK_CACHE.get_or_init(|| HashMap::new()); + link_cache = LINK_CACHE.get_mut().expect("Init of OnceCell failed!"); + } + + let link_info = match link_cache.get(&args[0]) { + Some(working_link) => working_link.clone(), + None => { + let response = match minreq::get(&args[0]).send() { + Ok(s) => s, + Err(e) => { + smp.warnings + .push(MacroProcessorWarning::from_macro_invocation( + macro_name, + args, + format!("Dead link {:#?} ({:#?})!", args[0], e), + )); + return Ok(args[0].to_string()); + } + }; + let working_link = response.status_code == 200 && response.reason_phrase == "OK"; + let info = (working_link, response.status_code, response.reason_phrase); + link_cache.insert(String::from(&args[0]), info.clone()); + info } }; - if response.status_code != 200 || response.reason_phrase != "OK" { + if !link_info.0 { smp.warnings .push(MacroProcessorWarning::from_macro_invocation( macro_name, args, format!( "Dead link {:#?} ({} {})!", - args[0], response.status_code, response.reason_phrase + args[0], link_info.1, link_info.2 ), )); } |