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, 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)]56struct ResolvedImportResolver {57 resolved: RefCell<FxHashMap<(SourcePath, ResolvePathOwned), (SourcePath, bool)>>,58}59impl ImportResolver for ResolvedImportResolver {60 fn load_file_contents(&self, _resolved: &SourcePath) -> crate::Result<Vec<u8>> {61 unreachable!("all files should be loaded at this point");62 }6364 fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> crate::Result<SourcePath> {65 Ok(self66 .resolved67 .borrow()68 .get(&(from.clone(), path.as_path().to_owned()))69 .expect("all imports should be resolved at this point")70 .071 .clone())72 }7374 fn resolve_from_default(&self, path: &dyn AsPathLike) -> crate::Result<SourcePath> {75 self.resolve_from(&SourcePath::default(), path)76 }77}7879enum Job {80 LoadFile { path: SourcePath, parse: bool },81 ParseFile(SourcePath),82 ResolveImport { from: SourcePath, import: Import },83}8485#[allow(clippy::future_not_send)]86pub async fn async_import<H>(s: State, handler: H, path: &dyn AsPathLike) -> Result<(), H::Error>87where88 H: AsyncImportResolver,89{90 let resolved = (s.import_resolver() as &dyn Any)91 .downcast_ref::<ResolvedImportResolver>()92 .expect("for async imports, import_resolver should be set to ResolvedImportResolver");9394 let mut queue = vec![Job::LoadFile {95 path: handler.resolve_from_default(path).await?,96 parse: true,97 }];98 while let Some(job) = queue.pop() {99 match job {100 Job::LoadFile { path, parse } => {101 if !s.0.file_cache.borrow().contains_key(&path) {102 let data = handler.load_file_contents(&path).await?;103 s.0.file_cache104 .borrow_mut()105 .insert(path.clone(), FileData::new_bytes(data.as_slice().into()));106 }107 if parse {108 queue.push(Job::ParseFile(path));109 }110 }111 Job::ParseFile(path) => {112 if let Some(file) = s.0.file_cache.borrow_mut().get_mut(&path) {113 if file.parsed.is_none() {114 let Some(code) = file.get_string() else {115 continue;116 };117 let source = Source::new(path.clone(), code.clone());118 119 file.parsed = crate::parse_jsonnet(&code, source).map(Rc::new).ok();120 if let Some(parsed) = &file.parsed {121 let mut imports = FoundImports(vec![]);122 imports.visit_expr(parsed);123 for import in imports.0 {124 queue.push(Job::ResolveImport {125 from: path.clone(),126 import,127 });128 }129 }130 }131 }132 }133 Job::ResolveImport { from, import } => {134 {135 let mut resolved_map = resolved.resolved.borrow_mut();136 if let Some((resolved, expression)) =137 resolved_map.get_mut(&(from.clone(), import.path.clone()))138 {139 if import.expression && !*expression {140 *expression = true;141 queue.push(Job::ParseFile(resolved.clone()));142 }143 continue;144 }145 }146 let resolved = handler.resolve_from(&from, &import.path).await?;147 queue.push(Job::LoadFile {148 path: resolved,149 parse: import.expression,150 });151 }152 }153 }154 Ok(())155}