foreign-variables
It can be used to fetch data provided by application.
let mut s = ftd::interpret(name, source)?; loop { ftd::Interpreter::StuckOnImport { module, state: mut st, } => { let source = if module.eq("fpm/time") { st.add_foreign_variable_prefix(module.as_str()); "".to_string() } else { lib.get_with_result(module.as_str(), &st.tdoc(&mut Default::default()))? }; s = st.continue_after_import(module.as_str(), source.as_str())?; } ftd::Interpreter::StuckOnForeignVariable { variable, state } => { let value = resolve_foreign_variable(variable.as_str(), name)?; s = state.continue_after_variable(variable.as_str(), value)? } } fn resolve_foreign_variable(variable: &str, doc_name: &str) -> ftd::p1::Result<ftd::Value> { match variable.strip_prefix("fpm/time#") { Some("now-str") => Ok(ftd::Value::String { text: std::str::from_utf8( std::process::Command::new("date") .output() .expect("failed to execute process") .stdout .as_slice(), ) .unwrap() .to_string(), source: ftd::TextSource::Header, }), _ => ftd::e2(format!("{} not found", variable).as_str(), doc_name, 0), } }
fpm/time.now-str
-- import: fpm/time -- ftd.text: $time.now-str
-- import: fpm/time
statement exposes foreign variable fpm/time.now-str
which returns the current date and time as a string. The output looks something like this
Mon Jun 13 15:08:18 IST 2022