turbopack_core/introspect/
source.rs

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