use std::collections::HashSet;
use std::path::PathBuf;
use std::time::Duration;
#[derive(Default)]
pub struct ScriptCtx {
pub argv: Vec<String>,
pub current_file: Option<PathBuf>,
pub required: HashSet<PathBuf>,
http_agent: Option<ureq::Agent>,
pub tests: Vec<TestCase>,
scratch: crate::scratch::ScratchRegistry,
}
pub struct TestCase {
pub name: String,
pub body: Vec<tatara_lisp::Spanned>,
}
impl ScriptCtx {
pub fn with_argv<I, S>(argv: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
Self {
argv: argv.into_iter().map(Into::into).collect(),
current_file: None,
required: HashSet::new(),
http_agent: None,
tests: Vec::new(),
scratch: crate::scratch::ScratchRegistry::default(),
}
}
pub fn scratch_dir(&mut self) -> std::io::Result<std::path::PathBuf> {
self.scratch.dir()
}
pub fn scratch_file(&mut self) -> std::io::Result<std::path::PathBuf> {
self.scratch.file()
}
#[must_use]
pub fn scratch_len(&self) -> usize {
self.scratch.len()
}
pub fn http(&mut self) -> &ureq::Agent {
self.http_agent.get_or_insert_with(|| {
ureq::Agent::config_builder()
.timeout_global(Some(Duration::from_secs(30)))
.build()
.new_agent()
})
}
}