git.delta.rocks / jrsonnet / refs/commits / b9cb94ab0371

difftreelog

feat async import ergonomics

mqslvqlpYaroslav Bolyukin2026-05-05parent: #3738ed2.patch.diff
in: master

1 file changed

modifiedcrates/jrsonnet-evaluator/src/async_import.rsdiffbeforeafterboth
52 ) -> impl Future<Output = Result<Vec<u8>, Self::Error>>;52 ) -> impl Future<Output = Result<Vec<u8>, Self::Error>>;
53}53}
5454
55#[derive(Acyclic)]55#[derive(Acyclic, Default)]
56struct ResolvedImportResolver {56pub struct ResolvedImportResolver {
57 resolved: RefCell<FxHashMap<(SourcePath, ResolvePathOwned), (SourcePath, bool)>>,57 resolved: RefCell<FxHashMap<(SourcePath, ResolvePathOwned), (SourcePath, bool)>>,
58}58}
59impl ResolvedImportResolver {
60 pub fn new() -> Self {
61 Self::default()
62 }
63}
59impl ImportResolver for ResolvedImportResolver {64impl ImportResolver for ResolvedImportResolver {
60 fn load_file_contents(&self, _resolved: &SourcePath) -> crate::Result<Vec<u8>> {65 fn load_file_contents(&self, _resolved: &SourcePath) -> crate::Result<Vec<u8>> {
61 unreachable!("all files should be loaded at this point");66 unreachable!("all files should be loaded at this point");
83}88}
8489
85#[allow(clippy::future_not_send)]90#[allow(clippy::future_not_send)]
86pub async fn async_import<H>(s: State, handler: H, path: &dyn AsPathLike) -> Result<(), H::Error>91pub async fn async_import<H>(
92 s: State,
93 handler: H,
94 path: &dyn AsPathLike,
95) -> Result<SourcePath, H::Error>
87where96where
88 H: AsyncImportResolver,97 H: AsyncImportResolver,
89{98{
90 let resolved = (s.import_resolver() as &dyn Any)99 let resolved = (s.import_resolver() as &dyn Any)
91 .downcast_ref::<ResolvedImportResolver>()100 .downcast_ref::<ResolvedImportResolver>()
92 .expect("for async imports, import_resolver should be set to ResolvedImportResolver");101 .expect("for async imports, import_resolver should be set to ResolvedImportResolver");
93102
103 let entry = handler.resolve_from_default(path).await?;
94 let mut queue = vec![Job::LoadFile {104 let mut queue = vec![Job::LoadFile {
95 path: handler.resolve_from_default(path).await?,105 path: entry.clone(),
96 parse: true,106 parse: true,
97 }];107 }];
98 while let Some(job) = queue.pop() {108 while let Some(job) = queue.pop() {
143 continue;153 continue;
144 }154 }
145 }155 }
146 let resolved = handler.resolve_from(&from, &import.path).await?;156 let resolved_path = handler.resolve_from(&from, &import.path).await?;
157 resolved.resolved.borrow_mut().insert(
158 (from.clone(), import.path.clone()),
159 (resolved_path.clone(), import.expression),
160 );
147 queue.push(Job::LoadFile {161 queue.push(Job::LoadFile {
148 path: resolved,162 path: resolved_path,
149 parse: import.expression,163 parse: import.expression,
150 });164 });
151 }165 }
152 }166 }
153 }167 }
154 Ok(())168 Ok(entry)
155}169}
156170