turbopack_core/introspect/
module.rs

1use anyhow::Result;
2use turbo_rcstr::{RcStr, rcstr};
3use turbo_tasks::{ResolvedVc, ValueToString, Vc};
4
5use super::{
6    Introspectable, IntrospectableChildren,
7    utils::{children_from_module_references, content_to_details},
8};
9use crate::{asset::Asset, module::Module};
10
11#[turbo_tasks::value]
12pub struct IntrospectableModule(ResolvedVc<Box<dyn Module>>);
13
14#[turbo_tasks::value_impl]
15impl IntrospectableModule {
16    #[turbo_tasks::function]
17    pub fn new(asset: ResolvedVc<Box<dyn Module>>) -> Result<Vc<Box<dyn Introspectable>>> {
18        Ok(*ResolvedVc::try_sidecast::<Box<dyn Introspectable>>(asset)
19            .unwrap_or_else(|| ResolvedVc::upcast(IntrospectableModule(asset).resolved_cell())))
20    }
21}
22
23#[turbo_tasks::value_impl]
24impl Introspectable for IntrospectableModule {
25    #[turbo_tasks::function]
26    fn ty(&self) -> Vc<RcStr> {
27        Vc::cell(rcstr!("asset"))
28    }
29
30    #[turbo_tasks::function]
31    fn title(&self) -> Vc<RcStr> {
32        self.0.ident().to_string()
33    }
34
35    #[turbo_tasks::function]
36    fn details(&self) -> Vc<RcStr> {
37        content_to_details(self.0.content())
38    }
39
40    #[turbo_tasks::function]
41    fn children(&self) -> Vc<IntrospectableChildren> {
42        children_from_module_references(self.0.references())
43    }
44}