1use std::{any::Any, cell::RefCell, future::Future, rc::Rc};23use jrsonnet_gcmodule::Acyclic;4use jrsonnet_ir::{IStr, Source, SourcePath, visit::Visitor};5use rustc_hash::FxHashMap;67use crate::{AsPathLike, FileData, ImportResolver, ResolvePathOwned, Result, State};89pub struct Import {10 path: ResolvePathOwned,11 expression: bool,12}1314pub struct FoundImports(Vec<Import>);15impl Visitor for FoundImports {16 fn visit_import(&mut self, expression: bool, value: IStr) {17 self.0.push(Import {18 path: ResolvePathOwned::Str(value.to_string()),19 expression,20 });21 }22}2324pub trait AsyncImportResolver {25 type Error;26 27 28 29 30 31 32 fn resolve_from(33 &self,34 from: &SourcePath,35 path: &dyn AsPathLike,36 ) -> impl Future<Output = Result<SourcePath, Self::Error>>;37 fn resolve_from_default(38 &self,39 path: &dyn AsPathLike,40 ) -> impl Future<Output = Result<SourcePath, Self::Error>> {41 async { self.resolve_from(&SourcePath::default(), path).await }42 }4344 45 46 47 48 49 fn load_file_contents(50 &self,51 resolved: &SourcePath,52 ) -> impl Future<Output = Result<Vec<u8>, Self::Error>>;53}5455#[derive(Acyclic, Default)]56pub struct ResolvedImportResolver {57 resolved: RefCell<FxHashMap<(SourcePath, ResolvePathOwned), (SourcePath, bool)>>,58}59impl ResolvedImportResolver {60 pub fn new() -> Self {61 Self::default()62 }63}64impl ImportResolver for ResolvedImportResolver {65 fn load_file_contents(&self, _resolved: &SourcePath) -> crate::Result<Vec<u8>> {66 unreachable!("all files should be loaded at this point");67 }6869 fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> crate::Result<SourcePath> {70 Ok(self71 .resolved72 .borrow()73 .get(&(from.clone(), path.as_path().to_owned()))74 .expect("all imports should be resolved at this point")75 .076 .clone())77 }7879 fn resolve_from_default(&self, path: &dyn AsPathLike) -> crate::Result<SourcePath> {80 self.resolve_from(&SourcePath::default(), path)81 }82}8384enum Job {85 LoadFile { path: SourcePath, parse: bool },86 ParseFile(SourcePath),87 ResolveImport { from: SourcePath, import: Import },88}8990#[allow(clippy::future_not_send)]91pub async fn async_import<H>(92 s: State,93 handler: H,94 from: &SourcePath,95 path: &dyn AsPathLike,96) -> Result<SourcePath, H::Error>97where98 H: AsyncImportResolver,99{100 let resolved = (s.import_resolver() as &dyn Any)101 .downcast_ref::<ResolvedImportResolver>()102 .expect("for async imports, import_resolver should be set to ResolvedImportResolver");103104 let entry = handler.resolve_from(from, path).await?;105 let mut queue = vec![Job::LoadFile {106 path: entry.clone(),107 parse: true,108 }];109 while let Some(job) = queue.pop() {110 match job {111 Job::LoadFile { path, parse } => {112 if !s.0.file_cache.borrow().contains_key(&path) {113 let data = handler.load_file_contents(&path).await?;114 s.0.file_cache115 .borrow_mut()116 .insert(path.clone(), FileData::new_bytes(data.as_slice().into()));117 }118 if parse {119 queue.push(Job::ParseFile(path));120 }121 }122 Job::ParseFile(path) => {123 if let Some(file) = s.0.file_cache.borrow_mut().get_mut(&path)124 && file.parsed.is_none()125 {126 let Some(code) = file.get_string() else {127 continue;128 };129 let source = Source::new(path.clone(), code.clone());130 131 file.parsed = crate::parse_jsonnet(&code, source).map(Rc::new).ok();132 if let Some(parsed) = &file.parsed {133 let mut imports = FoundImports(vec![]);134 imports.visit_expr(parsed);135 for import in imports.0 {136 queue.push(Job::ResolveImport {137 from: path.clone(),138 import,139 });140 }141 }142 }143 }144 Job::ResolveImport { from, import } => {145 {146 let mut resolved_map = resolved.resolved.borrow_mut();147 if let Some((resolved, expression)) =148 resolved_map.get_mut(&(from.clone(), import.path.clone()))149 {150 if import.expression && !*expression {151 *expression = true;152 queue.push(Job::ParseFile(resolved.clone()));153 }154 continue;155 }156 }157 let resolved_path = handler.resolve_from(&from, &import.path).await?;158 resolved.resolved.borrow_mut().insert(159 (from.clone(), import.path.clone()),160 (resolved_path.clone(), import.expression),161 );162 queue.push(Job::LoadFile {163 path: resolved_path,164 parse: import.expression,165 });166 }167 }168 }169 Ok(entry)170}