polysite/compiler/
path.rs

1use crate::{
2    builder::metadata::{PATH_META, TARGET_FILE_META},
3    *,
4};
5use tracing_error::SpanTrace;
6
7/// [`SetExtension`] changes target file's extension and URL path extension to specified one.
8#[derive(Clone)]
9pub struct SetExtension(String);
10impl SetExtension {
11    pub fn new(ext: impl AsRef<str>) -> Self {
12        Self(ext.as_ref().to_owned())
13    }
14}
15impl Compiler for SetExtension {
16    #[tracing::instrument(skip(self, ctx))]
17    fn next_step(&mut self, mut ctx: Context) -> CompilerReturn {
18        let ext = self.0.clone();
19        compile!({
20            let mut target = ctx.target().await.ok_or(Error::InvalidMetadata {
21                trace: SpanTrace::capture(),
22            })?;
23            let mut path = ctx.path().await.ok_or(Error::InvalidMetadata {
24                trace: SpanTrace::capture(),
25            })?;
26            target.set_extension(ext.clone());
27            ctx.metadata_mut().insert_local(
28                TARGET_FILE_META.to_owned(),
29                Metadata::to_value(target.to_string_lossy())?,
30            );
31            path.set_extension(ext);
32            ctx.metadata_mut().insert_local(
33                PATH_META.to_owned(),
34                Metadata::to_value(path.to_string_lossy())?,
35            );
36            Ok(CompileStep::Completed(ctx))
37        })
38    }
39}