Download next.js/turbopack/crates/turbopack-dev-server/src/source/combined.rs from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 3.14 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/turbopack/crates/turbopack-dev-server/src/source/combined.rs
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/next.js/turbopack/crates/turbopack-dev-server/src/source/combined.rs
-
curl -L -o combined.rs https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/turbopack/crates/turbopack-dev-server/src/source/combined.rs
3.14 kB
| use anyhow::Result; | |
| use turbo_rcstr::{RcStr, rcstr}; | |
| use turbo_tasks::{ResolvedVc, TryJoinIterExt, Vc}; | |
| use turbopack_core::introspect::{Introspectable, IntrospectableChildren}; | |
| use super::{ | |
| ContentSource, | |
| route_tree::{RouteTree, RouteTrees}, | |
| }; | |
| use crate::source::ContentSources; | |
| /// Combines multiple [ContentSource]s by trying all content sources in order. | |
| /// | |
| /// The content source which responds with the most specific response (that is | |
| /// not a [ContentSourceContent::NotFound]) will be returned. | |
| pub struct CombinedContentSource { | |
| pub sources: Vec<ResolvedVc<Box<dyn ContentSource>>>, | |
| } | |
| impl CombinedContentSource { | |
| pub fn new(sources: Vec<ResolvedVc<Box<dyn ContentSource>>>) -> Vc<Self> { | |
| CombinedContentSource { sources }.cell() | |
| } | |
| } | |
| impl ContentSource for CombinedContentSource { | |
| async fn get_routes(&self) -> Result<Vc<RouteTree>> { | |
| let all_routes = self | |
| .sources | |
| .iter() | |
| .map(|s| async move { s.get_routes().to_resolved().await }) | |
| .try_join() | |
| .await?; | |
| Ok(Vc::<RouteTrees>::cell(all_routes).merge()) | |
| } | |
| fn get_children(&self) -> Vc<ContentSources> { | |
| Vc::cell(self.sources.clone()) | |
| } | |
| } | |
| impl Introspectable for CombinedContentSource { | |
| fn ty(&self) -> Vc<RcStr> { | |
| Vc::cell(rcstr!("combined content source")) | |
| } | |
| async fn title(&self) -> Result<Vc<RcStr>> { | |
| let titles = self | |
| .sources | |
| .iter() | |
| .map(|&source| async move { | |
| Ok( | |
| if let Some(source) = | |
| ResolvedVc::try_sidecast::<Box<dyn Introspectable>>(source) | |
| { | |
| Some(source.title().await?) | |
| } else { | |
| None | |
| }, | |
| ) | |
| }) | |
| .try_join() | |
| .await?; | |
| let mut titles = titles.into_iter().flatten().collect::<Vec<_>>(); | |
| titles.sort(); | |
| const NUMBER_OF_TITLES_TO_DISPLAY: usize = 5; | |
| let mut titles = titles | |
| .iter() | |
| .map(|t| t.as_str()) | |
| .filter(|t| !t.is_empty()) | |
| .take(NUMBER_OF_TITLES_TO_DISPLAY + 1) | |
| .collect::<Vec<_>>(); | |
| if titles.len() > NUMBER_OF_TITLES_TO_DISPLAY { | |
| titles[NUMBER_OF_TITLES_TO_DISPLAY] = "..."; | |
| } | |
| Ok(Vc::cell(titles.join(", ").into())) | |
| } | |
| async fn children(&self) -> Result<Vc<IntrospectableChildren>> { | |
| Ok(Vc::cell( | |
| self.sources | |
| .iter() | |
| .copied() | |
| .map(|s| async move { Ok(ResolvedVc::try_sidecast::<Box<dyn Introspectable>>(s)) }) | |
| .try_join() | |
| .await? | |
| .into_iter() | |
| .flatten() | |
| .map(|i| (rcstr!("source"), i)) | |
| .collect(), | |
| )) | |
| } | |
| } | |