use crate::macro_processor::error::SMPError;
use crate::macro_processor::macro_processor::MacroProcessorWarning;
use crate::macro_processor::MacroProcessor;
/// Warn on dead link
pub fn smp_builtin_wodl(
smp: &mut MacroProcessor,
macro_name: &str,
args: &mut [String],
) -> Result<String, SMPError> {
if args.len() != 1 {
smp.warnings
.push(MacroProcessorWarning::from_macro_invocation(
macro_name,
args,
format!("Wrong number of arguments, expected 1"),
));
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());
}
};
if response.status_code != 200 || response.reason_phrase != "OK" {
smp.warnings
.push(MacroProcessorWarning::from_macro_invocation(
macro_name,
args,
format!(
"Dead link {:#?} ({} {})!",
args[0], response.status_code, response.reason_phrase
),
));
}
Ok(args[0].to_string())
}