turbo_tasks_env/
custom.rs

1use anyhow::Result;
2use turbo_rcstr::RcStr;
3use turbo_tasks::{ResolvedVc, Vc};
4
5use crate::{EnvMap, ProcessEnv, case_insensitive_read};
6
7/// Allows providing any custom env values that you'd like, deferring the prior
8/// envs if a key is not overridden.
9#[turbo_tasks::value]
10pub struct CustomProcessEnv {
11    prior: ResolvedVc<Box<dyn ProcessEnv>>,
12    custom: ResolvedVc<EnvMap>,
13}
14
15#[turbo_tasks::value_impl]
16impl CustomProcessEnv {
17    #[turbo_tasks::function]
18    pub fn new(prior: ResolvedVc<Box<dyn ProcessEnv>>, custom: ResolvedVc<EnvMap>) -> Vc<Self> {
19        CustomProcessEnv { prior, custom }.cell()
20    }
21}
22
23#[turbo_tasks::value_impl]
24impl ProcessEnv for CustomProcessEnv {
25    #[turbo_tasks::function]
26    async fn read_all(&self) -> Result<Vc<EnvMap>> {
27        let prior = self.prior.read_all().owned().await?;
28        let custom = self.custom.owned().await?;
29
30        let mut extended = prior;
31        extended.extend(custom);
32        Ok(Vc::cell(extended))
33    }
34
35    #[turbo_tasks::function]
36    async fn read(&self, name: RcStr) -> Result<Vc<Option<RcStr>>> {
37        let custom = case_insensitive_read(*self.custom, name.clone());
38        match &*custom.await? {
39            Some(_) => Ok(custom),
40            None => Ok(self.prior.read(name)),
41        }
42    }
43}