File size: 3,750 Bytes
1e92f2d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | use anyhow::Result;
use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, Vc};
use turbo_tasks_fs::{
FileContent, FileJsonContent, FileLinesContent, FileSystemPath, LinkContent, LinkType,
};
use crate::version::{VersionedAssetContent, VersionedContent};
/// An asset. It also forms a graph when following [Asset::references].
#[turbo_tasks::value_trait]
pub trait Asset {
/// The content of the [Asset].
#[turbo_tasks::function]
fn content(self: Vc<Self>) -> Vc<AssetContent>;
/// The content of the [Asset] alongside its version.
#[turbo_tasks::function]
fn versioned_content(self: Vc<Self>) -> Result<Vc<Box<dyn VersionedContent>>> {
Ok(Vc::upcast(VersionedAssetContent::new(self.content())))
}
}
#[turbo_tasks::value(shared)]
#[derive(Clone)]
pub enum AssetContent {
File(ResolvedVc<FileContent>),
// for the relative link, the target is raw value read from the link
// for the absolute link, the target is stripped of the root path while reading
// See [LinkContent::Link] for more details.
Redirect { target: RcStr, link_type: LinkType },
}
#[turbo_tasks::value_impl]
impl AssetContent {
#[turbo_tasks::function]
pub fn file(file: ResolvedVc<FileContent>) -> Result<Vc<Self>> {
Ok(AssetContent::File(file).cell())
}
#[turbo_tasks::function]
pub async fn parse_json(self: Vc<Self>) -> Result<Vc<FileJsonContent>> {
let this = self.await?;
match &*this {
AssetContent::File(content) => Ok(content.parse_json()),
AssetContent::Redirect { .. } => {
Ok(FileJsonContent::unparsable("a redirect can't be parsed as json").cell())
}
}
}
#[turbo_tasks::function]
pub async fn file_content(self: Vc<Self>) -> Result<Vc<FileContent>> {
let this = self.await?;
match &*this {
AssetContent::File(content) => Ok(**content),
AssetContent::Redirect { .. } => Ok(FileContent::NotFound.cell()),
}
}
#[turbo_tasks::function]
pub async fn lines(self: Vc<Self>) -> Result<Vc<FileLinesContent>> {
let this = self.await?;
match &*this {
AssetContent::File(content) => Ok(content.lines()),
AssetContent::Redirect { .. } => Ok(FileLinesContent::Unparsable.cell()),
}
}
#[turbo_tasks::function]
pub async fn len(self: Vc<Self>) -> Result<Vc<Option<u64>>> {
let this = self.await?;
match &*this {
AssetContent::File(content) => Ok(content.len()),
AssetContent::Redirect { .. } => Ok(Vc::cell(None)),
}
}
#[turbo_tasks::function]
pub async fn parse_json_with_comments(self: Vc<Self>) -> Result<Vc<FileJsonContent>> {
let this = self.await?;
match &*this {
AssetContent::File(content) => Ok(content.parse_json_with_comments()),
AssetContent::Redirect { .. } => {
Ok(FileJsonContent::unparsable("a redirect can't be parsed as json").cell())
}
}
}
#[turbo_tasks::function]
pub async fn write(self: Vc<Self>, path: FileSystemPath) -> Result<()> {
let this = self.await?;
match &*this {
AssetContent::File(file) => {
path.write(**file).as_side_effect().await?;
}
AssetContent::Redirect { target, link_type } => {
path.write_link(
LinkContent::Link {
target: target.clone(),
link_type: *link_type,
}
.cell(),
)
.as_side_effect()
.await?;
}
}
Ok(())
}
}
|