Skip to main content

textlane_html/plugins/format/
plugin.rs

1use textlane_core::{Allocator, Attach, Error, File, Plugin, Transform, Tree, TreeKind};
2
3use crate::actions::hast::format::format_hast;
4use crate::actions::hast::minify::minify_hast;
5use crate::models::options::Format;
6
7impl Plugin for Format {
8    fn attach(&self, context: &mut Attach) -> Result<(), Error> {
9        context.add_transform(Box::new(self.clone()));
10        Ok(())
11    }
12}
13
14impl Transform for Format {
15    fn input(&self) -> TreeKind {
16        TreeKind::Hast
17    }
18
19    fn output(&self) -> TreeKind {
20        TreeKind::Hast
21    }
22
23    fn run<'a>(
24        &self,
25        tree: Tree<'a>,
26        _file: &mut File,
27        alloc: &'a Allocator,
28    ) -> Result<Tree<'a>, Error> {
29        match tree {
30            Tree::Hast(mut node) => match self.minify_newlines() {
31                Some(newlines) => {
32                    minify_hast(&mut node, newlines, alloc);
33                    Ok(Tree::Hast(node))
34                }
35                None => Ok(Tree::Hast(format_hast(node, self, alloc))),
36            },
37            _ => Err(Error::new("invalid-stage", "`html.format` takes hast")),
38        }
39    }
40}